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的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
随机推荐
- 约束布局 ConstraintLayout
app:layout_constraintVertical_bias="0.5"app:layout_constraintHorizontal_bias="0.5&quo ...
- 从零开始写bootloader(2)
下图是设置内核启动参数的存放图示,由于bootloader启动内核时,需要给内核传输一些启动参数,但是由于当bootloader把内核 启动之后,程序就跳转到内核中执行了,再也不会回到bootload ...
- stm32初做项目心得
在导师的带领下,基本了解了嵌入式的开发的基本流程: 1.首先从厂家拿到样板之后,首先进行检测,检测什么呢,先检测电源系统,看你的电源系统是否能够正常工作,就是各个管脚是否短路,断路. 2.检测完之后, ...
- POJ 1177 Picture(线段树周长并)
描述 A number of rectangular posters, photographs and other pictures of the same shape are pasted on ...
- python的定时任务模块--schedule
首先先安装一下模块 下面我们简单的学习一下schedule模块 先简单的看个示例 import schedule def test(*args,**kwargs): print("hello ...
- Day 04 列表,元祖,range
列表: why: 1.字符串取值费劲 2.对字符串做任何操作,取出来的都是字符串 3.字符串有长度限制 基于以上原因,python提供了另一个数据类型,list 容器类数据类型. 列表页脚数组,可以存 ...
- TableViewCell去除选中效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableV ...
- 32-java 里面list的问题
申明一个对象时,一般前的类型需要申明的: List list1 = new ArrayList<Edge1>(); 这个不能调用对象的属性 和 List<Edge1> l ...
- WEB框架Django之中间件/缓存/CBV/信号
一Djano的中间件 1 中间件的概念 中间件顾名思义,是介于request与respose处理之间的一道处理过程,相对比较轻量级,并且全局上改变django的输入与输出.因为改变是全局, 所有需要谨 ...
- RBAC 继完善代码之后的,再一次完善
在上一篇文章中,我的中间件是 保存在我的web 业务app 中的.但是rbac我想要完成的是一个 组件的功能, 所以这个验证的 中间件, 何不放到rbac的app之中: 为了太乱先放一个项目的目录图 ...