通过Spring MVC框架包含其他JSP是一个好主意吗? [英] Is including other JSP via the Spring MVC framework a good idea?

查看:133
本文介绍了通过Spring MVC框架包含其他JSP是一个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个模糊而宏大的问题,但希望我能用尽可能少的具体例子来解释它.

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

我们最近将Spring MVC转换为我们的应用程序框架,但发现在开发过程中有一个(实际上,只有一个)限制因素:如何在适当的模型中包含动态视图.

We recently switched to Spring MVC for our application framework but found one (and really, only one) limiting factor during development: how to include dynamic views with the appropriate model.

例如,我们正在创建一个包含可重用片段的页面.左边是一个随机q和一个"片段,而上面是一个通用的导航"片段.

For instance, we are creating a page that contains reusable fragments. On the left we have a "random q and a" fragment while on the top we have a common "navigation" fragment.

每个片段都需要不同的模型.我一直在指示正在创建导航"部分的开发人员创建导航模型,控制器和视图,并将"q和a"模型,控制器和逻辑完全分开.如果另一个页面布局需要导航"而不是"q and a"或反之,则这是为了鼓励可重用性.

Each of these fragments requires a different model. I have been instructing the developer that is creating the "navigation" portion to create a navigation model, controller and view completely separate of the "q and a" model, controller and logic. This is to encourage reusability if another page layout wants the "navigation" but not the "q and a" or vice verse.

您知道我要去哪里吗? 主页"页面包含两个片段,但是最好不必知道"片段需要哪个控制器/模型/视图.

Do you see where I am going with this? The "home" page includes both fragments but it would be nice to not have to "known" which controller/model/view the fragments need.

我一直在指示开发人员以以下方式使用Spring MVC....

I've been instructing developers to use Spring MVC in the following manner....

home.jsp的示例:

Example of home.jsp:

<body>
    <div class="top">
        <jsp:include page="/navigation"/>
    </div>
    <div class="left">
        <jsp:include page="/randomgQuestion"/>
    </div>
</html>

这个想法是在请求时,将必要的其他片段与它们所需的模型一起动态引入.

The idea is at request time the necessary other fragments will be pulled in dynamically with the models that they require.

这是个好主意吗?有更好的方法吗?

Is this a good idea? Is there a better way?

欢迎任何讨论,但请保持建设性.

Any discussion is welcome but please be constructive.

目标是可重用性和愚蠢的看法.

我将根据要求提供任何更新或澄清.谢谢.

I'll offer any updates or clarifications upon request. Thank you.

推荐答案

您所描述的内容有点像门户/portlet功能(JSR-286)=>,即应用程序(门户)生成的网页由内容由其他嵌入式应用程序(portlet)生成.门户网站是使用INCLUDE调度 (等效于<jsp:include>)以提供JSR-286功能.因此,从这个角度来看,最好使用<jsp:include>提供可重用的内容块,每个内容块都有自己的MVC生命周期(尽管共享相同的请求属性名称空间)...

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

还要注意,如果您只想在JSP之间重用一个简单的片段,那么一个简单的<%@include file="menu.jspf" %>可能会更好.

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.

我也觉得应该提到JSP标记功能...将可重用的内容作为JSP TAG文件(/WEB-INF/tags/[taglib-folder/]*.tag)可以提供一些高级布局功能.要获得更多高级功能,您可以实现基于Java的标记库.

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.

为了说明我如何在一个项目中使用自定义TAG并包含指令,以下是一个JSP视图:

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>

我们没有用例,需要用INCLUDE分派(即<jsp:include />).

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

这篇关于通过Spring MVC框架包含其他JSP是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