Apache Tiles in Spring mvc project

2019-09-11 08:01发布

I am absolutely new to Tiles and novice in spring mvc. I want to add tiles view in my current configuration. Please help

So far I have done this, but not working:

@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;

}
}

My tiles.xml just basic setting

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

Layout.jsp copy pasted from tiles org

<%@ 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条回答
登录 后发表回答