2015-05-21 61 views
0

我使用Spring MVC中使用Hibernate, 當我提交表單是它顯示了一個錯誤在提交hibernate表單時出錯?

The request sent by the client was syntactically incorrect". 

這是我的形式:

 <table> 
     <form:form action="save" method="post" modelAttribute="staffSchedule"> 
     <tr> 
      <td>date:</td> 
      <td><form:input path="date"/></td> 
     </tr> 
     <tr> 
      <td>Start Time:</td> 
      <td><form:input path="startTime"/></td> 
     </tr> 
     <tr> 
      <td>End Time:</td> 
      <td><form:input path="endTime"/></td> 
     </tr> 
     <tr> 
      <td>Status:</td> 
      <td> 
       <form:select path="status"> 
        <form:options items="${status}" /> 
       </form:select> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2" align="center"> 
       <input type="submit" value="Save"> 
      </td> 
     </tr>   
     </form:form> 

控制器代碼

@RequestMapping(value = "/save", method = RequestMethod.POST) 
public ModelAndView saveUser(@ModelAttribute StaffSchedule staffSchedule) { 
    staffScheduleDao.saveOrUpdate(staffSchedule); 
    return new ModelAndView("redirect:/list"); 
} 

我該怎麼做才能糾正這個問題?我認爲問題出在date

+0

與休眠無關 – Jens

回答

0

Spring不知道如何反序列化從前端進入的日期。 您可以使用@InitBinder來格式化日期。

嘗試使用類似這樣的格式來設置日期;

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); 
    sdf.setLenient(true); 
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true)); 
} 

PS:示例代碼取自here

+0

謝謝Semih eker。 它只顯示轉換的日期格式。錯誤仍然存​​在。 –

+0

不用客氣,您必須修改客戶端發送的格式{「MM/dd/yyyy」}。 –

+0

以編輯形式顯示日期在SimpleDateFormat中使用哪種格式。但仍然存在錯誤。 mysql的日期格式是yyyy:MM:dd。 –