SpringBoot 同時整合thymeleaf html、vue html和jsp-在線助手
問題描述
SpringBoot如何同時訪問html和jsp
SpringBoot訪問html頁面可以,訪問jsp頁面報錯
SpringBoot如何同時整合thymeleaf html、vue html和jsp
java web spring mvc項目如何同時訪問html和jsp
解決辦法:配置多視圖實現的視圖解析器
步驟一、新建一個ViewResolverConfiguration,下面是網站 在線助手 的具體實現:
/**
* 主要配置多視圖實現的視圖解析器相關bean實例
*
* http://www.it399.com/
*
* 其實關鍵點在于兩個:
* 1、配置order屬性
* 2、配置viewnames屬性
*
* 注意:
* return new ModelAndView("jsps/index");//或者return "jsps/index"
* 對應 /WEB-INF/jsps/index.jsp
* ==========================
* 同理:
* return "thymeleaf/index";//或者return “thymeleaf/index”
* 對應 /WEB-INF/thymeleaf/index.html
*
*
*/
@Configuration
public class ViewResolverConfiguration {
@Configuration//用來定義 DispatcherServlet 應用上下文中的 bean
@EnableWebMvc
@ComponentScan("com.csy.spring")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
// resolver.setPrefix("/WEB-INF/");
// resolver.setSuffix(".jsp");
// resolver.setViewNames("jsps/*");
resolver.setPrefix("/");
resolver.setSuffix(".jsp");
resolver.setViewNames("*");
resolver.setOrder(2);
return resolver;
}
@Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("utf-8");
templateResolver.setCacheable(false);
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
// templateEngine
return templateEngine;
}
@Bean
public ThymeleafViewResolver viewResolverThymeLeaf() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("utf-8");
viewResolver.setOrder(1);
//viewResolver.setViewNames(new String[]{"thyme/*"});
viewResolver.setViewNames(new String[]{"thymeleaf/*","vue/*"});
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
}
}
}
步驟二、測試類:
@Controller
public class IndexController {
@GetMapping("/testJsp")
public String testJsp(Model model) {
model.addAttribute("message", "this is index jsp page");
return "index";
}
@GetMapping("/testThemleaf")
public String testThemleaf(Model model) {
model.addAttribute("message", "this is index jsp page");
return "thymeleaf/test";
}
@GetMapping("/testVue")
public String testVue(Model model) {
model.addAttribute("message", "this is index jsp page");
return "vue/testVue";
}
}
測試結果:訪問 在線助手|在線工具|在線生成|在線制作,或者輸入以下鏈接
jsp頁面 http://www.it399.com/index.jsp
thymeleaf模板頁面 http://www.it399.com/blog/web/201805031726
vue搭建的頁面 http://www.it399.com/blog/web/Web201805041746
均正常顯示
這樣springboot就可以同時支持訪問jsp頁面和html頁面了。
本文同步發布在
在線助手|在線工具|在線生成|在線制作 ,轉載請注明來自 在線助手 博客頻道【SpringBoot 同時整合thymeleaf html、vue html和jsp】,原文鏈接 http://www.it399.com/blog/web/201805041746