How to JSP display multiple page with one layout (Example index.jsp?page=about display about.jsp)
I new in JSP, i have a problem with JSP
in php i use
$page=$_GET["page"]
for display multiple page for one layout it mean i have index , it display layout and when i click on menu go to about us the index url = index.jsp?page=about in PHP when i declare $page above and next step i do
Sw开发者_Go百科itch($page){
case 1:about
include 'aboutus.php'
case 2:news
include 'news.php'
}
How can i do it ? How jsp can do the same way php to display multiple page in 1 layout
Use jsp:include
.
<jsp:include page="/WEB-INF/${param.page}.jsp" />
And pass ?page=news
or ?page=about
, etc as parameter. The ${param.page}
prints the outcome of request.getParameter("page")
. You can prevent direct access to JSP files (by entering URL in browser address bar) by placing JSP files in /WEB-INF
folder.
See also:
- Basic JSP/Servlet tutorials
- Hidden features of JSP/Servlet
- How to avoid Java code in JSP
nowadays you use "templates" of Java Server Faces (JSF) for this approach. When you use JSP, you actually don't use the same concept as in PHP. You'd better use the MVC concept. But to answer your question, you could probably achieve this with the include tag http://java.sun.com/products/jsp/tags/11/syntaxref1112.html and control it with JSTL: http://www.java2s.com/Code/Java/JSTL/JSTLiftag.htm
精彩评论