Spring and the dynamic inclusion of JSP files

I am starting to build web applications in Spring 3 (and in J2EE) in general. Looking at the loopback example, I saw that the programmer creates many parts of the JSP, for example, the header, includes a footer, and then stitches them together using static inclusion. Anyway, I would like to be able to 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 need the main page, and then the ability to insert the ModelAndView returned by the controller, which parts of the pages are displayed in each situation (with data attached to it). This works, but does not give errors if ${subpage} not found, the jsp name is incorrect or missing. I would like more error checking ...

Is this the best and recommended way to do this? And if that seems like a good idea for what I mean, what is the right way to do this?

+6
java spring spring-mvc jsp
source share
4 answers

It seems you have extra quotes in the subpage . Get rid of them. For example:

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

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

+1
source share

You might want to use the Apache Tiles 2 integration to manage your JSP files. Spring has good integration with Apache Tiles support. It also shows if there is an error on your page. I gave an example of this at http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html

+2
source share

To check for errors you can use:

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

and then you can check it with:

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

Take a look at Sitemesh (http://www.opensymphony.com/sitemesh). This is an easy-to-use servlet-based page layout system. I completed a number of projects using it using Spring MVC, and it worked very well.

0
source share

All Articles