如何将 Sitemesh 3 与 Spring MVC 3 集成?

Posted

技术标签:

【中文标题】如何将 Sitemesh 3 与 Spring MVC 3 集成?【英文标题】:How do I integrate Sitemesh 3 with Spring MVC 3? 【发布时间】:2013-01-24 09:38:45 【问题描述】:

我正在尝试使用 Sitemesh 3 来控制 Spring MVC 应用程序的 JSP 输出的修饰。

当我点击应用程序时,Sitemesh 似乎正在向 Spring servlet 发出请求以尝试检索其装饰器文件。这可能是正确的行为,也可能不是正确的行为,但它让我很头疼。

我对 Sitemesh 3 的理解是它在 Spring 之后才开始工作,即在 Response 对象上工作。

我在浏览器中遇到的错误是 404,并且在日志中(配置/代码如下):

INFO: Server startup in 1367 ms
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 
'springiness' processing GET request for [/clientmanager/]^M
DEBUG: 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 
Looking up handler method for path /^M
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 
Returning handler method [public java.lang.String 
uk.co.hermes.HomeController.home(java.util.Locale,org.springframework.ui.Model)]^M
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning 
cached instance of singleton bean 'homeController'^M
DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for 
[/clientmanager/] is: -1^M
INFO : uk.co.hermes.HomeController - Welcome home! The client locale is en_GB.^M
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking     
afterPropertiesSet() on bean with name 'home'^M
DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view   
[org.springframework.web.servlet.view.JstlView: name 'home'; URL [/WEB- 
INF/jsp/home.jsp]] in DispatcherServlet with name 'springiness'^M
DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'serverTime'  
of type [java.lang.String] to request in view with name 'home'^M
DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-  
INF/jsp/home.jsp] in InternalResourceView 'home'^M
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed  
request^M
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 
'springiness' processing GET request for [/clientmanager/WEB- 
INF/decorators/mainDecorator.html]^M
DEBUG:  
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping -  
Looking up handler method for path /WEB-INF/decorators/mainDecorator.html^M
DEBUG:  
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping -  
Did not find handler method for [/WEB-INF/decorators/mainDecorator.html]^M
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request 
with URI [/clientmanager/WEB-INF/decorators/mainDecorator.html] in DispatcherServlet 
with name 'springiness'^M
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request^M

我怀疑这是我的 web.xml 以及我如何定义映射(URL)的问题:

<servlet>
    <servlet-name>springiness</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springiness</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>sitemeshfilter</filter-name>
    <filter-class>uk.co.hermes.filters.SitemeshFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>sitemeshfilter</filter-name>
    <!-- leaving SitemeshFilter class to decide which responses it should decorate -->
    <url-pattern>/*</url-pattern>
</filter-mapping>

我的自定义过滤器:

public class SitemeshFilter extends ConfigurableSiteMeshFilter 

private Logger log = LoggerFactory.getLogger(SitemeshFilter.class);

/**
 * See http://wiki.sitemesh.org/display/sitemesh3/Configuring+SiteMesh+3
 */
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) 
log.debug("** hit the sitemesh filter");
        // apply this decorator (template) to the path defined...
        builder.addDecoratorPath("/*", "/WEB-INF/decorators/mainDecorator.html");

        // ... when the response type matches one of these 
        builder.setMimeTypes("text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml");

WEB-INF/ |-jsp |-home.jsp |-装饰器 |-mainDecorator.html

还有我的超级简单控制器:

    @RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) 
    logger.info("Welcome home! The client locale is .", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );

    return "home";

【问题讨论】:

Sitemesh 2.4.2 适用于 spring3 .. 【参考方案1】:

在我的例子中,我使用了这个小tutorial 来使它工作。

【讨论】:

此链接已损坏 谢谢,我修好了。【参考方案2】:

这里是another blog,它显示了 Sitemesh 3 和 Spring MVC 之间的集成

【讨论】:

链接到外部材料通常是可以的,但请确保包含一些您自己的文本,以便您的答案具有其自身价值。【参考方案3】:

由于没有人发布实际内容,所以你去:

pom.xml 添加:

<dependency>
    <groupId>org.sitemesh</groupId>
    <artifactId>sitemesh</artifactId>
    <version>3.0.0</version>
</dependency>

WEB-INF/web.xml † 添加:

<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

WEB-INF/sitemesh3.xml 中添加:

<sitemesh>
    <mapping path="/*" decorator="/WEB-INF/decorator1.jsp"/>
</sitemesh>

WEB-INF/decorator1.jsp 添加:

<html>
    <head>
    ...
    </head>
    <body>
        <sitemesh:write property='body'/>
    </body>
</html>

† 如果使用 Spring Security,请将其放在 Spring Security 过滤器链下方。

【讨论】:

以上是关于如何将 Sitemesh 3 与 Spring MVC 3 集成?的主要内容,如果未能解决你的问题,请参考以下文章

spring boot 集成 sitemesh

Spring MVC 和模板变量

Spring Security 应用程序中的 Sitemesh 未装饰自定义错误页面

spring mvc freemarker 怎么让指定的控制器不适用freemarker

Spring Security、Sitemesh 和 Taglibs

Spring Session 不适用于 Sitemesh