8

我正在尝试制作一个将发布 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;
}
4

2 回答 2

11

所以我想我找到了perp!

这个小家伙就在 POST AJAX 调用中:

contentType: "application/json",

我最初将 JSON 发送到我的控制器,然后我了解到 Spring MVC 有那个漂亮的 ModelAttribute 注释,所以我不必担心在我的 POST 期间转换为 JSON 或从 JSON 转换。不幸的是,我没有考虑删除该 contentType 行,并且有人会认为它会引发错误或仍然存在的东西......显然不是。只是绑定中的 NULL 值和无结果的调试时间......

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

于 2013-08-02T12:28:24.013 回答
0

尝试将以下代码放在 aplication-context.xml 中。如果您有您的表格,这可能是解决方案encType='multipart/form-data'

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
于 2013-12-06T13:46:11.100 回答