Jsp Pages Hierarchy
I have a complicated website design (downloaded a design + css from the web) and I want to cleverly use include
s, so that I can separate the design from the content.
However, there's some complication with that. the content is located inside a <div>
which is inside a <div>
, etc.
How do I use includes so that basically, I can have each repeatable aspect of the site (header, navigation) in its own file, and for every actual page, have none开发者_如何学Python of the design in the jsp?
i.e., in a certain page, I only want to have
//possible includes
<h1>Hello World!</h1>
//possible includes
You should probably look at Apache Tiles since it does most of this for you.
If you want to roll your own you can create two files, say header.jsp and footer.jsp with the start and end of the page:
header.jsp
<html>
<head>
</head>
<body>
<div>
//header content
</div>
<div>
//main content
and, footer.jsp
</div>
</body>
</html>
And include both in your content pages.
Alternatively, you can create a single layout.jsp page:
<html>
<head>
</head>
<body>
<div>
//header content
</div>
<div>
<c:out value="${content}"/>
</div>
</body>
</html>
Then all request will populate the content variable then load the layout page. This way you do not have to repeat the includes all over the place.
Example: In your servlet you can give the variable a value like this:
String content = "<h1>Hello World!</h1>";
request.setAttribute("content", content);
//forward to layout.jsp
This way, on the layout.jsp page the content will be displayed. You can of course create several such variable placeholders.
Third Option: Use a jsp:include tag to include dynamic content. For example The layout.jsp page will look like this:
<html>
<head>
</head>
<body>
<div>
//header content
</div>
<div>
<jsp:include page="${page}"/>
</div>
</body>
</html>
The include tag will fetch the page at the given url. The reason for using this suggestion is when you have more complicated displays it would be better to use a JSP page to construct the display. The servlet will then only be concerned with the business logic. You can create a separate jsp page for each display. The servlet will look like this:
//business logic
//save the data to be displayed in the request
String page = "nextpagetodisplay.jsp";
request.setAttribute("page", page);
//forward to layout.jsp
精彩评论