走进SpringBoot之页面模板与JSP

走进SpringBoot之页面模板与JSP

 虽然在我们现在工作中,后端 人员一般只写接口,前后端分离,一般也使用不到页面模板,但是 官方推荐使用thymeleaf,这边就蛮记录一下,对于jsp做过web开发的同志应该都 会清楚,现在的项目基本是使用jsp,虽然官方已经不建议使用了,但是还是要了解一下。

Thymeleaf

 了解thymeleaf 之前我们要知道,SpringBoot默认的几个静态页面的目录:

/static、/public、/resources 、/META-INF/resources

我们可以将项目中使用到的静态文件放在这几个项目中

首先我们创建一个SpringBoot项目

1.加入thymeleaf依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

 2.编写controller

@Controller
 
public class ThymeleafController {
	@RequestMapping(value = "/testthymeleaf")
	public ModelAndView test(ModelAndView mv) {
	    mv.setViewName("thymeleaf1");
	    mv.addObject("name","欢迎使用Thymeleaf!");
	    return mv;
	}
}

3.在src/main/resources/templates目录下创建 thymeleaf.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>thymeleaf demo</title>
</head>
<body>
<h1>Hello thymeleaf</h1>

<h2 th:text="'名称:'+${name}">默认值</h2>
</body>
</html>

 4.配置thymel信息,在application.properties下配置

 #thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

 5.启动项目,浏览器访问http://127.0.0.1:8080/testthymeleaf

至此,Thymeleaf整合成功,当然thymeleaf还有很多内容,但是我个人也没用过,只是稍微了解 。

如果按照上面的做法 之后访问报404 的话 检查一下SpringBoot的版本,设置为1.3.3就可以解决了。

 

JSP

对于一些老项目基本都是使用jsp,所以SpringBoot也有提供对jsp的支持

1.添加jsp的依赖

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

2.编写controller

@Controller
public class JspController {

	@GetMapping("/testjsp")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("name", "ccl");
		//模版名称,实际的目录为:src/main/webapp/jsp/index.html
		mv.setViewName("test");
		return mv;
	}
}

 3.在src/main/webapp/WEB-INF/jsp下创建test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charsetUTF-8">
<title>jsp demo </title>
</head>
<body>
<h1>Hello Jsp</h1>
<h2 >这是 ${name}  的jsp</h2>
</body>
</html>

4.配置jsp,在application.properties里配置:

#jsp 支持
spring.mvc.view.suffix=.jsp
spring.mvc.view.prefix=/WEB-INF/jsp/

如果SpringBoot的版本太低的话配置为:

#jsp 支持
spring.view.suffix=.jsp
spring.view.prefix=/WEB-INF/jsp/

5.启动项目,浏览器访问http://127.0.0.1:8080/testjsp

至此。SpringBoot整合jsp成功。 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值