创建maven项目就不说了,需要的找我前面的博客
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.geyao</groupId>
<artifactId>springmaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Springmaven01</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/home.jsp</welcome-file>
</welcome-file-list>
<!-- 处理中文乱码 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- SpringMVC控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<!-- 主要就是DispatcherServlet这个servlet起到分发的作用,对请求进行控制分发 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 每个springmvc项目都要一个springmvc项目配置位置,下面配置springmvc配置文件的路径 -->
<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>dispatcherServlet</servlet-name>
<!-- 下面配置springmvc的过滤分发请求类型,可以是/ 或者*.action等 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
springMVC-servlet.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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 定义要扫描 controller的包-->
<context:component-scan base-package="wormday.springmvc.helloworld" />
<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<!--指定视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图的路径 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 视图名称后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
indexcontoller类
package wormday.springmvc.helloworld;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class IndexController {
@RequestMapping("/index")
public String say(HttpSession session) {
System.out.print(session.getId());
return "home";
}
}
UserController类
package wormday.springmvc.helloworld;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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 wormday.springmvc.pojo.Address;
import wormday.springmvc.pojo.User;
@Controller
@RequestMapping("/user")
@SessionAttributes(value= {"user"},types= {String.class})
public class UserController {
@RequestMapping("/form")
public String form() {
return "form";
}
@RequestMapping("/save")
public String save(User user) {
System.out.print(user);
return "success";
}
@RequestMapping("/edit")
public ModelAndView edit(HttpSession session) {
User user = new User();
user.setUsername("helen");
user.setPassword("123");
user.setAge(12);
user.setEmail("[email protected]");
Address address = new Address();
address.setProvince("anhui");
address.setCity("haha");
user.setAddress(address);
ModelAndView mv = new ModelAndView();
mv.setViewName("form");
mv.addObject("user", user);
return mv;
}
@RequestMapping("/edit1")
public String edit1(Map<String,Object> map) {
User user = new User();
user.setUsername("helen111");
user.setPassword("123");
user.setAge(12);
user.setEmail("[email protected]");
Address address = new Address();
address.setProvince("jiangsu");
address.setCity("hah");
user.setAddress(address);
map.put("user",user);
return "form";
}
@RequestMapping("/edit2")
public String edit2(Model model) {
User user = new User();
user.setUsername("helen111");
user.setPassword("123");
user.setAge(12);
user.setEmail("[email protected]");
Address address = new Address();
address.setProvince("jiangsu");
address.setCity("hah");
user.setAddress(address);
model.addAttribute("user",user);
model.addAttribute("strvalue","gp1701");
model.addAttribute("intvalue",1701);
return "form";
}
@RequestMapping(value="/list",method=RequestMethod.GET)
public String list() {
return "user/list";
}
@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
public String delete() {
//执行删除
System.out.println("delete");
return "redirect:/user/list";
}
@RequestMapping(value="/{id}",method=RequestMethod.PUT)
public String update() {
//执行更新
System.out.println("update");
return "redirect:/user/list";
}
}
Address类
package wormday.springmvc.pojo;
public class Address {
private String province;
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;
}
private String city;
public Address() {
super();
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + "]";
}
}
user类
package wormday.springmvc.pojo;
public class User {
public User() {
super();
// TODO Auto-generated constructor stub
}
private String username;
private String password;
private String email;
private Integer age;
private Address address;
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 [username=" + username + ", password=" + password + ", email=" + email + ", age=" + age
+ ", address=" + address + "]";
}
}
form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${sessionScope.strvalue}
${sessionScope.intvalue}
<form action="save" method="post">
<label for="">用户名:<input type="text" name="username" value="${RequestScope.user.username}"/></label>
<label for="">密码:<input type="text" name="password" value="${user.password}"/></label>
<label for="">年龄:<input type="text" name="age" value="${sessionScope.user.age}"/></label>
<label for="">邮箱:<input type="text" name="email" value="${user.email}"/></label>
<label for="">省份:<input type="text" name="address.province" value="${user.address.province}"/></label>
<label for="">城市:<input type="text" name="address.city" value="${user.address.city}"/></label>
<button>登录</button>
</form>
</body>
</html>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户列表</h1>
<a href="javascript:void(0)" οnclick="deleteById()">删除</a>
<a href="javascript:void(0)" οnclick="updateById()">修改</a>
<form action="1" method="post" id="deleteForm">
<input type="text" name="_method" value="DELETE"/>
<button>delete提交</button>
</form>
<form action="1" method="post" id="updateForm">
<input type="text" name="_method" value="PUT"/>
<button>update提交</button>
</form>
<script>
function deleteById(){
document.getElementById('deleteForm').submit();
}
function updateById(){
document.getElementById('updateForm').submit();
}
</script>
</body>
</html>
运行结果
标签:String,springframework,springMvc34,user,address,put,org,restful,public From: https://www.cnblogs.com/yao-655442/p/16608813.html