Spring mvc项目中的Apache Tiles

时间:2015-03-16 14:52:07

标签: jsp spring-mvc tiles apache-tiles

我绝对是Tiles和spring mvc的新手。我想在当前配置中添加切片视图。请帮忙

到目前为止,我已经这样做了,但没有工作:

@EnableWebMvc
@Configuration
@ComponentScan({"com.kharoud"})
public class MvcConfiguraion extends WebMvcConfigurerAdapter{


private PerformanceMonitorInterceptor performanceInteceptor;

@Override
public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer ){      
    configurer.enable();
}   

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

    configurer.favorPathExtension(true);
    configurer.useJaf(false);
    configurer.ignoreAcceptHeader(false);
    configurer.mediaType("html", MediaType.TEXT_HTML);
    configurer.mediaType("json", MediaType.APPLICATION_JSON);
    configurer.defaultContentType(MediaType.TEXT_HTML); 

};

public void addInteceptors(InterceptorRegistry registory){
    registory.addInterceptor(performanceInteceptor);
//  registory.addInterceptor(localeChangeInterceptor());
}

@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource     reloadableResourceBundleMessageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:message_eng");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}


@Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager){

    List< ViewResolver > resolvers = new ArrayList< ViewResolver >();  

    InternalResourceViewResolver r1 = new InternalResourceViewResolver();
    r1.setPrefix("/WEB-INF/views/");
    r1.setSuffix(".jsp");
    r1.setViewClass(JstlView.class);
    resolvers.add(r1);

    JsonViewResolver r2 = new JsonViewResolver();
    resolvers.add(r2);

    TilesViewResolver r3 = new TilesViewResolver();
    resolvers.add(r3);          

    ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
    resolver.setViewResolvers(resolvers);
    resolver.setContentNegotiationManager(manager);
    return resolver;        
}

@Bean
public TilesConfigurer tilesConfigurer(){
    String[] definitions = new String[]{
            "WEB-INF/tiles/tiles.xml"
    };
    TilesConfigurer tilesConfigurer = new TilesConfigurer();
    tilesConfigurer.setDefinitions(definitions);
    return tilesConfigurer;

}
}

我的tiles.xml只是基本设置

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
   "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="myapp.homepage" template="/layout.jsp">
<put-attribute name="title" value="Tiles tutorial homepage" />
<put-attribute name="header" value="/header.jsp" />
<put-attribute name="menu" value="" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="" />
</definition>
</tiles-definitions>

从tiles org

粘贴的Layout.jsp副本
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<html>
<head>
<title><tiles:getAsString name="title"/></title>
</head>
<body>
    <table>
  <tr>
    <td colspan="2">
      <tiles:insertAttribute name="header" />
    </td>
  </tr>
  <tr>
    <td>
      <tiles:insertAttribute name="menu" />
    </td>
    <td>
      <tiles:insertAttribute name="body" />
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <tiles:insertAttribute name="footer" />
    </td>
  </tr>
 </table>
 </body>
 </html>

1 个答案:

答案 0 :(得分:0)