pom.xml
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JSP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- Maven 依赖示例 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.16.0</version> <!-- 使用适当的版本号 -->
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
application.yml
spring:
mvc:
view:
prefix: /WEB-INF/views/
suffix: .jsp
#logging:
# level:
# org.springframework: DEBUG
DemoApplication.java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
HelloHandler的增删改查
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/index")
public ModelAndView index(){
System.out.println("Index method is called.");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("list",studentRepository.findAll());
System.out.println("= =:"+studentRepository.findAll());
return modelAndView;
}
@GetMapping("/deleteById/{id}")
public String deleteById(@PathVariable("id")long id){
studentRepository.deleteById(id);
return "redirect:/hello/index";
}
@GetMapping("/save")
public String showSaveForm(Model model) {
// Add an empty student for the form
return "save";
}
@PostMapping("/save")
public String save(Student student){
studentRepository.saveOrUpdate(student);
return "redirect:/hello/index";
}
@PostMapping("/update")
public String update(Student student){
studentRepository.saveOrUpdate(student);
return "redirect:/hello/index";
}
@GetMapping("/findById/{id}")
public ModelAndView findById(@PathVariable("id")long id){
Student byId = studentRepository.findById(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("update");
modelAndView.addObject("student",studentRepository.findById(id));
return modelAndView;
}
}
Student类
@Data
@AllArgsConstructor
public class Student {
private long id;
private String name;
private int age;
}
StudentRepository 和 StudentRepositoryimpl
public interface StudentRepository {
public Collection<Student> findAll();
public Student findById(long id);
public void saveOrUpdate(Student student);
public void deleteById(long id);
}
@Repository
public class StudentRepositoryimpl implements StudentRepository {
private static Map<Long, Student> studentMap;
static {
System.out.println("Initializing studentMap...");
studentMap = new HashMap<>();
studentMap.put(1L,new Student(1L,"张三",22));
studentMap.put(2L,new Student(2L,"田小班",10));
studentMap.put(3L,new Student(3L,"田小文",10));
System.out.println("studentMap initialized: " + studentMap);
}
@Override
public Collection<Student> findAll() {
return studentMap.values();
}
@Override
public Student findById(long id) {
return studentMap.get(id);
}
@Override
public void saveOrUpdate(Student student) {
studentMap.put(student.getId(),student);
}
@Override
public void deleteById(long id) {
studentMap.remove(id);
}
}
----------JSP表格------------------
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1>学生信息</h1>
<table>
<tr>
<th>学生编号</th>
<th>学生姓名</th>
<th>学生年龄</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="student">
<tr>
<td><c:out value="${student.id}" /></td>
<td><c:out value="${student.name}" /></td>
<td><c:out value="${student.age}" /></td>
<td>
<a href="/hello/findById/${student.id}">修改</a>
<a href="/hello/deleteById/${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<a href="/hello/save">添加学生</a>
----------JSP表单------------------
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/hello/save" method="post">
<div>ID:<input type="text" name="id"></div>
<div>Name:<input type="text" name="name"></div>
<div>Age:<input type="text" name="age"></div>
<input type="submit" value="提交">
</form>
</body>
</html>