プラットフォーム: Spring 3.1.2、Tomcat 7.0.30
この問題は、Spring 3.0 から 3.1 にアップグレードした後に発生しました。
次のような通常のスプリング対応フォームがあります。
<c:url value='/healthcare/insurance/policy/save' var="url" /> <form:form commandName="insurancePolicy" action="${url }" method="POST" cssClass="form-horizontal"> <div class="module"> <div class="header"> <strong><spring:message code="insurance.policy.view.formbox" /> </strong> </div> <div class="module-content row-fluid"> <div class="span4"> <div class="control-group"> <label class="control-label"><spring:message code="insurance.policy.view.policyId" /></label> <div class="controls"> <form:input path="policyId" cssClass="span10" readonly="true" /> </div> </div> <div class="control-group"> <label class="control-label"><spring:message code="insurance.policy.view.tpaId" /></label> <div class="controls"> <form:select path="tpaId" items="${tpas }" itemLabel="tpaName" itemValue="tpaId" cssClass="span10" /> </div> </div>
等々。
私が提出する方法は次のとおりです。
function save(){
$("#insurancePolicy").submit();
}
私がキャッチする方法は、以下のように抽象化されたクラスにあるコントローラーを使用することです。
public abstract class AbstractCrudViewController<T, PK extends Number, META extends T> extends AbstractViewController {
@SuppressWarnings("unchecked")
@RequestMapping(value="/save", method = RequestMethod.POST)
protected String save(@ModelAttribute T t, Model model, HttpServletRequest request){
logger.debug("Executing save for model: {}", t.toString());
try{
if (getPrimaryKeyValue(t) == null || ((Number) getPrimaryKeyValue(t)).intValue() <=0 ){
logger.debug("Creating new record");
PK pk = getCrudService().create(t);
logger.debug("New record created with ID: {}", pk);
ViewUtil.addSuccess(model, "crud.saveSuccess", request, getPrimaryKeyValue(t));
}
init バインダーは、AbstractViewController で次のように宣言されます。
public abstract class AbstractViewController {
@InitBinder
public void initBinder(WebDataBinder binder) {
String dateFormat = "dd/MM/yyyy";
binder.registerCustomEditor(DateTime.class, new DateTimeEditor(dateFormat,true));
}
}
わかった。問題は次のとおりです。フォームを保存していますが、ログには次のように表示されます。
DEBUG:com.keype.hawk.hr.mvc.StaffViewController[save]:Executing save for model: Staff [staffId=null, firstName=Steve,, lastName=Harris,, middleName=,, nationality=AF, dob=2012-09-11T00:00:00.000+03:00, status=1000, title=Mr, address1=, address2=, city=, state=, postalCode=, country=AF, homePhoneAreaCode=, homePhone=,, homePhoneExt=, homePhoneAllowSoliciation=, workPhoneAreaCode=, workPhone=null, workPhoneExt=, workPhoneAllowSoliciation=, fax=, mobileAreaCode=, mobile=, mobileAllowSolicitation=, email=null, joiningDate=null, gender=, govtId=, maritalStatus=, passportNumber=, passportExpiryDate=null, totalWorkExperience=, notes=, staffCreated=null, staffModified=null, dateCreated=null, dateModified=null]
各フィールドの後のコンマに注意してください。
デバッグのために、jquery serialize を使用してフォームをシリアル化し、チェックしましたが、コンマはありません。次に、このデータをサーバーにajax投稿し、 @ModelAttribute が仕事をした後、カンマが追加されます。
何か案が?Initバインダーで何か?