Jsp页面层次结构 [英] Jsp Pages Hierarchy
问题描述
我有一个复杂的网站设计(从Web上下载了一个设计+ CSS),并且我想巧妙地使用include
s,以便可以将设计与内容分开.
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.
但是,这有一些复杂之处.内容位于<div>
内,而<div>
内,依此类推.
我如何使用包含,以便基本上可以将站点的每个可重复方面(标题,导航)保存在其自己的文件中,并且对于每个实际页面,jsp中都没有任何设计?
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 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
推荐答案
您可能应该看看 Apache Tiles 因为它可以为您完成大部分任务.
You should probably look at Apache Tiles since it does most of this for you.
如果要自己滚动,可以创建两个文件,分别在页面的开头和结尾处分别标头header.jsp和footer.jsp:
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
header.jsp
<html>
<head>
</head>
<body>
<div>
//header content
</div>
<div>
//main content
和footer.jsp
and, footer.jsp
</div>
</body>
</html>
并将两者都包含在您的内容页面中.
And include both in your content pages.
或者,您可以创建一个layout.jsp页面:
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.
示例: 在您的Servlet中,您可以为变量指定一个类似这样的值:
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
这样,将在layout.jsp页面上显示内容.您当然可以创建多个这样的变量占位符.
This way, on the layout.jsp page the content will be displayed. You can of course create several such variable placeholders.
第三种选择: 使用jsp:include标记可包含动态内容.例如 layout.jsp页面将如下所示:
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>
include标签将在给定的URL处获取页面.使用此建议的原因是,当您拥有更复杂的显示时,最好使用JSP页面来构造显示.然后,该servlet将仅与业务逻辑有关.您可以为每个显示创建一个单独的jsp页面. servlet看起来像这样:
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
这篇关于Jsp页面层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!