SpringMVC框架下数据的增删改查,数据类型转换,数据格式化,数据校验,错误输入的消息回显

在eclipse中javaEE环境下:

这儿并没有连接数据库,而是将数据存放在map集合中;

将各种架包导入lib下。。。

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"
id="WebApp_ID" version="2.5"> <!-- 配置SpringMVC的DispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.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> <!-- 配置 HiddenHttpMethodFilter: 把 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的bean配置文件: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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.0.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.atguigu.springmvc"></context:component-scan> <!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!--
default-servlet-handler 将在 SpringMVC 上下文中定义一个 DefaultServletHttpRequestHandler,
它会对进入 DispatcherServlet 的请求进行筛查, 如果发现是没有经过映射的请求, 就将该请求交由 WEB 应用服务器默认的
Servlet 处理. 如果不是静态资源的请求,才由 DispatcherServlet 继续处理 一般 WEB 应用服务器默认的 Servlet 的名称都是 default.
若所使用的 WEB 服务器的默认 Servlet 名称不是 default,则需要通过 default-servlet-name 属性显式指定 -->
<mvc:default-servlet-handler/> <!-- 一般都会配置这个 <mvc:annotation-driven ></mvc:annotation-driven>,
由于。。。requestmapping请求实现不了,使用这个,会使requestmapping请求一定实现
-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 配置 ConversionService ,自定义类型转换器,bean配置-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- bean。。。为自定义类型转换器的类名,其中,首字母小写 -->
<ref bean="employeeConverter"/>
</set>
</property>
</bean> <!-- 配置国际化资源文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean> </beans>

数据分装的两个类,是一对多的级联关系

数据的分装类:Department

package com.atguigu.springmvc.crud.entities;

public class Department {

    private Integer id;
private String departmentName; public Department() {
super();
// TODO Auto-generated constructor stub
} public Department(Integer id, String departmentName) {
super();
this.id = id;
this.departmentName = departmentName;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getDepartmentName() {
return departmentName;
} public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
} @Override
public String toString() {
return "Department [id=" + id + ", departmentName=" + departmentName
+ "]";
} }

数据的分装类:Employee,对此类中的属性,做了注解,可以实现表单验证

package com.atguigu.springmvc.crud.entities;

import java.util.Date;

import javax.validation.constraints.Past;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat; /*
* 在封装类的属性上面添加注解,是为了实现类型样式的转换:
* 步骤:
* 1>.在封装类的属性上面添加各种注解
* 2>.在springmvc.xml的配置中添加标配<mvc:annotation-driven></mvc:annotation-driven>,
* 不管怎样都不影响注解
*
* */
public class Employee { private Integer id; @NotEmpty
private String lastName; @Email
private String email;
//1 male, 0 female
private Integer gender; private Department department; //@Past注解的作用是:输入是此时间之前的时间,否则抛出错误
@Past
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth; @NumberFormat(pattern="#,###,###.#")
private Float salary; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Integer getGender() {
return gender;
} public void setGender(Integer gender) {
this.gender = gender;
} public Department getDepartment() {
return department;
} public void setDepartment(Department department) {
this.department = department;
} public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
} public Float getSalary() {
return salary;
} public void setSalary(Float salary) {
this.salary = salary;
} @Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", gender=" + gender + ", department=" + department
+ ", birth=" + birth + ", salary=" + salary + "]";
} public Employee(Integer id, String lastName, String email, Integer gender,
Department department) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.department = department;
} public Employee() {
// TODO Auto-generated constructor stub
} }

两个DAO方法,有两个分装类数据的增删改查方法;

DepartmentDAO:

package com.atguigu.springmvc.crud.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import org.springframework.stereotype.Repository; import com.atguigu.springmvc.crud.entities.Department; @Repository//标识持久层组件
public class DepartmentDao { private static Map<Integer, Department> departments=null; static{
departments=new HashMap<Integer, Department>(); departments.put(1001, new Department(1001,"D-AA"));
departments.put(1002, new Department(1002,"D-BB"));
departments.put(1003, new Department(1003,"D-CC"));
departments.put(1004, new Department(1004,"D-DD"));
departments.put(1005, new Department(1005,"D-EE"));
} //获取map集合中的所有值
public Collection<Department> getDepartments(){
return departments.values();
} //通过id值,获取map集合中的一个值
public Department getDepartment(Integer id){
return departments.get(id);
}
}

