我正在尝试制作一个发布CardRequestResource的表单:
public class CardRequestResource extends ResourceSupport{
private Long cardRequestId;
private String deliveryMethod;
private String address;
private boolean isHomeDelivery;
private String requestDate;
private String expectedDate;
private String comments;
private Long statusId;
private PersonResource person;
//Getters and Setters
}
在我的控制器中,我首先加载JSP并将空CardRequestResource添加到ModelMap:
@RequestMapping(value = { "", "/approval" }, method = RequestMethod.GET)
public String getApproval(ModelMap map) {
map.put("cardRequestResource", new CardRequestResource());
return "cardoffice/approval";
}
我的JSP使用cardRequestResource模型属性构建表单:
<form:form id="detailForm" name="detailForm" modelAttribute="cardRequestResource">
<form:input path="statusId" type="hidden" id="statusId" name="statusId" />
<form:textarea path="comments" name="comments" id="commentTextarea" rows="7" cols="81" style="font-style: normal;"/>
</form:form>
Javascript函数进行AJAX调用以填充表单值:
function getCardRequestDetails(cardRequestUrl) {
$.ajax({
type : "GET",
url : cardRequestUrl,
dataType : "json",
success : function(response) {
loadCardRequestDetails(response);
},
error : function(response) {}
});
};
function loadCardRequestDetails(cardRequest) {
$("#statusId").attr("value", cardRequest.statusId);
$("#commentTextarea").val(cardRequest.comments);
}
此时,用户可以更新评论文本区域,并且隐藏的输入可以有条件地改变用户在该字段中输入的内容。然后在提交表单时,我调用以下Javascript函数:
function postCardRequest(url) {
var serialized = $("#detailForm").serialize();
alert(serialized);
$.ajax({
type: "POST",
url: url,
data: serialized,
contentType: "application/json",
dataType: "json"
});
}
警报显示字段使用最初由AJAX / Javascript或用户加载的数据正确填充。但是,当我到达处理帖子的控制器中的处理程序时,CardRequestResource是非空的,但是其中的EVERY SINGLE字段为NULL!
处理程序代码:
@RequestMapping(value = "/approval/submit", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> postCardRequest(@ModelAttribute(value = "cardRequestResource") CardRequestResource cardRequestResource) {
Map<String, Object> responseMap = new HashMap<String, Object>();
final String success = "success";
Boolean isSuccessful = Boolean.FALSE;
if(cardRequestResource != null){
isSuccessful = Boolean.TRUE;
}
//TODO
System.out.println("Status: " + cardRequestResource.getStatusId() + ", Comments: " + cardRequestResource.getComments());
responseMap.put(success, isSuccessful);
return responseMap;
}
答案 0 :(得分:9)
所以我觉得我找到了perp!
这个小伙伴就在POST AJAX电话中:
contentType: "application/json",
我最初是将JSON发送给我的控制器,然后我了解到Spring MVC有那个漂亮的ModelAttribute注释,所以我不必担心在POST期间转换为/从JSON转换。不幸的是,我没有考虑删除那个contentType行,并且人们会认为它会抛出错误或其中的某些东西仍在那里......显然不是。绑定中的NULL值和无效调试的小时数......
删除该行后,我开始在CardRequest属性中获取数据而不是NULL!
答案 1 :(得分:0)
尝试将以下代码放在application-context.xml中。如果您使用表单encType='multipart/form-data'
,这可能是解决方案。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>