Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC and ajax requests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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:
      Attachment

      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
      Attached Files

      Comment


      • #4
        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.

        Comment


        • #5
          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

          Comment


          • #6
            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?

            Comment

            Working...
            X