spring MVC controller (Controller) 400 receives the error occurred parameter date type

Recently finished school spring mvc, encountered a problem. When I have a form that date types of data (such as date of birth) when you submit to a background controller; occurs 400error; 400error with short words is receiving a request parameter type and parameter types of background is not fine.

I guess that this is probably because a date type parameter problem; The following summarizes some of the processing springMVC processing at the receiving date type parameter.

 

==== one method

  We parameters background with String received first, then the string converted to date. / **

     * New employees 
     * 
     * @param empVo 
     * @return returns successfully identify
      * / 
    @ RequestMapping ( "/ empAdd" ) 
    @ResponseBody // hireday is passed over the form reception date 
    public String empAdd (EmpVo empVo, String hireday ) {
 
     // date date string format converted into    the SimpleDateFormat the format
= new new the SimpleDateFormat ( "the MM-dd-YYYY" ); the ParsePosition position1 = new new the ParsePosition (0 ); a date hiredayDate = format.parse ( hireday, Position1); // are the entry date and date of birth empVo.setHireDay ( hiredayDate ); // . DateHelper.parseString (StringHelper.getBirAgeSex (empVo.getCardno ()) GET ( "Birthday") // get ID cards by birth date empVo.setBirthday (DateHelper.parseString (StringHelper.getBirAgeSex (empVo.getCardno ()) GET ( "Birthday"), "the MM-dd-YYYY." )); // state empVo.setStatus (. 1 ); // default password empVo.setPassword ( "123456" ); emp.save (empVo); return "Success" ; }

 

==== two methods

  Date Format entity class add annotations  

@DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date hireDay;//入职日期

@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    public Date getHireDay() {
        return hireDay;
    }

 

==== method three ( recommended )

  Add date data controller binding method

// Converts a string to the Date class 
    @InitBinder
     public  void of an initBinder (WebDataBinder Binder, the WebRequest Request) {
         // convert date format 
        DateFormat the dateFormat = new new the SimpleDateFormat ( "the MM-dd-YYYY HH: mm: SS" );
         // Register custom editor 
        binder.registerCustomEditor (a Date. class , new new the CustomDateEditor (the dateFormat, to true )); 
        
    }

 

==== method four

  Date realize the global configuration and type converters

  Design date transform class

package com.xueqing.demo;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
 
public class DateEdtor implements WebBindingInitializer {   
    public void initBinder(WebDataBinder binder, WebRequest request) {
        // TODO Auto-generated method stub
        //转换日期格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}

Configured in the spring MVC profile

<!-- 配置全局日期转换器 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">    
            <bean class="nuc.ss.wlb.core.web.DateEdtor"/>
        </property>
    </bean>

  

  

Guess you like

Origin www.cnblogs.com/268lwc/p/12127948.html