次のコードを使用して、文字列入力フォーム パラメータを Bean の日付プロパティにバインドしようとしています。registrationDate
日付フォーマッタをbeanという名前の特定のプロパティにのみバインドしたいUser
。registrationDate
の呼び出しでフィールド名を指定するさまざまな方法を試し
ましたbinder.registerCustomEditor
が、呼び出されCustomDateEditor
ませんでした。
CustomDateEditor
以下のコントローラーのメソッドの最後のコメント行で示されているように、呼び出しからフィールド名を削除し、binder.registerCustomEditor
すべての日付フィールドに対してグローバルにする場合にのみ機能します。initBinderAll()
誰かが私を正しい方向に向けてくれることを願っています。
user.jsp :-
<form:form action="/some_url" modelAttribute="user">
<input type="text" id="registrationDate" name="registrationDate" value="${regDate}" />
</form:form>
User.java :-
@Entity
@Table(name="user")
public class User {
@Column(name = "reg_date")
@Temporal(TemporalType.DATE)
private Date registrationDate;
}
UserController.java :-
@Controller
public class UserController {
@RequestMapping(value = "/some_url", method = RequestMethod.POST)
public String handleSubmission(
@ModelAttribute("user")@Valid User user, BindingResult result) throws IOException {
}
@InitBinder
public void initBinderAll(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, "user.registrationDate", new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), false));
binder.registerCustomEditor(Date.class, "User.registrationDate", new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), false));
binder.registerCustomEditor(Date.class, "registrationDate", new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), false));
//binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), true));
}
}