Spring Portlet Jquery Ajax发布到Controller

编程入门 行业动态 更新时间:2024-10-26 09:23:25
本文介绍了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

本文发布于:2023-10-07 17:25:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470016.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Portlet   Spring   Jquery   Controller   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!