开发者

Spring and dynamic inclusion of JSP files

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 开发者_JAVA技巧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?


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


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")


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