我正在尝试制作一个将发布 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 并向 ModelMap 添加一个空的 CardRequestResource:
@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 是非空的,但其中的每个字段都是空的!
处理程序代码:
@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;
}