平台:Spring 3.1.2,Tomcat 7.0.30
从3.0升级到Spring 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();
}
我捕获的方式是通过Controller,它位于一个抽象的类中,如下所示:
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 bind在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序列化序列化了表单并检查了它,并且没有逗号。然后ajax将这些数据发布到服务器,并在@ModelAttribute完成它的工作后,将附加逗号。
有什么想法吗?使用Init binder的东西?
答案 0 :(得分:17)
我遇到了同样的问题,但Twitter Bootstrap不是原因。
就我而言,我们有2个具有相同名称属性的输入。在这种情况下,这两个输入的数据将作为数组发送。由于我的输入为空,因此数组为myName=,
答案 1 :(得分:-2)
问题孤立。似乎Twitter Bootstrap正在创建问题。表格中有一个标签。我把标签放在表格之外,问题就解决了。非常奇怪。