SPRINGMVC + Spring + MyBatis Learning Note: Submit data encounters basic type and date type 400 error resolution

Source: Internet
Author: User

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 Import java.util.Date;      2 3 public class User {4 5 private int userId; 6 private String userName; 7 private Date createdate; 8         9 Public User () {}10 one public user (int userId, String userName, Date createdate) {13 super (); This.userid = userid;14 This.username = username;15 this.createdate = createdate;16}17 Public User (String userName, Date createdate) {super (); this.username = username;21 this.createdate = createdate;22}23 public int getUserId () {return userid;26}27-public void Setuserid (in     T userId) {This.userid = userid;30}31 public String getusername () {return username;34 }35-public void Setusername (String userName) {PNs This.username = username;38}39-Public Date g Etcreatedate () {createdate;42 return}43-public void Setcreatedate (Date createdate) {thIs.createdate = createdate;46}47 @Override49 public String toString () {return "User [createdate = "+ CreateDate +", userid= "+ userId51 +", username= "+ UserName +"] "; 52}53}

Page code

1 <form action= "Reguser" method= "POST" >2         username:<input type= "text" name= "UserName"/><br>3         createdate:<input type= "text" name= "CreateDate"/><br>4         double type: <input type= "text" Name= " DD "/><br>5         <input type=" 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 Import Java.text.SimpleDateFormat; 2 Import java.util.Date; 3 4 Import Org.springframework.beans.propertyeditors.CustomDateEditor; 5 Import Org.springframework.stereotype.Controller; 6 Import Org.springframework.web.bind.WebDataBinder; 7 Import Org.springframework.web.bind.annotation.InitBinder; 8 9 Import sun.beans.editors.doubleeditor;10 import sun.beans.editors.floateditor;11 Import SUN.BEANS.EDITORS.INTEDITOR;12 Import sun.beans.editors.longeditor;13 @Controller15 public class Basecontroller {16 @InitBinder public void Initbinder (Webdatabinder binder) {Binder.registercustomeditor (D Ate.class, New Customdateeditor (New SimpleDateFormat ("Yyyy-mm-dd"), true); Binder.registercustomeditor (Int.class, New Inteditor ()); Binder.registercustomeditor (Long.class, new  Longeditor ());  Binder.registercustomeditor (Double.class, New Doubleeditor ());  Binder.registercustomeditor (Float.class, New Floateditor ()); 25    }26 27 28} 

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 Import Org.springframework.stereotype.Controller; 2 Import org.springframework.web.bind.annotation.RequestMapping; 3  4 import com.kickstarter.entity.User; 5  6 @Controller ("Usercontroller") 7 public class Usercontroller extends basecontroller{8  9     @RequestMapping (value= "Reguser") public     String datetest (user user, double dd) {11         a         System.out.println (User.tostring ());         System.out.println (DD);         return "index";     }16}


Above, problem solved. Then we switch the second way, delete the Basecontroller class, add annotations directly to the CreateDate field in the user entity class, and note the 10th line of code:

 1 Import java.util.Date; 2 3 Import Org.springframework.format.annotation.DateTimeFormat; 4 5 public class User {6 7 private int userId; 8 private String UserName; 9 @DateTimeFormat (pattern= "Yyyy-mm-dd") Private Date createdate;12 Public user () {}14 public user (int userId, String Usernam E, Date createdate) {+ super (); This.userid = userid;18 this.username = username;19 thi S.createdate = createdate;20}21 public User (String userName, Date createdate) {(); His.username = username;25 This.createdate = createdate;26}27-public int getUserId () {retur n userid;30}31 public void Setuserid (int userId) {This.userid = userid;34}35-Public Str ing GetUserName () {PNS return username;38}39-public void Setusername (String userName) {in this. UserName = username;42}43 public Date GetCReatedate () {createdate;46}47, public void Setcreatedate (Date createdate) {this.cr Eatedate = createdate;50}51 @Override53 public String toString () {si return ' User [createdate= ' + CreateDate + ", userid=" + userId55 + ", username=" + UserName + "]"; 56}57}

This also resolves the date format to 400 issue. And no matter whether the page has data can be used normally.

Please refer to the following information:

Http://www.spring.net

SPRINGMVC + Spring + MyBatis Learning Note: Submit data encounters basic type and date type 400 error resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.