如何将Sitemesh 3与Spring MVC 3集成在一起?

编程入门 行业动态 更新时间:2024-10-07 20:34:14
本文介绍了如何将Sitemesh 3与Spring MVC 3集成在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to use Sitemesh 3 to control the decoration of JSP output from a Spring MVC application.

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

When I hit the application it seems that Sitemesh is making a request to the Spring servlet to try and retreive its decorator file. This may, or may not be correct behaviour but it is causing me all many of headaches.

我对Sitemesh 3的理解是,它在Spring之后即响应对象上完成工作.

My understanding of Sitemesh 3 is that it does its work after Spring, ie on the Response object.

我在浏览器中遇到的错误是404和日志中的错误(后面是config/code):

The error I'm getting in the browser is a 404 and in the logs (config/code follows):

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)的问题:

I suspect it's a problem in my web.xml and how I've defined the mappings(URLs):

<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 wiki.sitemesh/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

WEB-INF/ |-jsp |-home.jsp |-decorators |-mainDecorator.html

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

And my super simple Controller:

@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"; }

推荐答案

由于没有人发布实际内容,因此您可以进行以下操作:

Since no one has posted actual content, here you go:

在pom.xml中添加:

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

中的

†添加:

in WEB-INF/web.xml † add:

<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过滤器链的下方.

† put this below your Spring Security Filter Chain if using Spring Security.

更多推荐

如何将Sitemesh 3与Spring MVC 3集成在一起?

本文发布于:2023-11-23 22:07:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   Sitemesh   MVC   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!