Spring Portlet Jquery Ajax发布到Controller [英] Spring Portlet Jquery Ajax post to Controller

查看:130
本文介绍了Spring Portlet Jquery Ajax发布到Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The startdate and enddate are joda dateTime in the POJO and the error I get is:

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,但是没有任何效果.任何帮助将不胜感激.

I also can't edit the Pojo and add @DateTimeFormat because the Pojos are generated from XSD. I also tried adding a customObjectMapper, but nothing works. Any help would be much appreciated.

原始问题:

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

I'm trying to submit a form and send the data to the Controller method. The issue is the ModelAttribute is empty and does not have the values. 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")也为null.我想念什么?请帮助

This getDescription is null. Also if I use request.getParameter("description") is also null. What am I missing? Please help

推荐答案

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

You don't need to work with JSON data at all.

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

First, avoid stringification of dataObject:

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

第二,删除contentType: 'application/json',因为在这种情况下这没有意义.

Second, remove contentType: 'application/json' as it doesn't make sense in this case.

使用dataObject作为键/值对并使用默认的contentType,可以正确构造POST请求.

With dataObject being a key/value pair and default contentType, the POST request will be constructed correctly.

要处理单击和提交事件,我建议jQuery单击和提交方法:

To handle both click and submit events, I suggest to jQuery click and submit methods:

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

我为问题创建了小提琴.

请参见 jQuery.ajax 文档.

这篇关于Spring Portlet Jquery Ajax发布到Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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