Spring MVC ModelAttribute字段为空,而不是表单输入值

我正在尝试制作将发布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为非null,但其中的每个EVERLE字段都是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;

}

回答:

所以我想我找到了perp!

这个小家伙就在POST AJAX呼叫中:

contentType: "application/json",

我最初是将JSON发送到控制器,然后了解到Spring

MVC具有漂亮的ModelAttribute批注,因此我不必担心在POST期间向JSON进行转换。不幸的是,我不打算删除该contentType行,而会认为它会引发错误或其他错误……显然不会。绑定中只有NULL值,无数小时的调试…

删除该行之后,我开始在CardRequest属性中获取数据,而不是NULL!

以上是 Spring MVC ModelAttribute字段为空,而不是表单输入值 的全部内容, 来源链接: utcz.com/qa/425893.html

回到顶部