如何在我的Spring MVC Web应用程序中添加.jsp页眉和页脚? [英] How do I add .jsp headers and footers to my Spring MVC web app?

查看:196
本文介绍了如何在我的Spring MVC Web应用程序中添加.jsp页眉和页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将.jsp页眉和页脚添加到我的Spring MVC网页应用程序?

How do I add .jsp headers and footers to my Spring MVC web app?

我知道有很多不同的答案,但我想知道(它们都是真的,但更重要的是)这样做的正确方法是什么?我只是在学习Spring,我有一个提示答案就在于HandlerInterceptor。现在,我可能只是这样做.jsp包括。即使有这个包含解决方案,你能详细说明我在哪里放置页眉/页脚结构吗?任何建议或方向都会很棒。

I know there's many different answers, but I would like to know (them all really but more importantly) what is the proper way to do this? I'm just learning Spring and I have a hint the answer lies with HandlerInterceptor. For now, I might just do so .jsp includes. Even with this include solution, could you detail where I would place the headers/footers structurally? Any advice or direction would be great.

推荐答案

我在研究时发现了你的问题:-)不确定我的解决方案是否< good |坏|一个黑客|已经存在> 或者如果有更好的方法,但它适用于我当前的项目。

I found your question whilst researching :-) Not sure if my solution is <good | bad | a hack | already exists> or if there is a better way but it's working for my current project.

myapp-中servlet.xml 您可以使用自己的实现扩展viewResolver viewClass

In your myapp-servlet.xml you can extend the viewResolver viewClass with your own implementation:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:viewClass="com.my.app.view.JstlView"
  p:prefix="/WEB-INF/views/"
  p:suffix=".jsp"/>

通过覆盖renderMergedOutputModel,您可以强制所有视图真正成为模板您可以在其中定义全局布局,然后只需< jsp:include /> 您的部分。

By overriding the renderMergedOutputModel you can force all views to really be a template in which you can define your global layout and then simply <jsp:include/> your partial(s).

package com.my.app.view;

import java.util.*;
import org.springframework.web.servlet.view.InternalResourceView;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JstlView extends InternalResourceView {
    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String dispatcherPath = prepareForRendering(request, response);

        // set original view being asked for as a request parameter
        request.setAttribute("partial", dispatcherPath.substring(dispatcherPath.lastIndexOf("/") + 1);

        // force everything to be template.jsp
        RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/template.jsp");
        rd.include(request, response);
    }
}

如果你看看Spring的 InternalResourceView.java 在确定要使用的视图时,您将更好地了解Spring正在做什么。

If you look Spring's InternalResourceView.java you'll get a better idea of what Spring is doing when determining what view to use.

<!doctype html>
<html lang="en">
<head></head>
<body>
    <header>
        <jsp:include page="header.jsp"/>
    </header>
    <jsp:include page="${partial}"/>
    <footer>
        <jsp:include page="footer.jsp"/>
    </footer>
</body>
</html>

如何使用EL获取JSP中的request / session / servletcontext属性?帮助我获取属性值 $ {partial} out。

How to obtain request / session / servletcontext attribute in JSP using EL? helped me here with getting the attribute value ${partial} out.

<p>I'm a partial!</p>

然后在控制器中,返回 simple_partial 查看

Then in a controller, return the simple_partial view

package com.my.app.controller;

import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/")
public class App{
    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "simple_partial";
    }
}

将被 template.jsp 并回复了。

这篇关于如何在我的Spring MVC Web应用程序中添加.jsp页眉和页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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