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

前端 未结 2 786
有刺的猬
有刺的猬 2021-01-07 06:59

This is a vague and grand question but hopefully I can explain it with as little concrete examples as possible.

We recently switched to Spring MVC for our applicatio

相关标签:
2条回答
  • 2021-01-07 07:41

    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.

    0 讨论(0)
  • 2021-01-07 07:50

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

    • http://tiles.apache.org

    • http://wiki.sitemesh.org/wiki/display/sitemesh/Home

    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.

    0 讨论(0)
提交回复
热议问题