sitemesh和spring MVC装饰模式问题
我有弹簧工作的sitemesh,这是配置:decorator.xml
/exclude.jsp /exclude/* /*
这是我的web.xml
contextConfigLocation /WEB-INF/config/web-application-config.xml springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* sitemesh com.opensymphony.sitemesh.webapp.SiteMeshFilter sitemesh /* REQUEST FORWARD ERROR org.springframework.web.context.ContextLoaderListener Resources Servlet org.springframework.js.resource.ResourceServlet 0 Resources Servlet /resources/* Spring MVC Dispatcher Servlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation 1 Spring MVC Dispatcher Servlet /spring/* index.html
这项工作,但是当我在decorator.xml中更改类似的模式时
/spring/cliente/index
它不起作用,我尝试了很多组合而没有。 然后我像这样在web.xml中更改spring servlet的映射
Spring MVC Dispatcher Servlet * .htm
并定义一个这样的新模式 :
/cliente/index.htm
它有效,所以有没有办法使这个与弹簧servlet的映射一起使用?
Spring MVC Dispatcher Servlet /spring/*
问题是SiteMesh使用Request.getServletPath(),它在你的spring mvc应用程序中将为所有内容返回“/ spring”。 我通过实现com.opensymphony.module.sitemesh.DecoratorMapper接口并使用它代替普通的ConfigDecoratorMapper来找到它。 然后我能够检查用于将装饰器映射到请求的各种参数。 不幸的是,我认为这使得您唯一的选择是在DispatcherServelet映射中使用* .html后缀或其某些变体。
另一种选择是配置PageDecoratorMapper并在原始未修饰页面中使用此标记来指定要使用的布局:
虽然你失去了url映射的好处。
我遇到了那个确切的问题。 发生的事情是,您在web.xml中指定的url路径的任何部分在传递给Spring之前都会被Web服务器剥离,但前提是您将通配符放在最后。 您已经发现当您的url是www.myapp.com/spring/cliente/index.html时,如果您将其放入web.xml中
Spring MVC Dispatcher Servlet /spring/*
Spring只会在/ spring之后看到请求路径的一部分。 在这种情况下,您需要将RequestMapping指定为“/cliente/index.html”。
您也可以通过这种方式指定servlet映射。
Spring MVC Dispatcher Servlet *.html
然后Spring将看到整个请求路径,您可以指定您的请求映射,例如“/spring/cliente/index.html”。 Sitemesh也是如此。 它只能看到Web服务器通过的内容。
也许这对某人有用,我遇到了同样的问题,经过google和stading sitemesh资源的研究后,通过扩展ConfigDecoratorMapping来解决问题。 就这个:
/** * Created by IntelliJ IDEA. * User: Inf-root * Date: 30.06.11 * Time: 1:00 * */ public class ConfigDecoratorMapperSpringMvcSupport extends ConfigDecoratorMapper { private static final Logger LOG = Logger.getLogger(ConfigDecoratorMapperSpringMvcSupport.class); private ConfigLoader configLoader = null; /** Create new ConfigLoader using '/WEB-INF/decorators.xml' file. */ public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException { LOG.debug("init()..."); super.init(config, properties, parent); try { String fileName = properties.getProperty("config", "/WEB-INF/decorators.xml"); configLoader = new ConfigLoader(fileName, config); } catch (Exception e) { throw new InstantiationException(e.toString()); } } /** Retrieve {@link com.opensymphony.module.sitemesh.Decorator} based on 'pattern' tag. */ public Decorator getDecorator(HttpServletRequest request, Page page) { LOG.debug("getDecorator()..."); String thisPath = request.getServletPath(); LOG.debug("\tThisPath: " + thisPath); String requestURI = request.getRequestURI(); LOG.debug("\t\tGet request URI: " + requestURI); //TODO check indexes thisPath = "/springURITemplate" + requestURI.substring(request.getContextPath().length(), requestURI.length() - 1); LOG.debug("\t\t\tThisPath: " + thisPath); String name = null; try { name = configLoader.getMappedName(thisPath); } catch (ServletException e) { e.printStackTrace(); } LOG.debug("\tResolved decorator name: " + name); Decorator result = getNamedDecorator(request, name); LOG.debug("Decorator is null ? " + (result == null)); return result == null ? super.getDecorator(request, page) : result; } }
而我的decorators.xml包含这样的东西
/springURITemplate/a/administration*
使用Spring 3.0.5在tomcat 7上测试
你尝试过/ spring / cliente / index *或/ spring / cliente / index / *吗?