EmployeeDAO:

package com.atguigu.springmvc.crud.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import com.atguigu.springmvc.crud.entities.Department;
import com.atguigu.springmvc.crud.entities.Employee; @Repository//标识持久层组件
public class EmployeeDao { private static Map<Integer, Employee> employees=null; @Autowired
private DepartmentDao departmentDao;
static{
employees=new HashMap<Integer, Employee>(); employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));
employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));
employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
} private static Integer initId=1006; //添加数据的方法
public void save(Employee employee){
if(employee.getId()==null){
employee.setId(initId++);
} employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
employees.put(employee.getId(), employee);
} //获取全部的数据的方法
public Collection<Employee> getAll(){
return employees.values();
} //获取集合中的一个数据
public Employee getEmployee(Integer id){
return employees.get(id);
} //删除集合中的一个数据
public void delect(Integer id){
employees.remove(id);
}
}

EmployeeHandler类,基于注解的实现方法,便于页面之间的跳转:

package com.atguigu.springmvc.crud.handlers;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
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.RequestParam; import com.atguigu.springmvc.crud.dao.DepartmentDao;
import com.atguigu.springmvc.crud.dao.EmployeeDao;
import com.atguigu.springmvc.crud.entities.Employee; @Controller //标识表现层组件
public class EmployeeHandler { @Autowired
private DepartmentDao departmentDao; @Autowired
private EmployeeDao employeeDao; //自定义类型转换器,使用这个请求
//<!-- lastname-email-gender-department.id 例如: GG-gg@atguigu.com-0-105 -->
@RequestMapping("/testConversionServiceConverer")
public String testConverter(@RequestParam("employee") Employee employee){
employeeDao.save(employee);
System.out.println("save: " + employee);
return "redirect:/emps";
} /*
* 在方法定义上使用@ModelAttribute注解:Spring MVC在调用目标处理方法前,
* 会先逐个调用在方法上标注了@ModelAttribute的方法;即每个方法会先调用 方法上标注的@ModelAttribute的方法
*
* @ModelAttribute作用是:可以从隐含对象中获取隐含的模型数据中获取对象,(就是可以获取jsp中隐含属性的变量值),
* 在将请求参数绑定到对象中,再传入 入参,将方法入参对象,添加到模型中;
* */
@ModelAttribute
public void getEmployee(@RequestParam(value="id",required=false) Integer id, Map<String, Object> map){
if(id!=null){
map.put("employee", employeeDao.getEmployee(id));
}
} //修改操作,将修改好的信息重定向到emps请求
@RequestMapping(value="/emp", method=RequestMethod.PUT)
public String update(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
} //修改操作,将要修改的信息显示在input.jsp页面上
@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
public String input(@PathVariable("id") Integer id,Map<String, Object> map){
map.put("employee", employeeDao.getEmployee(id));
map.put("departments", departmentDao.getDepartments());
return "input";
} //删除操作,然后重定向到/emps请求,即在list.jsp页面显示
@RequestMapping(value="/emp/{id}", method=RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
employeeDao.delect(id);
return "redirect:/emps";
} //@Valid注解,表单验证时使用; 表单验证时对架包的处理:将
//hibernate-validator-5.0.0.CR2-dist\hibernate-validator-5.0.0.CR2\dist\lib\required
//下的三个架包:el-api-2.2,javax.el-2.2.4,javax.el-api-2.2.4
//复制到 apache-tomcat-6.0.16\apache-tomcat-6.0.16\lib 目录下 并且彻底粉碎apache-tomcat包 lib中的el架包,这样才可以实现表单的验证 //添加信息操作,如果添加信息的格式正确,然后重定向到/emps请求,即到list.jsp页面;
//如果添加信息的格式不正确,则跳转到本页面(input.jsp),显示错误的信息,需要在input.jsp页面加入标签
//<form:errors path="*"></form:errors>,将所有的错误都显示在一块,错误信息的显示,
//也可以分开写,格式为<form:errors path="lastName"></form:errors>,path值为属性值,分别写在属性值的下边, 显示在 输入错误的文本框下边 @RequestMapping(value="/emp", method=RequestMethod.POST)
public String save(@Valid Employee employee, Errors result,Map<String, Object> map){
System.out.println("save"+employee); if(result.getErrorCount()>0){
System.out.println("出错了");
//将所有错误都输出
for(FieldError error:result.getFieldErrors()){
System.out.println(error.getField()+":"+error.getDefaultMessage());
}
//若验证出错, 则转向定制的页面
map.put("departments", departmentDao.getDepartments());
return "input";
}
employeeDao.save(employee); return "redirect:/emps";
} //添加员工的信息,将一个空的employee和非空的department放入到map集合中,
//通过视图解析器,转发到input页面,可以添加信息,是一个GET请求
@RequestMapping(value="/emp",method=RequestMethod.GET)
public String input(Map<String, Object> map){
map.put("departments", departmentDao.getDepartments());
map.put("employee", new Employee());
return "input";
} //显示employee的全部的信息
@RequestMapping("/emps")
public String list(Map<String, Object> map){
map.put("employees", employeeDao.getAll());
return "list";
} //测试方法
@RequestMapping("/test")
public String testTT(){
return "test";
} /*由@InitBinder标识的方法,可以对WebDataBinder初始化,
* WebDataBinder是DataBinder的子类,用于完成由表单字段到javaBean属性的绑定
*
* @InitBinder注解的方法不能有返回值,它必须声明为void;
* 方法的参数通常是WebDataBinder;
*
* @InitBinder注解的方法的作用是使该属性在jsp页面上没有值,例如lastName属性在input.jsp页面上没有值
* */
// @InitBinder
// private void initBinder(WebDataBinder binder) {
// binder.setDisallowedFields("lastName");
//
// } }

