1.Springboot引入JSP
- spring官网说,不建议springboot使用jsp
- spring默认值支持 Thymeleaf、 FreeMarker·、Velocity·、Groovy JSP
1.1.引入jsp相关的依赖
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--servlet依赖jar -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--jsp依赖jar -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<!--Jstl标签依赖的jar包start -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
1.2.在main下面创建 webapp/WEB-INF/jsp
1.3.将application.properties该名为application.yml,并编 写yml配置文件
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
1.4.将webapp配置为项目web资源目录(重要)
1.5.编写jsp文件
1.6.编写controller类进行测试
@Controller
public class HelloController {
@RequestMapping("/test")
public String getHello(){
System.out.println("Springboot JSP!!!!!!");
return "hello";
}
}
1.7.浏览器输入地址访问测试
localhost:8080/test
2.Springboot中对静态资源的处理
2.1.方式1:webjars的方式引入静态资源(常见的前端框架)依赖包
webjars网址:www.webjars.org
- 引入静态资源相关的依赖jar包
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
- jsp页面编写
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script type="application/javascript"
src="/webjars/jquery/3.6.0/jquery.js"></script>
<script>
$(function () {
$("#hh").click(function () {
alert(123);
});
})
</script>
</head>
<body>
<h1 id="hh">hello jsp for springboot</h1>
</body>
</html>
- 编写Controller
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
- 因为需要配置jsp页面,所以需要在application.yml中配置
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
注意,此项目也是需要配置springboot对于jsp页面支持的
2.2.方式2:访问静态资源
spring-boot-autoconfigure.jar中org.springframework.boot.autoconfigure.web.servlet中的 WebMvcAutoConfiguration.java
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
规定:只要在上述目录中编写静态资源文件,springboot会自动加载。
2.2.1.编写css文件进行测试
2.2.2.jsp页面编写
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="icon" href="/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/css/test.css">
</head>
<body>
<h1>静态资源加载</h1>
</body>
</html>
可以将静态资源存放上springboot默认识别的所有的资源目录中,springboot都会默认加载。
2.3.方式3:静态资源自定义目录
2.3.1.在resources目录下创建html目录,创建springboot.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
springbootHtml
</body>
</html>
2.3.2.配置application.yml文件
2.3.3.启动服务进行测试
localhost:8080/springboot.html
- 看看默认的资源路径是否还能起作用
发现默认的静态资源目录不能用了,如何解决呢?
- 在yml文件配置css的静态资源目录
3.Springboot使用Thymeleaf
3.1.什么是Thymeleaf
- Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎,能够处理HTML, XML,JavaScript,CSS甚至纯文本。
- 简单来说,thymeleaf和jsp作用类似,实现方式有所不同。
3.2.引入Thymeleaf的起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.3.看一下Thymeleaf的属性类
查看spring-boot-autoconfigure中的 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";//默认模板的位置prefix
private String suffix = ".html";//默认模板的后缀 .html
private String mode = "HTML";
private Charset encoding;
private boolean cache;
private Integer templateResolverOrder;
private String[] viewNames;
演示案例
在templates目录下创建login.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>欢迎来到登录页面</h1>
<form method="post">
用户名:<input type="text" name="name">
密码:<input type="number" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>
这时运行项目,是不能直接访问login.html页面,需要通过controller跳转一下
编写controlelr
@Controller
public class LoginController {
@RequestMapping("/toLogin")
public String toLogin(){
return "login";// 当返回login字符串时,thymeleaf会自动的拼接前缀和后缀 /templates/ login .html
}
}
这时才能成功。
3.4.案例:编写一个实体类和Controller
案例:浏览器输入http://localhost:8080/访问login.html,登录判断用户名是否为admin,密码是 否为123,之后跳转到list.html页面展示数据。
public class User {
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
@Controller
public class LoginController {
@RequestMapping("/")
public String toLogin(){
return "login";// 当返回login字符串时,thymeleaf会自动的拼接前缀和后缀 /templates/ login .html
}
@RequestMapping("/login")
public String login(String name,String password,Model model){
if ("admin".equals(name) && password.equals("123")){
List<User> list = new ArrayList<>();
list.add(new User("张三","zhangsan"));
list.add(new User("李四","lisi"));
list.add(new User("王五","wangwu"));
model.addAttribute("userList",list);
}
return "list";
}
@RequestMapping("/edit")
@ResponseBody
public String edit(String name,String password){
System.out.println("修改的用户名:"+name+",密码:"+password);
return "success";
}
}
3.5.案例:编写一个Thymeleaf页面
注意:需要在html中引入thymeleaf头信息
xmlns:th="http://www.thymeleaf.org"
login.htm
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/thym.css}">
</head>
<body>
<h1>欢迎来到登录页面</h1>
<form th:action="@{/login}" method="post">
用户名:<input type="text" name="name">
密码:<input type="number" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>
list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/thym.css}">
</head>
<body>
<h1>用户列表</h1>
<table border="1">
<tr>
<td>姓名</td>
<td>密码</td>
<td>修改</td>
</tr>
<tr th:each="user:${userList}">
<td th:text="${user.name}"></td>
<td th:text="${user.password}"></td>
<td><a th:href="@{/edit(name=${user.name},password=${user.password})}">修改</a>
</td>
</tr>
</table>
</body>
</html>
4.修改Springboot运行服务器
- 源码分析
spring-boot-autoconfigure.jar中 org.springframework.boot.autoconfigure.web.embedded中
-
Springboot支持的服务器种类
-
Jetty
-
Netty
-
Tomcat
-
Undertow
-
-
移除tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除tomcat依赖 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
- 添加jetty相关依赖
<!-- 切换服务器类型 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
- 启动进行测试