十、Springboot之thymeleaf与jsp共存
:
pom.xml添加依赖
<!--thymeleaf整合JSP需要用到下面的依赖-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</dependency>
此时项目结构如下:
ViewResolverConfiguration类代码:
package com.luzhanshi.springBootFrame.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;//此处及下面两行如果报错,就是”spring5“版本不对,你可以点击进去查看实际版本然后修改
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
/**
* 主要配置多视图实现的视图解析器相关bean实例,将该视图解析器注册到容器中
*
* 其实关键点在于两个:
* 1、配置order属性:视图解析器优先级问题(就是决定先使用哪个解析器来解析视图)
* 2、配置viewnames属性(配置对应的视图解析器解析哪些格式的视图)
*/
@Configuration
public class ViewResolverConfiguration {
@Configuration//用来定义 DispatcherServlet 应用上下文中的 bean
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
//jsp页面的视图解析器,解析到webapp下的jsp/目录下查找对应的jsp页面
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp");
resolver.setSuffix(".jsp");
resolver.setViewNames("*");//这里是设置该视图解析器所要匹配哪些格式的视图“*”代表匹配所有格式“
resolver.setOrder(2);//优先级,Spring配置多个视图解析器,数字越小,优先级越高,越先匹配
return resolver;
}
/**
* 下面都是配置hymeleaf的视图解析器相关的内容
* 对thymeleaf的视图解析器,解析到webapp下的html目录下查找对应的页面
* @return
*/
@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());
// templateEngine
return templateEngine;
}
@Bean
public ThymeleafViewResolver viewResolverThymeLeaf() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("utf-8");
viewResolver.setOrder(1);//设置该视图解析器优先级为1
//下面是设置该视图解析器所要匹配哪些格式的视图"html/*", "vue/*","jsps/*","templates/*"代表匹配"html/*", "vue/*","jsps/*","templates/*"格式的所有视图
viewResolver.setViewNames(new String[]{"html/*", "vue/*","jsps/*","templates/*"});
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
} /**
* 配置资源路径
* @param registry
*/
// @Override
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
// registry.addResourceHandler("/img/**").addResourceLocations("/img/");
// registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/" + "/static/");
// }
} }
properties文件thyemleaf配置的内容变化为:
# 模版存放路径
#spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.prefix=/WEB-INF/html
html文件以及jsp文件的视图路径为:
:
:
最后访问效果:
十、Springboot之thymeleaf与jsp共存的更多相关文章
- springboot同时使用thymeleaf和jsp模板
语言:javaEE 框架:springboot+thymeleaf.jsp模板引擎 背景:学习springboot过程中想同时使用thymeleaf和jsp访问(官方不建议) 步骤: 1) 在pom ...
- SpringBoot第十二篇:整合jsp
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10953600.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 Sprin ...
- 从.Net到Java学习第六篇——SpringBoot+mongodb&Thymeleaf&模型验证
SpringBoot系列目录 SpringBoot整合mongodb MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.如果你没用过Mong ...
- springboot-10-前端页面整合, thymeleaf, freemarker, jsp 模板使用
springboot 中不建议使用jsp作为页面展示, 怎么使用可以看: http://412887952-qq-com.iteye.com/blog/2292471 关于什么是thymeleaf, ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- 从.Net到Java学习第九篇——SpringBoot下Thymeleaf
从.Net到Java学习系列目录 Thymeleaf概述 Thymeleaf 是一个流行的模板引擎,该模板引擎采用java语言开发.模板引擎是一个技术名称,是跨领域平台的概念,在java语言体系下有模 ...
- SpringBoot 之Thymeleaf模板.
一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...
- springBoot整合mybatis、jsp 或 HTML
springBoot整合mybatis.jsp Spring Boot的主要优点: 1: 为所有Spring开发者更快的入门: 2: 开箱即用,提供各种默认配置来简化项目配置: 3: 内嵌式容器 ...
- IDEA上创建 Maven SpringBoot+mybatisplus+thymeleaf 项目
概述 在WEB领域,Java也是在不断的探索和改进,从开始的JSP--->Struts1--->Struts2+Spring--->Spring MVC--->SpringBo ...
随机推荐
- SQL 基础语句整理
SQL教程 SELECT 语句 SELECT * FROM 表名称 DISTINCT 语句 SELECT DISTINCT 列名称 FROM 表名称 SELECT LastName,FirstName ...
- IE6兼容笔记
1.IE6中,元素右浮动的时候前面不能有文本或内联元素,否则会换行独占一行 解决办法:将浮动元素放到文本或内联元素前面,大都在制作新闻列表的时候会遇到这种问题. 未完,待续!
- 第98:svd原理
SVD分解:任何矩阵都可以分解成第一行的形式,3个相乘.UV都是正交矩阵,中间的是奇异值. 3个相乘的形式可以拆分.即奇异值*第一行*第一列.在相加. 奇异值有时很小,在这种情况下,丢掉,可以减少计算 ...
- [PyQt5]动态显示matplotlib作图(一)
完整实例 import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePoli ...
- Qualcomm_Mobile_OpenCL.pdf 翻译-2
2 Opencl的简介 这一章主要讨论Opencl标准中的关键概念和在手机平台上开发Opencl程序的基础知识.如果想知道关于Opencl更详细的知识,请查阅参考文献中的<The OpenCL ...
- 对OO原则的个人理解
1.单一职责原则.(Single Responsibility Principle) 注解:社会化大生产分工要细.具体每个人最好只做一件事(不要一人兼多职),这样如果这个人请假或辞职,对生产不会产生影 ...
- 隐马尔可夫模型中基于比例因子的前向算法(java实现)
直接上干货哈,其他子算法,后续补上. System.out.print ...
- vue项目中利用popstate处理页面返回操作
需求背景:项目中需要做一个返回确认,避免用户误触返回键而退出当前页面. 原理:利用history和浏览器刷新popstate状态 实现: 1.在mounted() 阶段判断并添加popstate事件监 ...
- web.xml中url-pattern中/和/*的区别(来自网络)
其中/和/*的区别: < url-pattern > / </ url-pattern > 不会匹配到*.jsp,即:*.jsp不会进入spring的 Dispatcher ...
- <el-tree>文字显示不全解决办法(可以用省略号代替)
地址: https://www.jianshu.com/p/229f96b794d3