Spring MVC ModelAttribute字段为空,而不是表单输入值 [英] Spring MVC ModelAttribute Fields Are Null Instead of Form Input Values

查看:413
本文介绍了Spring MVC ModelAttribute字段为空,而不是表单输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个表单,该表单将发布CardRequestResource:

I'm trying to make a form that will post a 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:

In my controller, I first load the JSP and add an empty CardRequestResource to the ModelMap:

@RequestMapping(value = { "", "/approval" }, method = RequestMethod.GET)
public String getApproval(ModelMap map) {
    map.put("cardRequestResource", new CardRequestResource());       
    return "cardoffice/approval";
}

我的JSP使用cardRequestResource模型属性构建表单:

My JSP builds the form with the cardRequestResource model attribute:

<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调用以填充表单值:

A Javascript function makes an AJAX call to populate the form values:

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函数:

At this point a user may update the comment text area, and the hidden input may change conditionally on what the user enters in the field. Then when the form is submitted, I call the following Javascript function:

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,但其中的每个SINGLE字段均为NULL!

The alert shows that the fields are populated correctly with the data that was either originally loaded by AJAX/Javascript, or by the user. However when I get to the handler in my controller that processes the post, the CardRequestResource is non-null, but EVERY SINGLE field in it is 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;
}

推荐答案

所以我认为我找到了这个家伙!

So I think I found the perp!

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

This lil' guy right here in the POST AJAX call:

contentType: "application/json",

我最初是将JSON发送到我的控制器,然后我了解到Spring MVC具有漂亮的ModelAttribute批注,因此我不必担心在POST期间向JSON进行转换.不幸的是,我不打算删除该contentType行,而会认为它会引发错误或其中仍然存在某些错误……显然不是.绑定中只有NULL值,无数小时的调试...

I was originally sending JSON to my controller, then I learned that Spring MVC had that nifty ModelAttribute annotation, so I wouldn't have to worry about converting to/from JSON during my POST. Unfortunately, I didn't think to remove that contentType line, and one would think that it would throw an error or something with it still in there... apparently not. Just NULL values in the binding and hours of fruitless debugging...

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

After I removed that line, I started getting data in the CardRequest attributes instead of NULLs!

这篇关于Spring MVC ModelAttribute字段为空,而不是表单输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