我正在使用spring-boot-starter-thymeleaf使用spring boot 2.0.1.RELEASE。我有两个版本的PC和手机版本。
这是我的项目结构的简单版本 project structure
我希望该网站能够自动检测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]")
答案 0 :(得分:0)
您正在尝试配置已经configured for you的内容。
spring-boot-starter-thymeleaf
并且您的依赖项列表中包含spring-mobile-autoconfguration
。 注意:我假设您正在使用快照构建,以便与Spring和Spring Boot版本兼容。
Config
班级将以下内容添加到application.properties
spring.mobile.devicedelegatingviewresolver.normalPrefix=web/
spring.mobile.devicedelegatingviewresolver.tabletPrefix=web/
spring.thymeleaf