Spring和JSP文件的动态包含
一般来说,我开始在 Spring 3(以及 J2EE)中构建 Web 应用程序。 查看 petclinic 示例,我发现程序员创建了许多 JSP 片段,例如页眉、包含、页脚,然后使用静态包含将它们缝合在一起。无论如何,我想要的是我可能有一个基本页面,例如 Base.jsp
并且能够包含这样的内容:
<body>
<jsp:include page="${subpage}"></jsp:include>
</body>
原因是我想要一个主页,然后能够放入控制器返回的 ModelAndView
中,页面的哪些部分在每种情况下显示(附有数据)。这是可行的,但如果找不到 ${subpage}
、jsp 名称错误或丢失,它不会给出任何错误。我想要更多的错误检查...
这是最好和推荐的方法吗?如果这对于我的想法来说似乎是个好主意,那么正确的做法是什么?
I'm starting building web apps in Spring 3 (and in J2EE) in general.
Looking at the petclinic example I've seen that the programmer creates many JSP pieces, like header, includes, footer and then stitches them together using static inclusion. Anyway what I'd like is that I may have a base page, like Base.jsp
and be able to include things like this:
<body>
<jsp:include page="${subpage}"></jsp:include>
</body>
the reason is that I'd like a main page, then being able to put in the ModelAndView
returned by the controller which parts of the pages display in each situation (with the data attached to it). This works, but it gives no errors in case ${subpage}
is not found, the jsp name is wrong or missing. I'd like more error checking...
Is this the best and recommended way to do this? And if this seems a good idea for what I've in mind, what's the correct way of doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能希望使用 Apache Tiles 2 集成来管理 JSP 文件。 Spring 具有良好的集成支持 Apache Tiles。它还会显示您的页面中是否存在错误。我已将其示例放在 http: //krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html
You might want to use Apache Tiles 2 integration for managing your JSP files. Spring has good integration support Apache Tiles. It also shows if there's an error in your page. I've put an example of it at http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html
您的
子页面
中似乎还有其他引号。摆脱他们。例如:如果您必须在控制器或 servlet 中设置它 - 只需使用
request.setAttribute("subpage", "/jsp/index.jsp")
It appears you have additional quotes in your
subpage
. Get rid of them. For example:If you have to set it in a controller or servlet - just use
request.setAttribute("subpage", "/jsp/index.jsp")
对于错误检查,您可以使用:
稍后您可以使用以下命令进行检查:
For error checking you can use:
and later you can check it with:
看看 Sitemesh (http://www.opensymphony.com/sitemesh)。它是一个基于 servlet 过滤器的页面布局系统,易于使用。我已经使用它和 Spring MVC 完成了许多项目,并且效果非常好。
Take a look at Sitemesh (http://www.opensymphony.com/sitemesh). It is a servlet filter-based page layout system that is easy to use. I have done a number of projects using it with Spring MVC and it worked very well.