2014-05-02 53 views
1

我在使用日期選擇器的Spring MVC中遇到了麻煩。當我訪問日期時間選擇器時,代碼工作正常。但萬一我試圖離開該字段爲空,即不使用日期選擇器,它將作爲默認值(它應該)爲空,然後給出錯誤:未訪問日期選擇器會給出轉換錯誤

無法轉換java.lang.String類型的屬性值爲所需的類型java.util.Date屬性ticComDropoofDate;嵌套異常是org.springframework.core.convert.ConversionFailedException:無法將值「」從類型java.lang.String轉換爲java.util.Date類型;嵌套的異常是java.lang.IllegalArgumentException

我附上我的代碼與此和任何幫助將不勝感激。

我的控制器:

public class TicketController extends MasterController{ 

    @InitBinder 
    protected void initBinder(WebDataBinder binder){ 
     if(binder.getTarget() instanceof TicketDetailsBean) 
      binder.setValidator(new TicketDetailsValidator()); 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); 
    } 

     @RequestMapping(value="/TicketRegistration", method=RequestMethod.POST) 
    public ModelAndView createTicketRegistration(@Valid TicketDetailsBean ticket, BindingResult result, Model model) throws SQLException{ 
     TicketManager dm = new TicketManager(); 
     TechManager dm1 = new TechManager(); 
     SimpleMailManager sm = new SimpleMailManager(); 
     ModelAndView modelAndView = new ModelAndView(); 
     if(!result.hasErrors()){ 
      try{ 
       String message = dm.saveTicketRegistration(ticket); 
       /*if(check) 
       { 
        TicketDetailsBean ticket1 = new TicketDetailsBean(); 
        OwnerDetailsBean owner1 = new OwnerDetailsBean(); 
        ticket1 = sm.fetchData(ticket); 
        owner1 = sm.fetchOwner(ticket1); 

       }*/ 
       sm.sendMail();/*(owner1,ticket1);*/ 
       List<TicketDetailsBean> ticket2 = dm.findRegisteredTicket(); 
       if(message != null){ 
        message = "Duplicate entry not allowed. Data with same name already in the database..."; 
        modelAndView.addObject("Message", message); 
       } 
       modelAndView.setViewName("TicketDatabase"); 
       modelAndView.addObject("ticketDetailsBean", new TicketDetailsBean()); 
       modelAndView.addObject("ticketList", ticket2); 
       modelAndView.addObject("techDetailsBean", new TechDetailsBean()); 
       modelAndView.addObject("tech", globalBean); 
       return modelAndView; 
      }catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     else{ 
      List<TechDetailsBean> tech = dm1.findRegisteredTech(); 
      modelAndView.addObject(result.getAllErrors()); 
      modelAndView.setViewName("TicketRegistration"); 
      modelAndView.addObject("ticketDetailsBean", new TicketDetailsBean()); 
      modelAndView.addObject("techDetailsBean", new TechDetailsBean()); 
      modelAndView.addObject("tech", globalBean); 
      modelAndView.addObject("techList",tech); 
      return modelAndView; 
     } 
     /*List<TicketDetailsBean> ticket2 = dm.findRegisteredTicket(); 
     modelAndView.addObject("ticketDetailsBean", new TicketDetailsBean()); 
     modelAndView.addObject("ticketList", ticket2);*/ 
     return modelAndView; 
    } 

我的jsp:

<script>  
     $(function() { 
        $("#datepicker").datepicker({ 
           dateFormat: 'yy-mm-dd', 
          showOn: "button", 
          buttonImage: "<%=request.getContextPath() %>/resources/images/calendar.gif", 
          buttonImageOnly: true 
         }); 
       }).val(''); 
       $(function() { 
        $("#datepicker1").datepicker({ dateFormat: 'yy-mm-dd', 
         showOn: "button", 
          buttonImage: "<%=request.getContextPath() %>/resources/images/calendar.gif", 
          buttonImageOnly: true 
       }); 
      }).val(''); 
    </script> 

    <form:form modelAttribute="ticketDetailsBean" action="TicketRegistration" method="post"> 

<table> 
    <tr> 
     <td> 
      <h1>Ticket Registration Form</h1> 
      <h3>Please enter the information below</h3> 
      <h3>click "<b>Save</b>" when complete. </h3> 
      <h3> Fields marked with an asterisk '<b>*</b>' are required.</h3> 
      <h3> Click "<b>Reset</b>" to clear the fields</h3> 
     </td> 
    </tr> 

    <tr> 
     <td> 
      <fieldset title="info"> 
       <legend><b>Ticket Information</b></legend> 
       <table> 
        <tr> 
         <td align="left"><form:label for="ticDate" path="ticDate" cssErrorClass="error">* Ticket Date :</form:label></td> 
         <td align="left"><form:input path="ticDate" id="datepicker1" size="35" maxlength="35"/> <form:errors path="ticDate" /></td> 
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticServicetag" path="ticServicetag" cssErrorClass="error">* Service Tag Number :</form:label></td> 
         <td align="left"><form:input name="ticServicetag" path="ticServicetag" id="ticSrvctag" size="35" maxlength="35" value = "${computerList.comServicetag}" /> <form:errors path="ticServicetag" /></td>  
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticOwnerSuUsername" path="ticOwnerSuUsername" cssErrorClass="error">* Owner Su_username :</form:label></td> 
         <td align="left"><form:input path="ticOwnerSuUsername" id="owsusenm" size="35" maxlength="35" value ="${computerList.comOwnerSUUsername }" /> <form:errors path="ticOwnerSuUsername" /></td>  
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticTechname" path="ticTechname" cssErrorClass="error">* Tech Name :</form:label></td> 
         <td align="left"> 
          <select name="ticTechname" id = "ticTechname" id="slectboxid1"> 
           <option value = ""> ---Select---    </option> 
           <c:forEach var="item" items="${techList}"> 
             <option value = "${item.techName}"><c:out value= "${item.techName}"/></option> 
            </c:forEach> 
          </select> 
         <form:errors path="ticTechname" /></td> 
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticComProblem" path="ticComProblem" cssErrorClass="error">* Problem :</form:label></td> 
         <td align="left"><form:textarea path="ticComProblem" rows ="3" cols ="27"/> <form:errors path="ticComProblem" cssErrorClass="error" /></td> 
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticComments" path="ticComments" cssErrorClass="error"> Comments :</form:label></td> 
         <td align="left"><form:textarea path="ticComments" rows = "3" cols = "27"/> <form:errors path="ticComments" /></td> 
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticItemtoOrder" path="ticItemtoOrder" cssErrorClass="error"> Item To Order :</form:label></td> 
         <td align="left"><form:input path="ticItemtoOrder" size="35" maxlength="35" /> <form:errors path="ticItemtoOrder" /></td> 
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticComDropoofDate" path="ticComDropoofDate" cssErrorClass="error"> Computer Drop Off Date:</form:label></td> 
         <td align="left"><form:input path="ticComDropoofDate" id="datepicker" size="35" maxlength="35" /> <form:errors path="ticComDropoofDate"/></td> 
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticAssignedTech" path="ticAssignedTech" cssErrorClass="error">* Tech Assigned :</form:label></td> 
         <td align="left"> 
          <select name="ticAssignedTech" id = "ticAssignedTech" id="slectboxid2"> 
           <option value = ""> ---Select---   </option> 
           <c:forEach var="item" items="${techList}"> 
             <option value = "${item.techName}"><c:out value= "${item.techName}"/></option> 
            </c:forEach> 
          </select> 
          <form:errors path="ticAssignedTech" /> 
         </td> 
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticStatus" path="ticStatus" cssErrorClass="error">* Ticket Status :</form:label></td> 
         <td align="left"> 
          <select name="ticStatus" id = "ticStatus" id="slectboxid3"> 
          <option value="">---Select---    </option> 
          <option value="Open"> Open </option> 
          <option value="InProgress">In Progress</option> 
          <option value="Closed">Closed</option> 
          </select> 
          <form:errors path="ticStatus" /> 
         </td> 
        </tr> 
        <tr> 
         <td align="left"><form:label for="ticAddEquipments" path="ticAddEquipments" cssErrorClass="error"> Additional Equipments dropped with Computer :</form:label></td> 
         <td align="left"><form:textarea rows= "3" path="ticAddEquipments" cols ="27"/> <form:errors path="ticAddEquipments" /></td> 
        </tr>   
      </table> 
      </fieldset> 
      <fieldset> 
       <div align="center"> 
       <form action="TicketDatabase" method="get"> 
        <input type="submit" name="submit" value="Save"/> 
        <input type="reset" name="reset"/> 
       </form> 
       </div> 
      </fieldset> 
    </td> 
    </tr> 
    </table> 

回答

0

異常消息:

Unable to convert value "" from type java.lang.String to type java.util.Date

春天拋出,因爲你還沒有告訴春天空字符串轉換空值。

binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); 

通,如果你想空當空字符串像true

binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); 


CustomDateEditor構造 java docs有:

public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty)

創建新的CustomDateEditor實例,使用給定的DateFormat進行解析渲染allowEmpty參數指出是否允許空字符串 解析,即將其解釋爲空值。否則, 在這種情況下會引發IllegalArgumentException。

參數

dateFormat - 日期格式用於分析和呈現

allowEmpty - 如果空字符串應該被允許

+0

我試過,但它仍然沒有似乎工作。我嘗試了Java.sql.date,其中也存在相同的問題。 – Bubble

+0

終於工作..不得不修改一些小東西...感謝Rembo .... – Bubble

+0

@Bubble歡迎您,如果它解決了您的問題,您可以標記爲答案。 – Rembo