首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sitemesh和spring装饰器模式问题

sitemesh和spring装饰器模式问题
EN

Stack Overflow用户
提问于 2010-05-18 16:09:47
回答 4查看 19.2K关注 0票数 2

我有spring的站点,这是配置: decorator.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/styles">
    <excludes>
        <pattern>/exclude.jsp</pattern>
        <pattern>/exclude/*</pattern>
    </excludes>
    <decorator page="application/themeManager/theme.jsp" name="dos">
        <pattern>/*</pattern>
    </decorator>
</decorators>

这是我的web.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <!-- The master configuration file for this Spring web application -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/web-application-config.xml
        </param-value>
    </context-param>

    <!-- Enables Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

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


    <!-- Agregamos el filtro de sitemesh que permite interceptar todas las llamadas que necesitamos -->
    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <!-- Loads the Spring web application context -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Serves static resource content from .jar files such as spring-faces.jar -->
    <servlet>
        <servlet-name>Resources Servlet</servlet-name>
        <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>

    <!-- Map all /resources requests to the Resource Servlet for handling -->
    <servlet-mapping>
        <servlet-name>Resources Servlet</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>

    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all *.spring requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/spring/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

这是可行的,但是当我将decorator.xml中的模式更改为类似

代码语言:javascript
复制
<decorator page="application/themeManager/theme.jsp" name="dos">
    <pattern>/spring/cliente/index</pattern>
</decorator>

它不起作用,我尝试了很多组合,但什么也没有。然后更改web.xml中servlet的映射,如下所示

代码语言:javascript
复制
          Spring MVC Dispatcher Servlet         \*.htm       

并定义如下新模式

代码语言:javascript
复制
<decorator page="application/themeManager/theme.jsp" name="dos">
    <pattern>/cliente/index.htm</pattern>
</decorator>

它可以工作,那么有什么方法可以使它与servlet的映射一起工作呢?

代码语言:javascript
复制
<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/spring/*</url-pattern>
</servlet-mapping>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-10-29 14:46:22

问题是SiteMesh使用了Request.getServletPath(),在您的spring应用程序中,它将返回"/spring“所有内容。我是通过实现com.opensymphony.module.sitemesh.DecoratorMapper接口并使用它来代替普通的ConfigDecoratorMapper来发现这一点的。然后,我能够检查用于将装饰者映射到请求的各种参数。不幸的是,我认为这给您留下了唯一的选择,就是在*.html映射中使用DispatcherServelet后缀或其中的某些变体。

另一个选项是配置一个PageDecoratorMapper,并在原始未修饰页面中使用这个标记来指定要使用的布局:

代码语言:javascript
复制
 <meta name="decorator" content="layoutName" /> 

尽管这样,您取消了url映射的好处。

票数 4
EN

Stack Overflow用户

发布于 2010-06-21 21:44:29

我确实遇到过这个问题。正在发生的情况是,您在web.xml中指定的url路径的任何部分在传递到Spring之前都会被web服务器删除,但前提是必须将通配符放在最后。您已经发现,当您的url是www.myapp.com/spring/cliente/index.html时,如果将它放在您的web.xml中

代码语言:javascript
复制
<servlet-mapping>
   <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
   <url-pattern>/spring/*</url-pattern>
</servlet-mapping>

Spring只会在/spring之后看到请求路径的部分。在这种情况下,需要将RequestMapping指定为"/cliente/index.html“。

您还可以以这种方式指定servlet映射。

代码语言:javascript
复制
<servlet-mapping>
   <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
   <url-pattern>*.html</url-pattern>
</servlet-mapping>

然后Spring将看到整个请求路径,您可以指定如下"/spring/cliente/index.html“这样的请求映射。Sitemesh也是如此。它只看到web服务器通过的内容。

票数 2
EN

Stack Overflow用户

发布于 2011-06-29 18:50:43

也许这对某些人是有用的,我也遇到了同样的问题,在谷歌和存储站点资源的研究之后,通过扩展ConfigDecoratorMapping来解决这个问题。这就是:

代码语言:javascript
复制
/**
 * 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包含这样的东西

代码语言:javascript
复制
<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators defaultdir="/web/decorators">
    <decorator name="admin_decorator" page="admin_decorator.jsp">
        <pattern>/springURITemplate/a/administration*</pattern>
    </decorator>
</decorators>

用Spring3.0.5在tomcat 7上进行测试

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2859014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档