Spring Boot 整合 JSP
Spring Boot 与视图层的整合:
1、JSP
2、Thymeleaf
Java Server Page,是 Java 提供的⼀种动态网页技术,底层是 Servlet,可以直接在 HTML 中插⼊ Java代码。
JSP 底层原理:
JSP 是⼀种中间层组件,开发者可以在这个组件中将 Java 代码与 HTML 代码进行整合,由 JSP 引擎将组件转为 Servlet,再把开发者定义在组件中的混合代码翻译成 Servlet 的响应语句,输出给客户端。
1、创建基于 Maven 的 Web 项目,pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.19</version>
</dependency>
</dependencies>
2、创建 Handler
package com.southwind.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@GetMapping("/index")
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("mess","Hello Spring Boot");
return modelAndView;
}
}
3、JSP
<html> <head>
<title>Title</title>
</head> <body>
<h1>Index</h1>
${mess}
</body>
</html
4、application.yml
server:
port: 8181
spring:
mvc:
view:
prefix: /
suffix: .jsp
5、Application
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
实际应用
JSTL
1、pom.xml
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
2、创建实体类
package com.southwind.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
private Integer id;
private String name; }
3、Handler 中创建业务方法,返回 User 对象
package com.southwind.controller;
import com.southwind.entity.User;
import com.southwind.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserHandler {
@Autowired
private UserService userService;
@GetMapping("/findAll")
public ModelAndView findAll(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("list",userService.findAll());
return modelAndView;
}
@GetMapping("findById/{id}")
public ModelAndView findById(@PathVariable("id") Integer id){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("update");
modelAndView.addObject("user",userService.findById(id));
return modelAndView;
}
@PostMapping("/save")
public String save(User user){
userService.save(user);
return "redirect:/user/findAll";
}
@GetMapping("/deleteById/{id}")
public String deleteById(@PathVariable("id") Integer id){
userService.deleteById(id);
return "redirect:/user/findAll";
}
@PostMapping("/update")
public String update(User user){
userService.update(user);
return "redirect:/user/findAll";
}
}
4、Service
package com.southwind.service;
import com.southwind.entity.User;
import java.util.Collection;
public interface UserService {
public Collection<User> findAll();
public User findById(Integer id);
public void save(User user);
public void deleteById(Integer id);
public void update(User user);
}
package com.southwind.service.impl;
import com.southwind.entity.User;
import com.southwind.repository.UserRepository;
import com.southwind.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public Collection<User> findAll() {
return userRepository.findAll();
}
@Override
public User findById(Integer id) {
return userRepository.findById(id);
}
@Override
public void save(User user) {
userRepository.save(user);
}
@Override
public void deleteById(Integer id) {
userRepository.deleteById(id);
}
@Override
public void update(User user) {
userRepository.update(user);
}
}
5、Repository
package com.southwind.repository;
import com.southwind.entity.User;
import java.util.Collection;
public interface UserRepository {
public Collection<User> findAll();
public User findById(Integer id);
public void save(User user);
public void deleteById(Integer id);
public void update(User user);
}
package com.southwind.repository.impl;
import com.southwind.entity.User;
import com.southwind.repository.UserRepository;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class UserRepositoryImpl implements UserRepository {
private static Map<Integer,User> map;
static {
map = new HashMap<>();
map.put(1,new User(1,"张三"));
map.put(2,new User(2,"李四"));
map.put(3,new User(3,"王五"));
}
@Override
public Collection<User> findAll() {
return map.values();
}
@Override
public User findById(Integer id) {
return map.get(id);
}
@Override
public void save(User user) {
map.put(user.getId(),user);
}
@Override
public void deleteById(Integer id) {
map.remove(id);
}
@Override
public void update(User user) {
map.put(user.getId(),user);
}
}
6、JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html> <head>
<title>Title</title>
</head> <body>
<h1>Index</h1>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>
<a href="/user/deleteById/${user.id}">删除</a>
<a href="/user/findById/${user.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html> <head>
<title>Title</title>
</head> <body>
<form action="/user/save" method="post">
<input type="text" name="id"/><br/>
<input type="text" name="name"/><br/>
<input type="submit"/>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html> <head>
<title>Title</title>
</head> <body>
<form action="/user/update" method="post">
<input type="text" name="id" value="${user.id}" readonly/><br/>
<input type="text" name="name" value="${user.name}"/><br/>
<input type="submit"/>
</form>
</body>
</html>