System: WIN8.1
Database: Oracle 11GR2
Development tools: MyEclipse 8.6
Frames: Spring3.2.9, SpringMVC3.2.9, MyBatis3.2.8
When using SPRINGMVC development, if the page has date format data, background accept is also java.util.Date, then report 400 error. The following is a demonstration example of the solution:
This is the entity class, Inside CreateDate is java.util.Date type
1 Importjava.util.Date;2 3 Public classUser {4 5 Private intuserId;6 PrivateString UserName;7 PrivateDate createdate;8 9 PublicUser () {}Ten One PublicUser (intuserId, String userName, Date createdate) { A Super(); - This. UserId =userId; - This. UserName =UserName; the This. CreateDate =CreateDate; - } - - PublicUser (String userName, Date createdate) { + Super(); - This. UserName =UserName; + This. CreateDate =CreateDate; A } at - Public intgetUserId () { - returnuserId; - } - - Public voidSetuserid (intuserId) { in This. UserId =userId; - } to + PublicString GetUserName () { - returnUserName; the } * $ Public voidsetusername (String userName) {Panax Notoginseng This. UserName =UserName; - } the + PublicDate getcreatedate () { A returnCreateDate; the } + - Public voidsetcreatedate (Date createdate) { $ This. CreateDate =CreateDate; $ } - - @Override the PublicString toString () { - return"User [createdate=" + CreateDate + ", userid=" +userIdWuyi+ ", username=" + UserName + "]"; the } -}
Page code
1 <formAction= "Reguser"Method= "POST">2UserName:<inputtype= "text"name= "UserName"/><BR>3CreateDate:<inputtype= "text"name= "CreateDate"/><BR>4Double type:<inputtype= "text"name= "DD"/><BR>5 <inputtype= "Submit"value= "Register">6 </form>
The error occurs because of a form binding to a primitive primitive type. You need to specify a specific type editor. Use the following: First Add method Initbinder in Basecontroller and annotate @initbinder with annotations, then spring MVC will register these editors before binding the form. The rest of the controllers inherit the class. Customdateeditor Spring itself has been provided. The code is as follows:
1 ImportJava.text.SimpleDateFormat;2 Importjava.util.Date;3 4 ImportOrg.springframework.beans.propertyeditors.CustomDateEditor;5 ImportOrg.springframework.stereotype.Controller;6 ImportOrg.springframework.web.bind.WebDataBinder;7 ImportOrg.springframework.web.bind.annotation.InitBinder;8 9 ImportSun.beans.editors.DoubleEditor;Ten ImportSun.beans.editors.FloatEditor; One ImportSun.beans.editors.IntEditor; A ImportSun.beans.editors.LongEditor; - - @Controller the Public classBasecontroller { - - @InitBinder - Public voidInitbinder (Webdatabinder binder) { + -Binder.registercustomeditor (Date.class,NewCustomdateeditor (NewSimpleDateFormat ("Yyyy-mm-dd"),true)); +Binder.registercustomeditor (int.class,Newinteditor ()); ABinder.registercustomeditor (Long.class,Newlongeditor ()); atBinder.registercustomeditor (Double.class,Newdoubleeditor ()); -Binder.registercustomeditor (float.class,Newfloateditor ()); - } - - -}
The above code not only has the date format editor, but also has the basic type editor, thus solves the SPRINGMVC in the Controller method accepts the parameter, the basic type error question.
The following is the test code, inherited Basecontroller can be run directly. The accepted parameters are entity classes and underlying types.
1 ImportOrg.springframework.stereotype.Controller;2 Importorg.springframework.web.bind.annotation.RequestMapping;3 4 ImportCom.kickstarter.entity.User;5 6@Controller ("Usercontroller")7 Public classUsercontrollerextendsbasecontroller{8 9@RequestMapping (value= "Reguser")Ten PublicString datetest (user user, doubleDD) { One A System.out.println (user.tostring ()); - System.out.println (DD); - return"Index"; the } -}
Above, problem solved.
SPRINGMVC + Spring + MyBatis Study notes: Submit data Encounter date format 400 error resolution