案例数据库使用Map模拟完成
功能清单:
功能 | url和请求方式 |
---|---|
访问首页 | / 请求方式:get |
查询所有数据 | /user 请求方式:get |
删除数据 | /user/1 请求方式:delete |
跳转添加页面 | /add 请求方式:get |
添加数据 | /user 请求方式:post |
跳转更新页面 | /user/1 请求方式:get |
更新数据 | /user 请求方式:put |
1:某条数据的id值
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springmvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.14</version>
</dependency>
<!--日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!---->
<!--spring和thymeleaf整合-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.14.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 字符编码过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*
hiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
hiddenHttpMethodFilter
/*
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
springMVC
/
springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!- 开启组件扫描-->
<context:component-scan base-package="com.example.demo1"></context:component-scan>
<!-配置thymeleaf视图解析器-->
<bean id="viewResolver"
class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
<!--
处理静态资源,例如html、js、css、jpg
若只设置该标签,则只能访问静态资源,其他请求则无法访问
此时必须设置<mvc:annotation-driven/>解决问题
-->
<mvc:default-servlet-handler/>
<!-- 开启mvc注解驱动 -->
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 处理响应中文内容乱码 -->
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="defaultCharset" value="UTF-8" />
<property name="supportedMediaTypes">
<list>
<value>text/html</value>
<value>application/json</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:view-controller path="/add" view-name="user_add"/>
</beans>
User.java
package com.example.demo1;
import org.springframework.stereotype.Repository;
public class User {
private Integer id;
private String name;
private String password;
//男1,女0
private Integer gender;
public User() {
}
public User(Integer id, String name, String password, Integer gender) {
this.id = id;
this.name = name;
this.password = password;
this.gender = gender;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", gender=" + gender +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
}
dao.java
import java.util.Map;
@Repository
public class dao {
private static Map<Integer,User> lists = null;
static{
lists = new HashMap<Integer,User>();
lists.put(1,new User(1,"张三","123456",1));
lists.put(2,new User(2,"李梅","123456",0));
lists.put(3,new User(3,"李四","123456",1));
lists.put(4,new User(4,"小丽","123456",0));
}
private static Integer initnum = 5;
public Collection<User> getAll(){
return lists.values();
}
public User getById(Integer id){
return lists.get(id);
}
public void delete(Integer id){
lists.remove(id);
}
public void update(User user){
if (user.getId()==null){
user.setId(initnum++);
}
lists.put(user.getId(),user);
}
}
controller.java
package com.example.demo1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Collection;
@Controller
public class controller {
@Autowired
private dao dao;
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping(value = "/user",method = RequestMethod.GET )
public String getAll(Model model){
Collection<User> all = dao.getAll();
model.addAttribute("userlist",all);
return "user_list";
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public String add(User user){
dao.update(user);
return "redirect:/user";
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public String toupdate(@PathVariable("id") Integer id,Model model){
User user = dao.getById(id);
model.addAttribute("user_u",user);
return "user_update";
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String update(User user){
dao.update(user);
return "redirect:/user";
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
dao.delete(id);
return "redirect:/user";
}
}
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1><br/>
<a th:href="@{/user}">查看所有用户</a><br/>
<a th:href="@{/add}">添加用户</a>
</body>
</html>
user_list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<table id="datatab" border="1" cellspacing="0" cellpadding="0" style="text-align: center">
<tr><th colspan="5">用户列表</th></tr>
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
<th>性别</th>
<th>操作</th>
</tr>
<tr th:each="user:${userlist}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.password}"></td>
<td th:text="${user.gender}"></td>
<td><a th:href="@{'/user/'+${user.id}}">更新</a>
<a class="delete" @click="deleteuser" th:href="@{'/user/'+${user.id}}">删除</a>
</td>
</tr>
</table>
<form id="deleteform" th:method="post">
<input type="hidden" name="_method" value="delete">
</form>
<a th:href="@{/add}">添加用户</a>
</body>
<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>
<script type="text/javascript">
var vue = new Vue({
el:"#datatab",
methods:{
deleteuser:function (event) {
var delete_form = document.getElementById("deleteform");
delete_form.action = event.target.href;
delete_form.submit();
event.preventDefault();
}
}
});
</script>
</html>
user_add.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<form th:action="@{/user}" th:method="post">
姓名:<input type="text" name="name"><br/>
密码:<input type="password" name="password"><br/>
性别:<input type="radio" name="gender" value="1">男
<input type="radio" name="gender" value="0">女<br/>
<input type="submit" value="添加">
</form>
</body>
</html>
user_update.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户更新</title>
</head>
<body>
<form th:action="@{/user}" th:method="post">
<input type="hidden" name="_method" value="put">
id:<input type="number" name="id" th:value="${user_u.id}"><br/>
姓名:<input type="text" name="name" th:value="${user_u.name}"><br/>
密码:<input type="password" name="password" th:value="${user_u.password}"><br/>
性别:<input type="radio" name="gender" value="1" th:field="${user_u.gender}">男
<input type="radio" name="gender" value="0" th:field="${user_u.gender}">女<br/>
<input type="submit" value="更新">
</form>
</body>
</html>