【问题标题】:Spring Portlet Jquery Ajax post to ControllerSpring Portlet Jquery Ajax 发布到控制器
【发布时间】:2015-06-18 17:46:41
【问题描述】:

编辑:

开始日期和结束日期是 POJO 中的 joda dateTime,我得到的错误是:

SystemOut     O 14:10:16.040 [WebContainer : 2] DEBUG org.springframework.beans.BeanUtils - No property editor [org.joda.time.DateTimeEditor] found for type org.joda.time.DateTime according to 'Editor' suffix convention
...
SystemOut     O Error::Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'startTimestamp'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.joda.time.DateTime] for property 'startTimestamp': no matching editors or conversion strategy found

我也无法编辑 Pojo 并添加 @DateTimeFormat,因为 Pojo 是从 XSD 生成的。我也尝试添加一个 customObjectMapper,但没有任何效果。任何帮助将不胜感激。

原问题:

我正在尝试提交表单并将数据发送到 Controller 方法。问题是 ModelAttribute 为空且没有值。 Spring MVC Portlet + Jsp + Javascript + Jquery + Controller @ResourceMapping

片段:

JSP:

<portlet:resourceURL id="addNewURL" var="addNewURL">
    </portlet:resourceURL>  

<form:form id="qmat_new_notification_form" action="#" method="POST" modelAttribute="dataObject">    
  ...
  <input type="text" class="date-picker" id="start_date">
  ...    
  <input type="submit" value="Save" class="button" onclick="addNew()">    
</form:form>

jquery:

function addNew() { 

            var dataObject = JSON.stringify({
                'startTime': $('#start_date').val(),
                'endTime': $('#end_date').val(),
                'description': $('#message').val(),
                'active': $('#status').val()
            });

            alert("data::"+dataObject);

            $.ajax({
                url: "<%=addNewURL%>",
                type: 'POST',
                contentType: 'application/json',
                data: dataObject
            }).done(function(json){         
alert("Success!");
//more logic    
            }).fail(function() {
                alert("OOPS!");
            });
        }

控制器:

    @ResourceMapping(value = "addNewURL")
        public void addNew(@ModelAttribute(value = "dataObject") Obj n,
                            BindingResult bindingResult, ResourceRequest request, ResourceResponse response, ModelMap model) {

            if (!bindingResult.hasErrors()) {
                System.out.println("a:::"+n.getDescription());
}

此 getDescription 为空。另外,如果我使用 request.getParameter("description") 也是空的。我错过了什么?请帮忙

【问题讨论】:

  • 哪个门户网站? Liferay 6.2?
  • Websphere Portal Server 7.0.0.2。感谢询问

标签: javascript jquery json spring-mvc spring-portlet-mvc


【解决方案1】:

您根本不需要使用 JSON 数据。

首先,避免dataObject的字符串化:

var dataObject = {...}; // no JSON.stringify call

其次,删除contentType: 'application/json',因为在这种情况下它没有意义。

dataObject 是键/值对,默认 contentType,POST 请求将被正确构造。

要同时处理点击和提交事件,我建议使用 jQuery 的点击和提交方法:

$("#submit").click(function (event) {
    addNew();
    event.preventDefault();
});
$("#submit").submit(function (event) {
    addNew();
    event.preventDefault();
});

我为这个问题创建了一个fiddle

请参阅jQuery.ajax 文档。

【讨论】:

  • 非常感谢。这几乎可以工作!但问题是当我只使用提交时不会调用 javascript 函数。当我在提交按钮上使用 onclick 时,它会被调用。但它不会去控制器!还有形式上应该是什么行动的价值。我可以将其设置为 ${addNewURL} 吗?请指教。谢谢
  • 我会将其标记为正确,因为它解决了我的问题的一部分。将发布另一个关于 dateTime 问题的问题。谢谢
  • 谢谢。我更新了答案以涵盖表单中的点击和提交事件。
  • 谢谢。我的另一个问题也相关,所以不确定我是否需要再发布一个问题。请检查我的编辑
猜你喜欢
  • 2017-12-09
  • 1970-01-01
  • 2017-05-27
  • 2012-12-30
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多