springboot同时使用thymeleaf和jsp模板
语言:javaEE
框架:springboot+thymeleaf、jsp模板引擎
背景:学习springboot过程中想同时使用thymeleaf和jsp访问(官方不建议)
步骤:
1) 在pom.xml中加入依赖
jsp:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
thymeleaf:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2) 创建视图解析器
方法:新建ViewResolverConfiguration类
@Configuration
public class ViewResolverConfiguration{
@Configuration//用来定义 DispatcherServlet 应用上下文中的 bean
@EnableWebMvc
@ComponentScan("cn.test.book_j2ee")//扫描控制器组件
public class WebConfig extends WebMvcConfigurerAdapter/*这个适配器已过时*/{
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".jsp");
resolver.setViewNames("jsp/*");//当控制器返回的viewName符合规则时才使用这个视图解析器
resolver.setOrder();//设置优先级,数值越小优先级越高
return resolver;
}
}
}
3) 编写控制器
@Controller
public class indexController{
@RequestMapping("example")
public String a(){
return "example";//返回classpath:templates/example.html页面(thymeleaf)
}
@RequestMapping("index")
public String b(){
return "jsp/index";//返回 /WEB-INF/jsp/index.jsp 页面(jsp)
}
}
注:此时thymeleaf的访问路径还是默认的 classpath:templates/ ,后缀为 .html 。如需修改,请到 application.properties 配置。还可以修改viewName
参考博文:
https://blog.csdn.net/weixin_41063560/article/details/80313327
https://blog.csdn.net/e_Inch_Photo/article/details/80201272
https://blog.csdn.net/qq_19408473/article/details/71214972(使用配置文件来配置视图解析器)
springboot同时使用thymeleaf和jsp模板的更多相关文章
- SpringBoot系列之集成jsp模板引擎
目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...
- 十、Springboot之thymeleaf与jsp共存
: pom.xml添加依赖 <!--thymeleaf整合JSP需要用到下面的依赖--> <dependency> <groupId>org.thymeleaf&l ...
- springboot-10-前端页面整合, thymeleaf, freemarker, jsp 模板使用
springboot 中不建议使用jsp作为页面展示, 怎么使用可以看: http://412887952-qq-com.iteye.com/blog/2292471 关于什么是thymeleaf, ...
- springboot中Thymeleaf和Freemarker模板引擎的区别
前言这两个都是属于模板引擎,但是各有各的好处,enn,在市面上比较多的也就是jsp.freemarker.velocity.thymeleaf等页面方案.Thymeleaf和Freemarker的区别 ...
- (二)springboot整合thymeleaf模板
在我们平时的开发中,用了很久的jsp作view显示层,但是标签库和JSP缺乏良好格式的一个副作用就是它很少能够与其产生的HTML类似.所以,在Web浏览器或HTML编辑器中查看未经渲染的JSP模板是非 ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- SpringBoot 之Thymeleaf模板.
一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
随机推荐
- 137. Single Number II (Bit)
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 二次注入的学习--Buy Flag(http://10.112.68.215:10002)
这次有做一个二次注入的天枢CTF题目,算是完整地理解了一遍注入的知识.来,启航. 1.判断注入点 经过对题目的实践分析,知道注册时需要输入年龄大于18岁,但在登录后界面,年龄因为太大不能接 ...
- AD操作
加泪滴 批量添加覆铜过孔(先铺铜以后,再批量添加过孔) 开槽 在KEPP—OUT层 部分区域 不敷铜 开窗
- Django2.0的path方法无法使用正则表达式的解决办法
本人的django的版本是2.0.6 在django项目中,在urls.py文件中,匹配路由用的path方法,之前我用的都是url方法 写了一个path方法,使用正则表达式 from django.c ...
- selenium验证码和错误截图
验证码的识别: 1,破解验证码 OCR识别(一般使用tesseract-ocr) 人工智能(AI机器学习 TensorFlow,成本大) 2,绕过验证码 1, 让开发人员临时关闭验证码 2,提供万能验 ...
- C++ 计算直线的交点数(动态规划)
问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1466 Problem Description 平面上有n条直线,且无三线共点,问这些直线能有多少种不同 ...
- linux命令学习之:read
read命令从键盘读取变量的值,通常用在shell脚本中与用户进行交互的场合.该命令可以一次读取多个变量的值,变量和输入的值都需要使用空格隔开.在read命令后面,如果没有指定变量名,读取的数据将被自 ...
- centos7下haproxy1.7的使用与配置
centos7下haproxy1.7的使用与配置 haproxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理. 一.haproxy下载 ...
- Excel怎么下拉框多选
打开Exlce, 确定,然后 右击查看代码,把这段代码复制到新建的文件里面 此时Excel会给出提示,选择否,,系统会提示保存,在保存的时候选择启用宏的工作簿然后保存,此时Excel下拉框多选就搞定了 ...
- redhat 5.6安装wireshark
mkdir -p /mnt/cdrom mount -t iso9660 /dev/cdrom /mnt/cdrom cd mnt/cdrom/Server rpm -ivh lm_sensors-- ...