Integrate Thymeleaf, Tiles and Jsp view resolver t

2019-08-06 15:32发布

I am using Thymeleaf, Apache Tiles and Jsp view resolver together in my Spring-MVC project. My Tiles and JSP view resolver work fine, but the problem in Thymeleaf, the resolver is not work, Instead of thymeleaf the JSP resolver call and resolve the view, so that, i get an error. Following is my code:

view-resolver.properties

#tiles properties
tiles.cache = false
tiles.cache.limit = 1024
tiles.order = 0

#thymeleaf
thm.cache = false
thm.cache.limit = 1024
thm.order = 1

#url based properties
url.cache = false
url.cache.limit = 1024
url.order = 2

My configuration:

@Configuration
@PropertySource(value={"classpath:properties/view-resolver.properties"})
public class MultiViewResolverConfig{

@Autowired
private Environment env;

/** 
 * This method is used to create tiles view resolver which have 
 * view resolver first priority for resolve response view
 * */
private TilesViewResolver tilesViewResolver() {
    TilesViewResolver resolver = new TilesViewResolver();
    resolver.clearCache();
    resolver.setCache(Boolean.parseBoolean(env.getProperty("tiles.cache")));
    resolver.setCacheLimit(Integer.parseInt(env.getProperty("tiles.cache.limit")));
    resolver.setOrder(Integer.parseInt(env.getProperty("tiles.order")));
    return resolver;
}

/** 
 * This method is used to configure tiles for application and 
 * specify which definitions files are used. 
 * This method create a bean and register. 
 * */
@Bean
public TilesConfigurer tilesConfigure(){
    TilesConfigurer configurer = new TilesConfigurer();
    configurer.setDefinitions(new String[]{"classpath:tiles-definitions/user-definitions.xml", 
            "classpath:tiles-definitions/public-definitions.xml", "classpath:tiles-definitions/admin-definitions.xml"});
    configurer.setValidateDefinitions(true);
    configurer.setCheckRefresh(true);
    return configurer;
}

/** 
 * This method is used for configure the thymeleaf view configuration.
 * */
private ServletContextTemplateResolver templateResolver() {
    //SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode("HTML5");
    resolver.setCharacterEncoding("UTF8");
    resolver.setCacheable(Boolean.parseBoolean(env.getProperty("thm.cache")));
    resolver.setCacheTTLMs(Long.parseLong(env.getProperty("thm.cache.limit")));
    resolver.setOrder(Integer.parseInt(env.getProperty("thm.order")));
    return resolver;
}

/** 
 * This method is used to configure template engine for thymeleaf.
 * */
private SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    return templateEngine;
}

/** 
 * This method is used for configure thymeleaf view resolver.
 * This view resolver have second priority.
 * */
private ThymeleafViewResolver thymeleafViewResolver() {
    ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
    thymeleafViewResolver.setViewClass(ThymeleafView.class);
    thymeleafViewResolver.setTemplateEngine(templateEngine());
    return thymeleafViewResolver;
}

/** 
 * This method is used to create simple jsp view resolver
 * which have third priority for resolve response views
 * */
private UrlBasedViewResolver urlBasedViewResolver(){ 
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    resolver.setCache(Boolean.parseBoolean(env.getProperty("url.cache")));
    resolver.setCacheLimit(Integer.parseInt(env.getProperty("url.cache.limit")));
    resolver.setOrder(Integer.parseInt(env.getProperty("url.order")));
    return resolver;
}

/** 
 * This method is used setup multiple view resolver for applications.
 * This is a bean register to application.
 * */
@Bean
public ViewResolver setupViewResolver(ContentNegotiationManager manager){
    List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();

    viewResolvers.add(tilesViewResolver());
    viewResolvers.add(thymeleafViewResolver());
    viewResolvers.add(urlBasedViewResolver());
    ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
    resolver.setViewResolvers(viewResolvers);
    resolver.setContentNegotiationManager(manager);
    return resolver;
}
}

My controller:

@RequestMapping(value="/dashboard", method=RequestMethod.GET)
public String adminDashboard() {
    logger.info("ADMIN Dashboard ******************************* ");

    return "admin/html/template";
}

UPDATE: Logs

Following are the logs of my view rendring:

org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO [Line-No:154] - File Resource jar:file:/D:/luna_workspace/flyitin/.metadata/.plugins/     org.eclipse.wst.server.core/tmp0/wtpwebapps/flyitin-web/WEB-INF/lib/flyitin-common-0.0.1-SNAPSHOT.jar!/tiles-definitions/user-definitions_en.xml at jar:file:/D:/luna_workspace/flyitin/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/flyitin-web/WEB-INF/lib/flyitin-common-0.0.1-SNAPSHOT.jar!/tiles-definitions/user-definitions_en.xml not found, continue
 org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO [Line-No:154] - File Resource jar:file:/D:/luna_workspace/flyitin/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/flyitin-web/WEB-INF/lib/flyitin-common-0.0.1-SNAPSHOT.jar!/tiles-definitions/public-definitions_en.xml at jar:file:/D:/luna_workspace/flyitin/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/flyitin-web/WEB-INF/lib/flyitin-common-0.0.1-SNAPSHOT.jar!/tiles-definitions/public-definitions_en.xml not found, continue
 org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO [Line-No:154] - File Resource jar:file:/D:/luna_workspace/flyitin/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/flyitin-web/WEB-INF/lib/flyitin-common-0.0.1-SNAPSHOT.jar!/tiles-definitions/admin-definitions_en.xml at jar:file:/D:/luna_workspace/flyitin/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/flyitin-web/WEB-INF/lib/flyitin-common-0.0.1-SNAPSHOT.jar!/tiles-definitions/admin-definitions_en.xml not found, continue
 o.a.tiles.definition.dao.ResolvingLocaleUrlDefinitionDAO [Line-No:132] - Resolve definition for child name='user-dashboard' extends='user-template.
 o.a.tiles.definition.dao.ResolvingLocaleUrlDefinitionDAO [Line-No:132] - Resolve definition for child name='public-dashboard' extends='public-template.
 o.a.tiles.definition.dao.ResolvingLocaleUrlDefinitionDAO [Line-No:132] - Resolve definition for child name='admin-dashboard' extends='admin-template.
 o.s.beans.factory.support.DefaultListableBeanFactory [Line-No:1610] - Invoking afterPropertiesSet() on bean with name 'th-template'
 o.s.beans.factory.support.DefaultListableBeanFactory [Line-No:247] - Returning cached instance of singleton bean 'metaDataSourceAdvisor'
 org.springframework.web.servlet.DispatcherServlet [Line-No:1225] - Rendering view [org.springframework.web.servlet.view.JstlView: name 'th-template'; URL [/WEB-INF/views/th-template.jsp]] in DispatcherServlet with name 'dispatcher'
 o.s.beans.factory.support.DefaultListableBeanFactory [Line-No:247] - Returning cached instance of singleton bean 'requestDataValueProcessor'
  org.springframework.web.servlet.view.JstlView [Line-No:170] - Forwarding to resource [/WEB-INF/views/th-template.jsp] in InternalResourceView 'th-template'
 org.springframework.web.servlet.DispatcherServlet [Line-No:996] - Successfully completed request
 o.s.security.web.access.ExceptionTranslationFilter [Line-No:115] - Chain processed normally
  o.s.security.web.context.SecurityContextPersistenceFilter [Line-No:97] - SecurityContextHolder now cleared, as request processing completed

According to the logs, the thymeleaf is not in view resolving picture. When i disable the JSP view, the thymeleaf work fine.

0条回答
登录 后发表回答