Is including other JSP via the Spring MVC framework a good idea?

非 Y 不嫁゛ 提交于 2019-12-01 00:27:29

What you are describing feels a bit like portal / portlets functionality (JSR-286) => i.e. application (portal) generating webpages which are composed of content generated by other embedded applications (portlets). Portals are using INCLUDE dispatch (which is equivalent to <jsp:include>) to provide JSR-286 functionality. So from this point of view, it is a good idea to use <jsp:include> to provide reusable content blocks, each with its own MVC lifecycle (although sharing the same request attribute namespace)...

Also note that if you have just a simple fragment, which you would like to reuse in between JSPs, a simple <%@include file="menu.jspf" %> might be a better fit.

And I also feel that JSP tag functionality should be mentioned... making reusable content as a JSP TAG file (/WEB-INF/tags/[taglib-folder/]*.tag) can provide some advanced layout features. For even more advanced functionalities, you can implement Java based tag library.


To illustrate how I am using custom TAGs and include directive in one project, the following is a single JSP view:

<%@ include file="/WEB-INF/taglib.jspf" %>
<layout:admin section="test">
    <layout:admin-context />
    <layout:admin-content>
        <h1><spring:message code="test.overview.heading" /></h1>
        <h2><spring:message code="test.detail.heading" /></h2>
        <%@ include file="test-detail.jspf" %>
    </layout:admin-content>
</layout:admin>

We didn't have use-case, where INCLUDE dispatch (i.e. <jsp:include />) would be needed.

Well in terms of your UI, Apache Tiles and Sitemesh are things you might want to look at here.

In terms of the controller layer, Spring has the @ControllerAdvice annotation which can be used to place model attributes in scope for all controllers. If, for example, you placed the navigation model in your @ControllerAdvice, no other controller would have to worry about setting it as a model attribute.

http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html

@ModelAttribute methods can also be defined in an @ControllerAdvice-annotated class and such methods apply to all controllers. The @ControllerAdvice annotation is a component annotation allowing implementation classes to be autodetected through classpath scanning.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!