Results 1 to 6 of 6

Thread: Spring MVC and ajax requests

  1. #1
    Join Date
    Apr 2012
    Posts
    13

    Default Spring MVC and ajax requests

    hello guys I'm using Spring 3.1 MVC and I have a simple search form:

    HTML Code:
        <form:form id="formSearch" method="post" modelAttribute="searchCriteria" >
            <div id="freeflowlabel">   
                <p style="text-align:left;">
                    <span style="font-family:Arial;font-size:16px;font-weight:bold;font-style:normal;text-decoration:none;color:#333333;">${configProperties['cat.search.title']}</span>
                </p>
            </div>       
            <form:input id="freeFlowInputId" path="freeFlowText" />
            <p></p>
            <div id="advancedSearch">
    
            </div>
            <!--input id="searchButtonId" type="submit" value=${configProperties['cat.search.button']} style="cursor: pointer;"-->
    
            <button id="loadDataButton" onClick="loadData()">Search</button>
        </form:form>
    If I use the button submit (now commented) all is working well and in the server side I have the Model Attribute SearchCriteria containing the input inserted by the user in the form.

    Since I would like to search and show a table I would like to update just the table doing an ajax request and this ajax request is executed inside the methos loadData() attached to the search button. In this case I tried three ways:

    1) form.submit() nothing happen, on server side the bean has null attributes

    2) I'm using YUI as form so I tried to serialize a form but I receive an error: application/x-www-form-urlencoded is not supported

    3) I can do a serialization on JSON and send the form value in the body of the ajax POST request; this works but it's not so elegant and then I'm going to lose all the facility provided by form:tags....

    How can I solve this problem?

    I think that should be a relevant problem the interaction between Ajax and spring so I hope that there is a clean solution

    thank you
    Daniele Ippoliti

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    If the form submit submits null values your submit is wrong (i.e. your javascript function is wrong).

    I don't now YUI good enough to give a solution for that but the exception you get is you are propably issuing a GET request whereas your controller is for POST request. So switch the mechanism.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2012
    Posts
    13

    Default

    Hello,

    I checked and I'm doing a POST request... the exception that I'm receiving is:

    Code:
    org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
            at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConve
            at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConve
            at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:71
            at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
            at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
            at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
            at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
            at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
            at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
            at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
            at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
            at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
            at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
            at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
            at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
    the POST request is this:
    Post.jpg

    and in the header I have

    Content-Type application/x-www-form-urlencoded; charset=UTF-8

    it's the format that YUI or jquery or whatever serialize form...

    thanks
    Daniele

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Then fix the way you post the form. As it is expected that the BODY contains everything NOT the URL (which is what is happening now). So I suggest check the docs for the JS library you use on how to change it. If that isn't what you want then do a normal form submit and don't change the controller to have a @RequestBody annotation in the signature.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Apr 2012
    Posts
    13

    Default

    Quote Originally Posted by Marten Deinum View Post
    Then fix the way you post the form. As it is expected that the BODY contains everything NOT the URL (which is what is happening now). So I suggest check the docs for the JS library you use on how to change it. If that isn't what you want then do a normal form submit and don't change the controller to have a @RequestBody annotation in the signature.
    I'm checking the doc, but my scope is have a search form with a button that do an ajax request on server side because I need to update just the result table and not all the page, so iI have to call a javascript function to do the ajax call and in the ajax call I'm trying to pass the form search criteria, then if I do in the javascript function myForm.submit() I will have the reload of the page that is what I want to avoid...

    thank you
    Daniele

  6. #6
    Join Date
    Apr 2012
    Posts
    13

    Default

    Quote Originally Posted by dany6883 View Post
    I'm checking the doc, but my scope is have a search form with a button that do an ajax request on server side because I need to update just the result table and not all the page, so iI have to call a javascript function to do the ajax call and in the ajax call I'm trying to pass the form search criteria, then if I do in the javascript function myForm.submit() I will have the reload of the page that is what I want to avoid...

    thank you
    Daniele

    and what's about Spring.remoting.submitForm( have you ever used it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •