多语言展示
当前在线:1565今日阅读:60今日分享:41

SpringMVC中实现数据的格式化

SpringMVC中实现数据的格式化
工具/原料
1

SpringMVC

2

eclipse

方法/步骤
1

在表单提交页面加入一个日期字段。<%@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' %>    Insert title here                                               
                                                                                LastName:                                                                                                         <%-- 对于 _method 不能使用 form:hidden 标签, 因为 modelAttribute 对应的 bean 中没有 _method 这个属性 --%>                        <%--                                                 --%>                                                               
                Email:                
                <%                         Map genders = new HashMap();                        genders.put('1', 'Male');                        genders.put('0', 'Female');                                                request.setAttribute('genders', genders);                %>                Gender:                
                               
                Department:                
                                Birth:                
                Salary:                
                       
        

2

在SpringMVC控制层实现数据保存方法,并且打印employee。package com.gwolf.springmvc.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.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.gwolf.springmvc.dao.DepartmentDao;import com.gwolf.springmvc.dao.EmployeeDao;import com.gwolf.springmvc.domain.Employee;@Controllerpublic class EmployeeHandler {        @Autowired        private EmployeeDao employeeDao;                @Autowired        private DepartmentDao departmentDao;                                @RequestMapping(value='/emp', method=RequestMethod.POST)        public String save(Employee employee){                System.out.println('save: ' + employee);                                employeeDao.save(employee);                return 'redirect:/emps';        }}

3

在我们页面执行表单提交。当我们提交数据的时候,程序出现了一个400的错误,这是因为SpringMVC进行日期转化的时候没有告诉日期格式怎么转化。

4

修改实体类对象,给日期字段加上@DateTimeFormat注解说明。package com.gwolf.springmvc.domain;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;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        @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        }}

5

在SpringMVC中加上如下配置,这两个配置一定要按顺序配置,不然可能会有问题。

6

再次提交表单数据,查看我们的日期是否能够正确转化。

7

当类型转化出现异常的时候,我们可以通过BindingResult得到错误结果。

推荐信息