pom.xml
4.0.0
com.monkey1024
02mvc
0.0.1-SNAPSHOT
war
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-webmvc
5.0.4.RELEASE
02mvc
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
UTF-8
web.xml
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
characterEncodingFilter
/*
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springMVC
/
springmvc.xml
error/myerror
User.java
package com.monkey1024.bean;
import java.time.LocalDate;
import org.springframework.format.annotation.DateTimeFormat;
public class User {
private String name;
private int age;
@DateTimeFormat(pattern="yyyy-MM-dd")
private LocalDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
}
UserController.java
package com.monkey1024.controller;
import java.time.LocalDate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.monkey1024.bean.User;
@Controller
@RequestMapping("/user")
public class UserContoller {
@RequestMapping("/addUser.do")
// public ModelAndView addUser(String name, int age, @DateTimeFormat(pattern="yyyy-MM-dd") Date birthday) throws Exception{
// public ModelAndView addUser(String name, int age, @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate birthday) throws Exception{
public ModelAndView addUser(User user) throws Exception{
ModelAndView mv = new ModelAndView();
mv.addObject("name",user.getName());
mv.addObject("age",user.getAge());
mv.addObject("birthday",user.getBirthday());
mv.setViewName("user");
return mv;
}
}
add_user.jsp
Insert title here
姓名:
年龄:
生日:
user.jsp
Insert title here
姓名:
年龄:
生日: