Original link: Http://www.tuicool.com/articles/aYfaqa
When using SPRINGMVC, we may need to pass an object from View to the Controller. And when this Object is just some simple string, int, or Boolean member variable, SPRINGMVC can automatically convert the String type of the View layer's JSON to the corresponding type of the Object member variable. However, when this Object is a member variable of the date type, SPRINGMVC will make an error when converting a string to a date type and report an exception. But we need to use the Date type again, but Spring provides us with a simple way to do this task.
Summary
First, summarize the whole process, in fact, 3 steps:
1. Add @DateTimeFormat Annotations to the Date type's attributes
2. Join Joda Related Packages
3. Add <mvc:annotation-driven/> To the SPRINGMVC configuration file
SPRINGMVC provides an annotated @DateTimeFormat. You can convert a string type that is passed from View to a Date type. The use of the method is very simple, add annotations directly on the member variable, you can also specify format, as follows:
1 Public classPerson {2 3 PrivateString name;4 5 //add annotations directly to the date type, specifying formatting styles6 7@DateTimeFormat (pattern = "YYYY-MM-DD" )8 9 PrivateDate birthday;Ten One //Setterandgetter A -}
At this point, do not think Bob, you still need to complete the following two steps to be able.
The first need to add Joda jar package. Because the Joda package is used in @DateTimeFormat annotations, the missing packet is also reported as an exception. If you are using a direct import jar package, go to download Joda-jar import, if you are using a Maven managed project jar, then add dependencies in the profile file:
1 <Dependency>2 3 <groupId>Joda-time</groupId>4 5 <Artifactid>Joda-time</Artifactid>6 7 <version>2.3</version>8 9 </Dependency>
Second, you need to add the configuration in the Springmvc configuration XML file (typically the Dispatchservlet.xml file): <mvc:annotation-driven/>. This sentence configuration is a shorthand, in fact, the Spring container is injected with two Bena, respectively: Defaultannotationhandlermapping and Annotationmethodhandleradapter. The inside of the @DateTimeFormat annotation also needs to be processed using the two beans injected earlier, so without this configuration, there is no corresponding bean in the Spring container to handle the annotations also error. At this point, all the steps are complete and you can run.
Let's run a test and test the process:
First you need a form:
1 <formAction= "Test"Method= "POST">2 3 <inputtype= "text"name= "Name">4 5 <inputtype= "text"name= "Birthday">6 7 <inputtype= "Submit"name= "Submit">8 9 </form>
Receive with a Controller:
1@RequestMapping ("/test" )2 3 PublicModelandview Test (httpservletrequest request,4 5 @ModelAttribute person person) {6 7Modelandview view =NewModelandview ();8 9 System.out.println (person.tostring ());Ten OneView.setviewname ("/test/data"); A - returnview; - the}
SPRINGMVC member variable methods for handling date types