1

我有 Spring MVC 项目,我想使用 Apache Tiles 在我的应用程序中构建视图。我在我的pom.xml文件中添加了一些依赖项。我想知道我有太多但我有一些关于缺少类的例外,所以我在一些包中找到了它们并将它们添加到类路径中。

<!-- Tiles -->
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>2.2.2</version>
    </dependency>         
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-servlet</artifactId>
        <version>2.1.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-digester3</artifactId>
      <version>3.2</version>
      <classifier>with-deps</classifier>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.8.3</version>
    </dependency>

这是我ViewResolvers在 Dispatcher Servlet 上下文中的配置。我想知道如何将它们正确配置在一起。

<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/tiles-definitions.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean> 

<beans:bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
    <beans:property name="order" value="0" />
</beans:bean>

<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
    <beans:property name="order" value="1" />
</beans:bean>

我已阅读此问答:如何在 Spring 中使用多个 ViewResolver?但这对我没有帮助。目前,上述配置jsp视图可以正常工作,但对于我得到的瓷砖地址HTTP Status 404

编辑

这是我的控制器。与“家”相关的视图正在工作,但其他视图 - 瓷砖不工作。

 @RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {        
    return "home";
}

@RequestMapping(value = "/pets", method = RequestMethod.GET)
public String pets(Locale locale, Model model) {
    return "pets-tiles";
}   

@RequestMapping(value = "/dogs", method = RequestMethod.GET)
public String dogs(Locale locale, Model model) {
    return "dogs-tiles";
}   

@RequestMapping(value = "/cats", method = RequestMethod.GET)
public String cats(Locale locale, Model model) {
    return "cats-tiles";
}   

这是我的tiles-definitions.xml文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
 <tiles-definitions>

  <definition name="template-main" template="/WEB-INF/views/layouts/main.jsp">
   <put-attribute name="banner-content" value="/WEB-INF/views/sections/banner.jsp" />
   <put-attribute name="title-content" value="Pet Type" />
   <put-attribute name="primary-content" value="" />
   <put-attribute name="footer-content" value="/WEB-INF/views/sections/footer.jsp" />
  </definition>

  <definition name="template-detail" template="/WEB-INF/views/layouts/detail.jsp">
   <put-attribute name="banner-content" value="/WEB-INF/views/sections/banner.jsp" />
   <put-attribute name="title-content" value="Pet Type" />
   <put-attribute name="subtitle-content" value="" />
   <put-attribute name="primary-content" value="" />
   <put-attribute name="footer-content" value="/WEB-INF/views/sections/footer.jsp" />
  </definition>

  <definition name="pet-tiles" extends="template-main">
   <put-attribute name="primary-content" value="/WEB-INF/views/contents/pets.jsp" />
  </definition>

  <definition name="dog-tiles" extends="template-detail">
   <put-attribute name="subtitle-content" value="Canines" />
   <put-attribute name="primary-content" value="/WEB-INF/views/contents/dogs.jsp" />
  </definition>

  <definition name="cat-tiles" extends="template-detail">
   <put-attribute name="subtitle-content" value="Felines" />
   <put-attribute name="primary-content" value="/WEB-INF/views/contents/cats.jsp" />
  </definition>

 </tiles-definitions>

它们来自本教程: http: //krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html但不适用于我。

4

1 回答 1

1

我已经解决了我的问题。我不知道为什么它不起作用,但此刻一切似乎都很好。

AutoTilesView从这个 url 找到并实现了类:http: //forum.springsource.org/showthread.php?78312-Spring-MVC-Noob-help-with- site-templates

为什么默认设置TilesView不起作用?AutoTilesView类看起来像:

public class AutoTilesView extends TilesView {

private static final String PREFIX = "/WEB-INF/views/";
private static final String SUFFIX = ".jsp";

@Override
protected void renderMergedOutputModel(Map<String, Object> model,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    ServletContext servletContext = getServletContext();
    TilesContainer container = ServletUtil.getContainer(servletContext);

    if (container == null) {
        throw new ServletException("Tiles container is not initialized. " +
                "Have you added a TilesConfigurer to your web application context?");
    }

    AttributeContext attributeContext = container.getAttributeContext(request, response);

    StringBuilder builder = new StringBuilder();
    builder.append(PREFIX);
    builder.append(this.getUrl());
    builder.append(SUFFIX);     

    attributeContext.putAttribute("body", Attribute.createTemplateAttribute(builder.toString()));

    super.renderMergedOutputModel(model, request, response);
}
}

这是来自我的 Serlvet Dispatcher Context 的代码:

<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/tiles-definitions.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean> 

<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <beans:property name="viewClass" value="cos.tam.AutoTilesView" />
</beans:bean>

我已经删除了org.springframework.web.servlet.view.InternalResourceViewResolver

于 2012-07-19T13:54:20.620 回答