简体   繁体   中英

JSP dynamically change include page

I have a <div> tag:

<div id="content">
    <jsp:include page="some.jsp"/>
</div>

Also I have a few buttons:

<input type="submit" name="btn1" value="Page1">
<input type="submit" name="btn2" value="Page2">

I need when I click btn1 some.jsp changes to page1.jsp and to page2.jsp when click btn2.

Use dynamic include,

<jsp:include page="<%= myVariable %>" flush="true" />

Pd: Take a look a flush http://www.coderanch.com/t/484149/JSP/java/flush-true-jsp-include-tag .

1- Instead of using static include, you can use dynamic include, then you can do something like this:

 <jsp:include page="${somePage}" flush="true" />

2- Use javascript to change the action of the form depending on the button you click:

 <input type="submit" value="Page1" name="btn1" 
  onclick="document.forms[0].action = 'somePage.jsp'; return true;" />

Im using this solution : My form is :

  <FORM> <select name="choix"> <option value="choix 1">choix 1</option> <option value="choix 2">choix 2</option> <option value="choix 3">choix 3</option> </select> <input type="submit" /> </FORM> 

and im using this in the same jsp page to include a page as what i select in that form:

  <% Ch = request.getParameter("choix"); %> <div id="inculde_page"> <jsp:include page="<%= "layouts/" + Ch + ".jsp" %>" /> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM