语言: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模板的更多相关文章

  1. SpringBoot系列之集成jsp模板引擎

    目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...

  2. 十、Springboot之thymeleaf与jsp共存

    : pom.xml添加依赖 <!--thymeleaf整合JSP需要用到下面的依赖--> <dependency> <groupId>org.thymeleaf&l ...

  3. springboot-10-前端页面整合, thymeleaf, freemarker, jsp 模板使用

    springboot 中不建议使用jsp作为页面展示, 怎么使用可以看: http://412887952-qq-com.iteye.com/blog/2292471 关于什么是thymeleaf, ...

  4. springboot中Thymeleaf和Freemarker模板引擎的区别

    前言这两个都是属于模板引擎,但是各有各的好处,enn,在市面上比较多的也就是jsp.freemarker.velocity.thymeleaf等页面方案.Thymeleaf和Freemarker的区别 ...

  5. (二)springboot整合thymeleaf模板

    在我们平时的开发中,用了很久的jsp作view显示层,但是标签库和JSP缺乏良好格式的一个副作用就是它很少能够与其产生的HTML类似.所以,在Web浏览器或HTML编辑器中查看未经渲染的JSP模板是非 ...

  6. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  7. SpringBoot 之Thymeleaf模板.

    一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...

  8. springboot整合Thymeleaf模板引擎

    引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...

  9. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

随机推荐

  1. Codeforces Round #450 (Div. 2)

    Codeforces Round #450 (Div. 2) http://codeforces.com/contest/900 A #include<bits/stdc++.h> usi ...

  2. HDU 6315 Naive Operations(线段树区间整除区间)

    Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...

  3. 项目总结08:spring quartz 定时器Demo

    将定时器用到的quartz.jar放在lip文件下 quartz.xml文件(完整) <?xml version="1.0" encoding="UTF-8&quo ...

  4. 489. Robot Room Cleaner扫地机器人

    [抄题]: Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or block ...

  5. swift - 本地通知

    1. AppDelegate  注册 class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? fun ...

  6. day 30 HTML

    HTML: 超文本标记语言(Hyper Text Markup Language) Html基本结构 <!DOCTYPE html> <html> <!-- 定义HTML ...

  7. Linux下 MYSQL 主从复制、同步

    mysql从3.23.15版本以后提供数据库复制功能.利用该功能可以实现两个数据库同步,主从模式(A->B),互相备份模式(A<=>B)的功能. 主从模式(A->B)的配置过程 ...

  8. mysql备份数据库出错mysqldump: [ERROR] unknown option '--no-beep'

    公司数据库前一版本是部署在windows上面的,由于业务需要,迁移到linux,之前一段脚本在windows下使用定时任务执行正常. mysqldump -uzzz -pxxxx --opt --de ...

  9. abp ef codefirst Value cannot be null. Parameter name: connectionString

    错误原因是abp生成的项目是mvc类型的,但在使用时,选择了vue去开发,所以在abp上重新生成了一个vue项目,把原有的mvc项目给删掉了,没有将新生成的vue类型的项目的文件覆盖掉原有的mvc其他 ...

  10. hdu 1026(BFS+输出路径) 我要和怪兽决斗

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...