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例項
*
*
*
* 其實關鍵點在於兩個:
* 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頁面
thymeleaf模板頁面 blog/web/201805031726vue搭建的頁面 blog/web/Web201805041746
均正常顯示
這樣springboot就可以同時支援訪問jsp頁面和html頁面了。
本文同步釋出在 ,轉載請註明來自 部落格頻道【SpringBoot 同時整合thymeleaf html、vue html和jsp】,原文連結 blog/web/201805041746
SpringBoot如何同時訪問html和jsp
SpringBoot訪問html頁面可以,訪問jsp頁面報錯
SpringBoot如何同時整合thymeleaf html、vue html和jsp
java web spring mvc專案如何同時訪問html和jsp
解決辦法:配置多檢視實現的檢視解析器
步驟一、新建一個ViewResolverConfiguration,下面是網站 的具體實現:
/**
* 主要配置多檢視實現的檢視解析器相關bean例項
*
*
*
* 其實關鍵點在於兩個:
* 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頁面
thymeleaf模板頁面 blog/web/201805031726vue搭建的頁面 blog/web/Web201805041746
均正常顯示
這樣springboot就可以同時支援訪問jsp頁面和html頁面了。
本文同步釋出在 ,轉載請註明來自 部落格頻道【SpringBoot 同時整合thymeleaf html、vue html和jsp】,原文連結 blog/web/201805041746
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28470105/viewspace-2154966/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [thymeleaf]springboot整合thymeleaf, html使用預置方法Spring BootHTML
- springBoot整合thymeleafSpring Boot
- SpringBoot整合Jsp和Thymeleaf (附工程)Spring BootJS
- jsp和html同時存在的個人理解JSHTML
- SpringBoot 實戰 (十二) | 整合 thymeleafSpring Boot
- SpringBoot、MyBatis、Shiro、Thymeleaf整合思路Spring BootMyBatis
- Vue SpringBoot實現Html和Markdown格式內容(含圖片上傳)儲存到MySQLVueSpring BootHTMLMySql
- Yii2-助手類(Html)HTML
- SpringBoot系列(六)整合thymeleaf詳解版Spring Boot
- SpringBoot之整合thymeleaf渲染Web頁面Spring BootWeb
- HTML知識整合HTML
- 【vue】v-htmlVueHTML
- 1 HTML(上)HTML
- Spring Boot中Thymeleaf和htmx助手工具庫Spring Boot
- 不要再學 JSP 了,學 SpringBoot + Thymeleaf + Vue吧JSSpring BootVue
- Vue指令——v-htmlVueHTML
- HTML <sup> 上標HTML
- 10個線上HTML程式碼編輯器HTML
- HTML和CSSHTMLCSS
- html標籤-HTML5精講 課時ID:6.1 【表嚴肅】#HTML教程 #HTML5教程 #html標籤HTML
- vue.js輸出HTML(v-pre | v-html)Vue.jsHTML
- CSS之CSS和html整合方式及優先順序CSSHTML
- vue2.0和vue3.0同時使用Vue
- 如何用猿大師辦公助手實現多人同時線上編輯Office Word文件?
- Vue學習04-----v-bloak和v-htmlVueHTML
- 同時連線gitlab和githubGitlabGithub
- springboot無妨訪問html頁面Spring BootHTML
- html中線上預覽pdf檔案之pdf線上預覽外掛HTML
- 【深入吧,HTML 5】 效能 & 整合 —— Web WorkersHTMLWeb
- 【深入吧,HTML 5】 效能 & 整合 —— History APIHTMLAPI
- 谷歌開發者工具線上編輯HTML程式碼谷歌HTML
- 動態生成HTML元素-模擬線上考試功能HTML
- SpringBoot3整合SpringDoc實現線上介面文件Spring Boot
- Python HTML和CSS 1:html文件結構和常用標籤PythonHTMLCSS
- springboot模版thymeleaf+freemarkerSpring Boot
- 如何讓html引用公共佈局(多個html檔案公用一個header.html和footer.html)HTMLHeader
- html和html5有什麼區別呢?HTML
- HTML <hr> 水平線標籤HTML