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的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
随机推荐
- mysql垂直分区和水平分区
数据库扩展大概分为以下几个步骤: 1.读写分离:当数据库访问量还不是很大的时候,我们可以适当增加服务器,数据库主从复制的方式将读写分离: 2.垂直分区:当写入操作一旦增加的时候,那么主从数据库将花更多 ...
- 修改bootstrap-table中的分页样式
使用bootstrap-table时,使用$("")选择器没办法选中下方的分页button按钮,可能跟它是动态生成的有关吧. 最终找到与之对应的js(bootstrap-table ...
- echarts折线图Demo
echarts链接:http://echarts.baidu.com/examples/editor.html?c=line-stack 黑底代码:http://gallery.echartsjs.c ...
- Spring <context:annotation-config> 和 <context:component-scan> 区别
一篇很不错的文章,看到就是赚到Get.... https://www.cnblogs.com/leiOOlei/p/3713989.html 说白了 :<context:component-sc ...
- 配置eclipse+SDK+ADT开发环境
第一步:配置jdk和eclipse环境(已完成): 第二步:ADT配置:依次点击菜单栏:help -> Install new software -> Add -> Local... ...
- jQuery和js使用点滴
1.checkbox全选按钮 <input type="checkbox" name="allcheck" id="allcheck" ...
- 关于vuex状态管理模式架构
一. 什么是vuex 集中存储管理所有组件的状态 并以相应的规则保证以一种可预测的方式发生变化. 例子: 实现加减 <p>{{count}} <button @click=" ...
- 关于call_rcu在内核模块退出时可能引起kernel panic的问题
http://paulmck.livejournal.com/7314.html RCU的作者,paul在他的blog中有提到这个问题,也明确提到需要在module exit的地方使用rcu_barr ...
- ubuntu12.04下安装Apache+PHP+MySQL
一.Apache1.安装apache2: sudo apt-get install apache2 2.重启apache2: sudo /etc/init.d/apache2 restart 3.在浏 ...
- JSON中的{}与[]的区别
[]:索引数组 {}:关联数组(js中,即对象)