2014-01-21 22 views
3

如何將表單輸入(easyui-datetimebox,大小寫)中的字符串轉換爲Controller中自動綁定的對象中的Calendar屬性?Spring:將字符串從視圖轉換爲日曆對象

我讀過http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html但我找不到任何接近正確的東西。

JSP:

<input id="DeadLineDate" 
    class="easyui-datetimebox" 
    name="DeadLineDate" 
    value="${SessionDeadLineDate}" 
    data-options="formatter:myformatter, 
       parser:myparser 
/> 

當submited,春季驗證引發錯誤:

Failed to convert property value of type java.lang.String to required type java.util.Calendar for property DeadLineDate; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Calendar] for property DeadLineDate: no matching editors or conversion strategy found. 

PS:彈簧3

編輯:添加控制器的方法來執行操作:

@Controller 
@RequestMapping("/project/MaintainProjectFrm") 
@SessionAttributes({"project","SessionDeadLineDate"}) 
public class MaintainProjectController { 

    /* ... many methods... */ 

    @RequestMapping(params = "update", method = RequestMethod.POST, produces={"text/plain; charset=UTF-8"}) 
    public String update(@ModelAttribute("project") Project project, 
          BindingResult result, 
            SessionStatus status, 
             ModelMap model, 
              HttpServletRequest req, 
               HttpServletResponse resp) throws IOException { 

     projectValidator.validate(project, result); 

     if (result.hasErrors()) { 
      //has errors, in this case, that one shown in text above, which is rendered again in view (JSP) 
      return "/project/MaintainProjectFrm"; 
     } else { 

      try{ 
       mpService.updateProject(project); 
      } 
      catch(Exception e){ 
       resp.setStatus(500); 
       resp.getWriter().write("Error updating project: " + e.getMessage()); 
       return "/project/MaintainProjectFrm"; 
      } 

      status.setComplete(); 

     } 
    } 

    /* ... yet other methods ... */ 
} 
+1

我們可以看到你的處理方法? –

+0

@SotiriosDelimanolis:當然,完成! – Alex

+0

我會使用@RequestParam從視圖中接收DeadLineDate,並手動創建一個Calendar對象,更新Project對象。但當然,這不是一種優雅的方式!我想知道如果在某個特定的maneer中,Spring能夠從這個屬性中自動綁定一個Calendar。 – Alex

回答

7

我假設你的Project類有字段DeadLineDate(字段應該以小寫字母開頭)。

@DateTimeFormat標註它像這樣

@DateTimeFormat(pattern = "yyyy/MM/dd") // or whatever pattern you want 
private Calendar DeadLineDate; 

您的客戶端,然後將需要發送相應的模式。

+0

優秀!!!我正在等待@gregor寫的更多內容,但絕對是最簡單的方法。 Spring很有這種靈活性,這真是太好了。是的,我肯定使用java模式。我的屬性在翻譯來自葡萄牙語的東西名稱時出現「資金不足」。非常感謝! – Alex

+0

偉大的aproach! – viniciusalvess

4

你有兩種方法可以實現這一點:你可以使用一個PropertyEditor

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    binder.registerCustomEditor(Calendar.class, new PropertyEditorSupport() { 
     @Override 
     public void setAsText(String text) throws IllegalArgumentException { 
      setValue(parseDate()); 
     } 

     private Calendar parseDate() { 
      try { 
       Calendar cal = Calendar.getInstance(); 
       SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); 
       cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011")); 
       return cal; 
      } catch (ParseException e) { 
       return null; 
      } 
     } 
    }); 
} 

有關文檔see thisand this.

或者你可以使用Spring的轉換服務。請參閱:"Spring 3 Type Conversion"

+0

感謝您的回答(我投了票)。但我寧願使用Sotirios Delimanolis方法,這對我來說是最簡單的解決方案。問候! – Alex

+0

@Alex:公認的解決方案適用於Spring 3.1嗎?對於我的情況,這個答案拯救了我!謝謝格雷戈爾。 +1 – sarwar026

+0

@ sarwar026,我實際上升級到了Spring 3.6,而且我還無法測試它。 – Alex

-1

試試這個像索蒂里奧斯Delimanolis說..

@DateTimeFormat(pattern = "yyyy/MM/dd") // or whatever pattern you want 
private Calendar DeadLineDate; 

最後,加上這pom.xml的:

<dependency> 
    <groupId>joda-time</groupId> 
    <artifactId>joda-time</artifactId> 
    <version>2.3</version> 
</dependency>