Spring的MVC集成在一起Thymeleaf,瓷砖和Jsp视图解析器(Integrate Thy

2019-10-21 20:43发布

我使用ThymeleafApache TilesJsp在我的春MVC项目视图解析器一起。 我的TilesJSP视图解析器做工精细,但在这个问题Thymeleaf ,解析器是不行的,而不是thymeleafJSP解析器调用和解决的观点,这样,我得到一个错误。 以下是我的代码:

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

我的配置:

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

我的控制器:

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

    return "admin/html/template";
}

更新:日志

以下是我的看法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

根据日志,该thymeleaf考虑不解决图片。 当我关闭JSP来看,thymeleaf做工精细。

文章来源: Integrate Thymeleaf, Tiles and Jsp view resolver together in Spring-MVC