<?
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" id="WebApp_ID" version="2.5">
<display-name>SpringMVCTest3</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>
<!--配置SpringMVC的DispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置DispatcherServlet的一个初始化參数:作用是,配置SpringMVC配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置filter:把POST 请求转化为DELETE、PUT请求 -->
<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>
---------------------------------------------------spring.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-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 配置自己主动扫描的包 -->
<context:component-scan base-package="controller;dao;domain" />
<!--配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 将在SpringMVC上下文中定义一个DefaultServletHttpRequestHandler, 他会对进入DispatcherServlet的请求 进行筛选,假设发现是没经过映射的请求,就将请求交给WEB的应用server默认 的Servlet处理,假设不是静态资源的请求,才由DispatcherServlet继续处理
一般WEB应用server的Servlet的名称都是default -->
<mvc:default-servlet-handler />
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
---------------------------------------------------------------------------------------------------comtroller.UserHandler.java--------------------------------------------
package controller;
//@Controller注解:将该类表示为控制器
@Controller
public class UserHandler {
// @Autowired它能够对类成员变量、方法及构造函数进行标注,完毕自己主动装配的工作。 通过 @Autowired的使用来消除 set,get方法。
@Autowired
private UserDao userDao;
@Autowired
private AddressDao addressDao;
/**
* 有 @ModelAttribute标记的方法。会在每一个目标方法运行之前被SpringMVC调用。
* @RequestParam(value="id", required=false)表单中的參数,required表示是否必须
*/
@ModelAttribute
public void setUser(@RequestParam(value="id", required=false)Integer id, Map<String, Object> map){
if(id != null){
map.put("user", userDao.getUser(id));
}
}
/**
* 改动保存操作
*
* @RequestMapping("/addUser")使用requestMapping来映射URL method相应的请求方式
*/
@RequestMapping(value="/saveUser", method=RequestMethod.PUT)
public String updateUser(User user){
userDao.saveUser(user);
return "redirect:/getUsers";
}
/**
* 编辑改动操作
*
* @RequestMapping("/addUser")使用requestMapping来映射URL method相应的请求方式
* @PathVariable("id")路径URL中的參数信息
*/
@RequestMapping(value="/editUser/{id}", method=RequestMethod.GET)
public String editUser(@PathVariable("id") Integer id, Map<String, Object> map){
map.put("addresses", addressDao.getAddresses());
map.put("user", userDao.getUser(id));
return "add";
}
/**
* 删除操作
*
* @RequestMapping("/addUser")使用requestMapping来映射URL method相应的请求方式
* @PathVariable("id")进行接受參数
*/
@RequestMapping(value="/deleteUser/{id}", method=RequestMethod.DELETE)
public String deleteUser(@PathVariable("id") Integer id){
userDao.deleteUser(id);
//删除后进行显示操作
return "redirect:/getUsers";
}
/**
* 保存操作
*
* @RequestMapping("/addUser")使用requestMapping来映射URL method相应的请求方式
*/
@RequestMapping(value = "/saveUser", method = RequestMethod.POST)
public String saveUser(User user) {
userDao.saveUser(user);
// 保存完毕后进入到显示操作
return "redirect:/getUsers";
}
/**
* 进入到加入操作页面
*
* @RequestMapping("/addUser")使用requestMapping来映射URL method相应的请求方式
*/
@RequestMapping(value = "/addUser", method = RequestMethod.GET)
public String addUser(Map<String, Object> map) {
map.put("addresses", addressDao.getAddresses());
// 相应struts1里边的from表单对象
map.put("user", new User());
return "add";
}
/**
* 显示操作
*
* @RequestMapping("/addUser")使用requestMapping来映射URL method相应的请求方式
*/
@RequestMapping("/getUsers")
public String getUses(Map<String, Object> map) {
map.put("users", userDao.getUsers());
return "list";
}
}
-------------------------------------------dao.UserDao.java------------------------------------------------------------
package dao;
//@Repository注解:它用于将数据訪问层 (DAO 层 ) 的类标识为 Spring Bean
@Repository
public class UserDao {
private static Map<Integer, User> users;
@Autowired
private AddressDao addressDao;
static {
users = new HashMap<Integer, User>();
users.put(1, new User(1, "a", 11, 1, new Address(101, "beijing")));
users.put(2, new User(2, "b", 22, 0, new Address(102, "shanghai")));
users.put(3, new User(3, "c", 33, 1, new Address(103, "guangzhou")));
}
private static Integer initId = 4;
public void saveUser(User user) {
if (user.getId() == null) {
user.setId(initId++);
}
user.setAddress(addressDao.getAddress(user.getAddress().getId()));
users.put(user.getId(), user);
}
public Collection<User> getUsers() {
return users.values();
}
public User getUser(Integer id) {
return users.get(id);
}
public User deleteUser(Integer id) {
return users.remove(id);
}
}
-------------------------------------------------------------dao.AddressDao.java-------------------------------------------------------------------
package dao;
@Repository
public class AddressDao {
private static Map<Integer, Address> addresses;
static {
addresses = new HashMap<Integer, Address>();
addresses.put(101, new Address(101, "beijign"));
addresses.put(102, new Address(102, "shanghai"));
addresses.put(103, new Address(103, "guangzhou"));
}
public Collection<Address> getAddresses() {
return addresses.values();
}
public Address getAddress(Integer id) {
return addresses.get(id);
}
}
-----------------------------------------domain------------------------------------------------package domain;
public class Address {
private Integer id;
private String name;
}
----------------------------
package domain;
public class User {
private Integer id;
private String name;
private Integer age;
private Integer sex;
private Address address;
}
--------------------------------------------views/jsp------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
<c:if test="${ empty requestScope.users }">
没有不论什么员工信息
</c:if>
<c:if test="${!empty requestScope.users }">
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>sex</th>
<th>address</th>
<th>edit</th>
<th>delete</th>
</tr>
<c:forEach items="${requestScope.users }" var="user">
<tr>
<td>${user.id }</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.sex==0?"女":"男" }</td>
<td>${user.address.name}</td>
<td><a href="editUser/${user.id }">edit</a></td>
<td>
<form action="deleteUser/${user.id}" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="delete"/>
</form></td>
</tr>
</c:forEach>
</table>
</c:if>
<br>
<br>
<a href="addUser">addUser</a>
</body>
</html>
---------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
<form:form action="${pageContext.request.contextPath }/saveUser" method="POST" modelAttribute="user">
<c:if test="${user.name==null }">
name:<form:input path="name"/><br>
</c:if>
<c:if test="${user.name!=null }">
<form:hidden path="id"/>
<input type="hidden" name="_method" value="PUT">
</c:if>
age:<form:input path="age"/><br>
<%
HashMap<String, String> sex = new HashMap<String, String>();
sex.put("0", "女");
sex.put("1", "男");
request.setAttribute("sex", sex);
%>
sex:<form:radiobuttons path="sex" items="${sex }"/><br>
address:<form:select path="address.id" items="${addresses }" itemLabel="name" itemValue="id"/><br>
<input type="submit" value="submit">
</form:form>
</body>
</html>
============================================SpringMVC form标签===============================================================
<!--Springform表单标签:form:input 、form:password、form:hidden、form:textarea:相应HTML表单的Text、password、hidden、textarea标签 -->
<!-- form:radiobutton:单选框组建标签。对那个表单bean相应的属性和value值相等时。单选框被选中。
-->
<!-- form:radiobuttons:单选框组件标签,用于构造多个单选框
-items:能够是一个list、String【】或者map
-itemValue:指定radio的value值。能够是集合中的bean的一个属性值
-itemLabel:指定radio的label值
-delimiter:多个单选框能够通过delimiter指定切割符 -->
<!--form:checkbox:复选框组件。
用户构造单个复选框
form:checkboxs:用户构造多个复选框。使用方式同form:radiobuttons标签
form:select:用于构造下拉框组件,使用方式等同于form:radiobuttons标签
form:option :下拉框选项组件标签。使用方式等同于form:radiobuttons标签
from:errors:显示表单组件或数据校验所相应的错误 -->