  表单验证的国际化资源文件:其实基于注解的  每一个值书写顺序为  属性上边的注解. 表单要输入的属性值的集合(即map集合的key值).属性名=输入错误时,要回显得语句

文件名为:i18n.properties;要映射上次资源文件,要到springmvc.xml文件中配置国际化资源文件

NotEmpty.employee.lastName=^^LastName\u4E0D\u80FD\u4E3A\u7A7A.
Email.employee.email=Email\u5730\u5740\u4E0D\u5408\u6CD5
Past.employee.birth=Birth\u4E0D\u80FD\u662F\u4E00\u4E2A\u5C06\u6765\u7684\u65F6\u95F4. typeMismatch.employee.birth=Birth\u4E0D\u662F\u4E00\u4E2A\u65E5\u671F.

自定义类型转换器:可以将字符串GG-gg@atguigu.com-0-105,转换为四个属性的值,放在map集合中,这儿还是要在springmvc.xml配置文件中进行配置;

package com.atguigu.springmvc.crud.converters;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component; import com.atguigu.springmvc.crud.entities.Department;
import com.atguigu.springmvc.crud.entities.Employee; @Component
public class EmployeeConverter implements Converter<String, Employee>{ @Override
//自定义类型转换器
public Employee convert(String source) {
if(source!=null){
//GG-gg@atguigu.com-0-105
String [] vals=source.split("-");
if(vals!=null && vals.length==4){
String lastName = vals[0];
String email = vals[1];
Integer gender = Integer.parseInt(vals[2]); Department department = new Department();
department.setId(Integer.parseInt(vals[3])); Employee employee = new Employee(null, lastName, email, gender, department);
System.out.println(source + "--convert--" + employee);
return employee;
} }
return null;
} }

index.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> <center> <a href="emps">List All Employees</a>
<br><br> <a href="test">TTTTTTT</a>
</center> </body>
</html>

list.jsp页面,显示employee的全部信息,有增加,删除,修改信息的超链接,实现。。。

<%@ 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>
<!--
SpringMVC 处理静态资源:
1. 为什么会有这样的问题:
优雅的 REST 风格的资源URL 不希望带 .html 或 .do 等后缀
若将 DispatcherServlet 请求映射配置为 /,
则 Spring MVC 将捕获 WEB 容器的所有请求, 包括静态资源的请求, SpringMVC 会将他们当成一个普通请求处理,
因找不到对应处理器将导致错误。
2. 解决: 在 SpringMVC 的配置文件中配置 <mvc:default-servlet-handler/>
和 <mvc:annotation-driven></mvc:annotation-driven>,
-->
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
$(".delete").click(function(){
var href=$(this).attr("href");
$("form").attr("action",href).submit();
return false;
});
}) </script> </head>
<body> <!--
一个超链接是一个get请求,用jQuery方法和影藏的表单的方法,
将get请求转化为post请求,再将post请求转换为delete请求(web.xml文件中进行了配置)
-->
<form action="" method="post">
<input type="hidden" name="_method" value="DELETE"/>
</form> <center> <c:if test="${empty requestScope.employees }">
没有任何员工信息!!!
</c:if>
<br><br> <c:if test="${!empty requestScope.employees }">
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>ID</th>
<th>ListName</th>
<th>Email</th>
<th>Gender</th>
<th>Department</th>
<th>Edit</th>
<th>Delete</th>
</tr> <c:forEach items="${requestScope.employees }" var="emp">
<tr>
<td>${emp.id }</td>
<td>${emp.lastName }</td>
<td>${emp.email }</td>
<td>${emp.gender==0 ? 'Female' : 'Male' }</td>
<td>${emp.department.departmentName }</td>
<td> <a href="emp/${emp.id }">Edit</a> </td>
<td> <a class="delete" href="emp/${emp.id }">Delete</a> </td>
</tr>
</c:forEach> </table>
</c:if> <br><br>
<a href="emp">And New Employee</a> </center> </body>
</html>

