背景:原系统是由springboot jsp,所有访问都是jsp
现在需要做HTML5定位,要同时支持访问HTML5和JSP
在application.yml的spring标签下配置
mvc: #view: # 页面默认前缀目录 prefix: /WEB-INF/jsp/ # 响应页面默认后缀 suffix: .jsp # # 配置JSP视图名 view-names: '*' thymeleaf: cache: false prefix: /WEB-INF/html/ suffix: .html mode: HTML5 encoding: UTF-8 view-names: html/*
发现访问jsp正常,html总是报500
看来还是没配置对
改成添加 配置文件,在配置文件中配置:
@Configuration public class ViewResolverConfiguration { @Configuration//用来定义 DispatcherServlet 应用上下文中的 bean @EnableWebMvc @ComponentScan("com.*.**") public class WebConfig implements WebMvcConfigurer { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); resolver.setViewNames("*"); resolver.setOrder(2); return resolver; } @Bean public ITemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setTemplateMode("HTML5"); templateResolver.setPrefix("/WEB-INF/"); templateResolver.setSuffix(".html"); templateResolver.setCharacterEncoding("utf-8"); templateResolver.setCacheable(false); return templateResolver; } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); return templateEngine; } @Bean public ThymeleafViewResolver viewResolverThymeLeaf() { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine()); viewResolver.setCharacterEncoding("utf-8"); viewResolver.setOrder(1); viewResolver.setViewNames(new String[]{"html/*"}); return viewResolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } } }
pom.xml中添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
大部分中都是访问jsp的
在jsp页面访问后台controller里返回页面
return "/a/b/c"; 这种绝对地址开头;
在html页面访问后台controll里返回页面
return "html/location"; 不以"/"开头;
jsp和html路径
WEB-INF/jsp/***
WEB-INF/html/***
到此:两种访问都没问题
但引发一个问题:对于jsp对应的Controller当返回json内容里,方法有注解@ResponseBody,返回到前台中文会乱码成????
解决方案:
在上面类ViewResolverConfiguration 中类WebConfig 中添加
@Bean public HttpMessageConverter<String> responseBodyStringConverter() { StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8); return converter; } @Bean public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper); //设置中文编码格式 List<MediaType> list = new ArrayList<MediaType>(); list.add(MediaType.APPLICATION_JSON_UTF8); mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list); return mappingJackson2HttpMessageConverter; } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters){ //解决中文乱码 converters.add(responseBodyStringConverter()); //解决 添加解决中文乱码后 上述配置之后,返回json数据直接报错 500:no convertter for return value of type converters.add(getMappingJackson2HttpMessageConverter()); }
这样,两种访问都可以
注意:这里顺序
jsp resolver.setOrder(2);
html viewResolver.setOrder(1);
如果换成1,2,则html不能访问
order值越小,优先级越高,不知道为啥不行呢?