Sitemesh代替整个视图弹簧

时间:2015-08-13 03:25:20

标签: java spring-mvc sitemesh

我在我的spring mvc项目中使用了sitemess。请先查看我的代码。

decorator.xml     

<decorator name="main-layout" page="main-layout.jsp">
    <pattern>/products</pattern>
    <pattern>/welcome</pattern>
    <pattern>/admin</pattern>
    <pattern>/ajax</pattern>
</decorator>

的web.xml

<!-- Spring MVC -->
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

    <!-- Loads Spring Security config file -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/spring-security.xml
    </param-value>
</context-param>

<!-- 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>

<!--  Sitemesh: Decorates pages with layouts  -->
<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>
</filter-mapping>

在我的主要布局中,我只导入一个导航栏。问题是它只显示我网站的每个视图的主要布局(仅显示导航栏)。

1 个答案:

答案 0 :(得分:0)

您只定义了一个装饰器,因此逻辑上只对每个页面应用该布局,您需要在decorator.xml中定义其他布局

通常这是我的decorator.xml的样子

 <decorator name="main" page="outer.jsp">
        <pattern>*/public/login*</pattern>
</decorator>
        
<decorator name="default" page="default.jsp">
        <pattern>*app*</pattern>
</decorator>

还要确保每个布局文件中都有<decorator:body/>,这是占位符,其中显示内页的内容。

像这样的东西

<body>
   <nav></nav>
   <aside></aside>
   <decorator:body/>
</body>