Restful风格 是一种比较流行的软件架构风格和开发方式。
源码下载地址:
**链接:**https://pan.baidu.com/s/1plbpUpeQt4-LANVoAPlvKQ
提取码:zhro
使用须知:
首先我是用了maven做的,我的链接里面也有两个项目,分别是maven的和springmvc的,如果是在springmvc下用的话也差不多。
(2)在地址栏输入设置的url地址list进行界面跳转,显示后台设置的数据。
(3)点击id为1003的删除按钮,删除数据。(F5刷新)
(4)点击添加按钮跳转到添加数据的界面,输入相应数据保存数据。
(5)点击id为1001的修改按钮,跳转到修改界面,修改它的数据。
它的基本思路就是定义javabean存储数据,在dao层做相关crud操作,在controller那里调用方法去执行它,list界面做了一个根据id提交表单,在controller有相应的url跳转,add界面和edit界面类似,获取数据保存到绘画中去再读取出来,主要注意的就是url的路径。
我们学习的过程中肯定会遇到许多问题,培养自学能力和有一颗想进步的心是非常重要的。
结构目录:
(1)首先肯定要做一个pojo类来存数据,就是一个普通的javabean。
User.java
package com.qfedu.springmvc.pojo;
public class User {
private Integer id;
private String username;
private String password;
private String email;
private Integer age;
private Address address;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer id, String username, String password, String email, Integer age, Address address) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.age = age;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", age="
+ age + ", address=" + address + "]";
}
}
Address.java
package com.qfedu.springmvc.pojo;
public class Address {
private Integer id;
private String province;
private String city;
public Address(Integer id, String province, String city) {
super();
this.id = id;
this.province = province;
this.city = city;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Address() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Address [id=" + id + ", province=" + province + ", city=" + city + "]";
}
}
(2)配置dao层
这里采用静态方式事先存一些数据进去,因为没有使用数据库,全部存在绘画里。并调用一些系统的方法来为crud操作做准备。
package com.qfedu.springmvc.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.qfedu.springmvc.pojo.Address;
import com.qfedu.springmvc.pojo.User;
@Repository
public class Userdao {
private static Map<Integer,User> users=null;
static {
users=new HashMap<Integer, User>();
users.put(1001,new User(1001,"l1","123","[email protected]",13,new Address(1,"广西","南宁")));
users.put(1002,new User(1002,"l2","123","[email protected]",14,new Address(2,"广东","广州")));
users.put(1003,new User(1003,"l3","123","[email protected]",15,new Address(3,"贵州","贵阳")));
}
private static Integer intId=1004;
public void save(User user) {
if(user.getId()==null) {
user.setId(intId++);
}
users.put(user.getId(), user);
}
public Collection<User> getAll(){
return users.values();
}
public User get(Integer id) {
return users.get(id);
}
public void delete(Integer id) {
users.remove(id);
}
}
(3)配置Controller,没有定义servlet,直接在这里一起操作了。
package com.qfedu.springmvc.controller;
import java.util.Collection;
import java.util.Map;
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 org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.qfedu.springmvc.dao.Userdao;
import com.qfedu.springmvc.pojo.Address;
import com.qfedu.springmvc.pojo.User;
@Controller
//把存入user的值放进session中,规定存进去的值是String类型
@SessionAttributes(value = {"user"},types = {String.class})
public class UserController {
@Autowired
private Userdao userDao;
///////////////////////////////////////////////////
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String list(Model m) {
Collection<User> mo=userDao.getAll();
m.addAttribute("mo", mo);
return "user/list";
}
@RequestMapping(value = "/{id}",method =RequestMethod.DELETE)
public String delete(@PathVariable(value="id") Integer id) {
System.out.println("delete" + id);
userDao.delete(id);
return "redirect:/list";
}
@RequestMapping(value = "/add",method =RequestMethod.GET)
public String add() {
System.out.println("add");
return "user/add";
}
@RequestMapping(value = "/",method =RequestMethod.POST)
public String save1(User user) {
System.out.println("save1");
userDao.save(user);
return "redirect:/list";
}
@RequestMapping(value = "/{id}",method =RequestMethod.GET)
public String edit10(@PathVariable(value="id") Integer id,Model c) {
System.out.println("edit");
User user= userDao.get(id);
c.addAttribute("user", user);
return "user/edit";
}
@RequestMapping(value = "/{id}",method =RequestMethod.PUT)
public String update(@PathVariable(value="id") Integer id,User user) {
System.out.println("update");
user.setId(id);
userDao.save(user);
return "redirect:/list";
}
}
(4)几个显示数据的jsp页面(忽略home.jsp)
list,jsp
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户列表</h1>
<a href="add">添加</a>
<table border="1">
<tr>
<td>id</td>
<td>姓名</td>
<td>年龄</td>
<td>省份</td>
<td>城市</td>
<td>操作</td>
</tr>
<c:forEach items="${mo}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.age}</td>
<td>${user.address.province}</td>
<td>${user.address.city}</td>
<td>
<a href="javascript:void(0)" onclick="deteteById(${user.id})">删除</a>
<a href="${user.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
<form action="1" method="post" id="d1">
<input type="text" name="_method" value="DELETE"/>
<button>DELETE提交</button>
</form>
<form action="1" method="post" id="d2">
<input type="text" name="_method" value="PUT"/>
<button>PUT提交</button>
</form>
<script>
function deteteById(id){
var form=document.getElementById('d1');
form.action = id;
document.getElementById('d1').submit();
}
function updateById(id){
var form=document.getElementById('d2');
form.action = id;
document.getElementById('d2').submit();
}
</script>
</body>
</html>
add.jsp
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/" method="post">
<label for="">用户名: <input type="text" name="username" value="${user.username}" /></label><br />
<label for="">密码: <input type="password" name="password" value="${user.password}"/></label><br />
<label for="">邮箱: <input type="text" name="email" value="${user.email}"/></label><br />
<!-- value="${sessionScope.user.age} 从session中取出数据 -->
<label for="">年龄: <input type="text" name="age" value="${sessionScope.user.age}"/></label><br />
<label for="">省份: <input type="text" name="address.province" value="${user.address.province}"/></label><br />
<label for="">城市: <input type="text" name="address.city" value="${user.address.city}"/></label><br />
<button>保存</button>
</form>
</body>
</html>
edit.jsp
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/${user.id}" method="post">
<input type="hidden" name="_method" value="PUT"/>
<!--<input type="hidden" name="id" value="${user.id }"/>-->
<label for="">用户名: <input type="text" name="username" value="${user.username}" /></label><br />
<label for="">密码: <input type="password" name="password" value="${user.password}"/></label><br />
<label for="">邮箱: <input type="text" name="email" value="${user.email}"/></label><br />
<!-- value="${sessionScope.user.age} 从session中取出数据 -->
<label for="">年龄: <input type="text" name="age" value="${sessionScope.user.age}"/></label><br />
<label for="">省份: <input type="text" name="address.province" value="${user.address.province}"/></label><br />
<label for="">城市: <input type="text" name="address.city" value="${user.address.city}"/></label><br />
<button>保存</button>
</form>
</body>
</html>
(5)其他的一些基本配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>springmvc-04-mvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springMVC</servlet-name>
<!-- 请求触发器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup><!-- 提前启动 -->
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
<!-- 或*.action -->
</servlet-mapping>
<!-- 解决springMVC的post乱码 -->
<filter>
<filter-name>encodingFilter</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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
pom.xml
<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>com.qfedu</groupId>
<artifactId>springmvc-04-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- 引入标签库 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 引入servlet-api和jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8001</port>
<path>/mvc</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置controller层路径扫描 com.qfedu.springmvc.controller -->
<context:component-scan base-package="com.qfedu.springmvc.controller"></context:component-scan>
<!-- 扫描dao -->
<context:component-scan base-package="com.qfedu.springmvc.dao"></context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>