input.jsp页面,employee信息添加的页面,此页面可以实现数据类型转换,数据格式化,数据校验,可以将校验值,显示在输入框下;

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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 action="testConversionServiceConverer" method="post">
<!-- lastname-email-gender-department.id 例如: GG-gg@atguigu.com-0-105 -->
Employee:<input type="text" name="employee"/>
<input type="submit" value="Submit"/>
</form> <br>
<!--
1. WHY 使用 form 标签呢 ?
可以更快速的开发出表单页面, 而且可以更方便的进行表单值的回显
2. 注意:
可以通过 modelAttribute 属性指定绑定的模型属性,
若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean
如果该属性值也不存在,则会发生错误。
--> <!-- 添加员工的信息的jsp -->
<form:form action="${pageContext.request.contextPath }/emp" method="post" modelAttribute="employee">
<!-- path 属性对应 html 表单标签的 name 属性值 --> <form:errors path="*"></form:errors>//所有的校验信息显示在一块
<br><br>
<c:if test="${employee.id==null }">
lastName:<form:input path="lastName"/>
<form:errors path="lastName"></form:errors>//分别显示校验信息
</c:if> <c:if test="${employee.id!=null }">
<form:hidden path="id"/> <!-- 将post请求转换为put请求,put请求即为更改 -->
<input type="hidden" name="_method" value="PUT"/> <%-- 对于 _method 不能使用 form:hidden 标签, 因为 modelAttribute 对应的 bean 中没有 _method 这个属性 --%>
<%--
<form:hidden path="_method" value="PUT"/>
--%>
</c:if> <br><br>
Email:<form:input path="email"/>
<form:errors path="email"></form:errors> <br><br> <%
Map<String, String> genders=new HashMap<String, String>();
genders.put("1", "Male");
genders.put("0", "Female"); request.setAttribute("genders", genders);
%>
Gender:
<br>
<form:radiobuttons path="gender" items="${genders }" delimiter="<br>"/>
<br><br> Department:<form:select path="department.id"
items="${departments }" itemLabel="departmentName" itemValue="id"></form:select>
<br><br>
<!--
1. 数据类型转换
2. 数据类型格式化
3. 数据校验.
1). 如何校验 ? 注解 ?
①. 使用 JSR 303 验证标准
②. 加入 hibernate validator 验证框架的 jar 包
③. 在 SpringMVC 配置文件中添加 <mvc:annotation-driven />
④. 需要在 bean 的属性上添加对应的注解
⑤. 在目标方法 bean 类型的前面添加 @Valid 注解
2). 验证出错转向到哪一个页面 ?
注意: 需校验的 Bean 对象和其绑定结果对象或错误对象时成对出现的,它们之间不允许声明其他的入参
3). 错误消息 ? 如何显示, 如何把错误消息进行国际化
--> Birth: <form:input path="birth"/>
<form:errors path="birth"></form:errors> <br><br>
Salary: <form:input path="salary"/>
<form:errors path="salary"></form:errors> <br><br>
<input type="submit" value="Submit"/>
</form:form> </body>
</html>
上一篇:Spring Data CrudRepository增删改查方法(八)


下一篇:SQL SERVER视图对查询效率的提高