Spring boot thymeleaf 不适用于 LiteDeviceDelegatingViewResolver



我正在使用Spring boot 2.0.1.RELEASE,使用spring-boot-starter-thymeleaf。我有两个版本的PC和手机页面。

这是我的项目结构的简单版本 项目结构

我希望网站自动检测PC浏览器和手机浏览器,以便它可以根据浏览器的类型将相同的请求URL映射到不同的页面

html 文件非常简单,如下所示。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>This is from PC web browser</p>
</body>
</html>

这是我的控制器的代码。

@Controller
public class MyController {
@RequestMapping("/")
public String mainDefault(){
return "home";
}
@RequestMapping("/home")
public String main(){
return "home";
}
}

为了检测设备,我编写了以下配置类

@Configuration
public class Config implements WebMvcConfigurer {
@Autowired
private ApplicationContext applicationContext;
@Bean
public LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(viewResolver);
resolver.setNormalPrefix("web/");
resolver.setMobilePrefix("mobile/");
resolver.setTabletPrefix("web/");
resolver.setOrder(1);
resolver.setEnableFallback(true);
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new LayoutDialect());
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.applicationContext);
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(".HTML5");
templateResolver.setCacheable(false);
return templateResolver;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor = new DeviceResolverHandlerInterceptor();
SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor = new SitePreferenceHandlerInterceptor();
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
registry.addInterceptor(deviceResolverHandlerInterceptor);
registry.addInterceptor(sitePreferenceHandlerInterceptor);
WebMvcConfigurer.super.addInterceptors(registry);
}
}

然后当我尝试运行应用程序时。我总是收到以下错误。似乎它可以映射到正确的资源,即 [/templates/web/home.html]。但它总是说无法打开 ServletContext 资源 [/templates/web/home.html]

如果我从移动浏览器访问,它会映射到 [/templates/mobile/home.html],这也是正确的。

谁能帮我?提前谢谢你。

2018-04-19 17:18:57.040 ERROR 17732 --- [nio-9020-exec-9] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-9020-exec-9] Exception processing template "web/home": An error happened during template parsing (template: "ServletContext resource [/templates/web/home.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/templates/web/home.html]")
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:235) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]

.........

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/templates/web/home.html]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:159) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.thymeleaf.spring5.templateresource.SpringResourceTemplateResource.reader(SpringResourceTemplateResource.java:103) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
... 52 common frames omitted
2018-04-19 17:18:57.047 DEBUG 17732 --- [nio-9020-exec-9] o.s.web.servlet.DispatcherServlet        : Error rendering view [org.thymeleaf.spring5.view.ThymeleafView@139a953a] in DispatcherServlet with name 'dispatcherServlet'
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/templates/web/home.html]")

您正在尝试配置已为您配置的内容。

  1. 确保你具有spring-boot-starter-thymeleaf,并且依赖项列表中有spring-mobile-autoconfguration

注意:我假设您使用的是快照版本,以便与 Spring 和 Spring 引导版本兼容。

  1. 删除Config
  2. 将以下内容添加到您的application.properties

    spring.mobile.devicedelegatingviewresolver.normalPrefix=web/
    spring.mobile.devicedelegatingviewresolver.tabletPrefix=web/
    spring.thymeleaf
    
  3. 重新启动应用程序

相关内容

  • 没有找到相关文章

最新更新