同时包含HTML和JSP ViewResolvers

时间:2015-06-23 13:36:36

标签: java jsp spring-mvc

我正在尝试在我的应用程序中使用Thymeleaf的ServletContextTemplateResolver以及InternalViewResolver来解析我的控制器中返回的htmljsp个视图。我已经将模板解析器的顺序设置为0,因为具有最高顺序的解析器是链中的最后一个解析器:

    /** Thymeleaf HTML Template View Resolver **/
    @Bean
    public ServletContextTemplateResolver getServletContextTemplateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/html/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setOrder(0);
        return templateResolver;

    }
    /** JSP View Resolver **/
    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setOrder(1);
        return resolver;
    }

但是我的观点总是被解析为html。是否还有一个我可以包含的逻辑(或者可能没有正确配置解析器?)或传递给我的ModelAndView对象以确保我的jsp视图映射到内部视图解析器并且html vies映射到我的模板解析器?即ModelAndView mav = new ModelAndView("mypage.html"); //之类的东西当然可以解析为mypage.html.jsp。

2 个答案:

答案 0 :(得分:1)

对于ServletContextTemplateResolver(即TemplateResolver的impl),您可以为应该忽略的文件指定“模式”。

尝试此方法:see here for any other applicable method

public void setResolvablePatterns(Set<String> resolvablePatterns)

<强>更新

看起来与Spring App一起使用的ThymeLeaf Resolver完全不同:org.thymeleaf.spring4.view.ThymeleafViewResolver

请参阅此处定义的bean: http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#views-and-view-resolvers-in-spring-mvc

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
  <property name="templateEngine" ref="templateEngine" />
  <property name="order" value="1" />
  <property name="viewNames" value="*.html,*.xhtml" />
</bean>

答案 1 :(得分:1)

这是我解决HTML和JSP的工作配置。您可以根据需要为Javaconfig进行调整。

  <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="order" value="1" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="templateEngine" ref="templateEngine" />
  </bean>

  <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolvers">
      <set>
    <ref bean="webTemplateResolver" />
    <ref bean="emailTemplateResolver" />
      </set>
    </property>
    <property name="additionalDialects">
      <set>
        <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
      </set>
    </property>
  </bean>

  <bean id="webTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/thymeleaf/" />
    <property name="templateMode" value="HTML5" />
    <property name="suffix" value=".html" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="cacheable" value="false" />
    <property name="order" value="3" />
  </bean>

  <bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
    <property name="prefix" value="templates/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="cacheable" value="false"/>
    <property name="order" value="2" />
  </bean>

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

  <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
      <list>
    <value>/WEB-INF/**/views.xml</value>
      </list>
    </property>
  </bean>

views.xml包含JSP文件的所有定义。