Spring和JSP文件的动态包含

发布于 2024-10-14 09:53:29 字数 488 浏览 3 评论 0原文

一般来说,我开始在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好多鱼好多余 2024-10-21 09:53:29

您可能希望使用 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

送舟行 2024-10-21 09:53:29

您的子页面中似乎还有其他引号。摆脱他们。例如:

<c:set var="subpage" value="/jsp/index.jsp" />

如果您必须在控制器或 servlet 中设置它 - 只需使用 request.setAttribute("subpage", "/jsp/index.jsp")

It appears you have additional quotes in your subpage. Get rid of them. For example:

<c:set var="subpage" value="/jsp/index.jsp" />

If you have to set it in a controller or servlet - just use request.setAttribute("subpage", "/jsp/index.jsp")

贪恋 2024-10-21 09:53:29

对于错误检查,您可以使用:

<c:catch var="myException">
   <c:import url="${subpage}" />
</c:catch>

稍后您可以使用以下命令进行检查:

<c:if test="${myException != null}">
...
</c:if>

For error checking you can use:

<c:catch var="myException">
   <c:import url="${subpage}" />
</c:catch>

and later you can check it with:

<c:if test="${myException != null}">
...
</c:if>
画骨成沙 2024-10-21 09:53:29

看看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文