0

EDIT:

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

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.

Original Question:

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

Snippet:

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!");
            });
        }

Controller:

    @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());
}

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

Harry
  • 546
  • 6
  • 22
  • 50

1 Answers1

1

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

First, avoid stringification of dataObject:

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

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

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

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();
});

I've created a fiddle for the question.

See jQuery.ajax documentation.

Tomas Pinos
  • 2,812
  • 14
  • 22
  • Thanks so much. This almost works! But the issue is the javascript function doesn't get called when I just use submit. It does get called when I use onclick on the submit button. But it doesn't go to the controller!!. Also in the form what should be the value of action. Can I set it to ${addNewURL}? Please advise. Thanks – Harry Jun 19 '15 at 16:28
  • I'll mark this as correct as it solved one part of my issue. Will post another question for dateTime issue. Thanks – Harry Jun 19 '15 at 18:36
  • Thanks. I updated the answer to cover both click and submit events in the form. – Tomas Pinos Jun 19 '15 at 18:53
  • Thank you. My other issue is related as well, so not sure if I need to post one more question. Please check my edits – Harry Jun 19 '15 at 19:29