0

I have an AJAX call which is processed and a jsp page is returned by Spring.

@RequestMapping(value = "/getData.do")
public String getData() {
    // ...
    return "data";
}

and I am returning data.jsp.

The AJAX call looks like this:

$.ajax({
    type : 'GET',
    url : "getData.do",

    error : function() {
        alert('failure');
    },
    success : function(result) {
        alert("Success");
    }
});

I have servlet mapping in web.xml for .do files

  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

OK, so when I have this <bean> definition in my dispatcher, it works fine.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/jsp/"/>
   <property name="suffix" value=".jsp"/>
</bean>

But, when I change it to this, it fails!

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="mediaTypes">
    <map>
      <entry key="html" value="text/html"/>
      <entry key="json" value="application/json"/>
    </map>
  </property>
  <property name="viewResolvers">
    <list>
      <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    </list>
  </property>
  <property name="defaultViews">
    <list>
      <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
    </list>
  </property>
</bean>

I need this for JSON. Any ideas?

Update : Added stacktrace

SEVERE: Servlet.service() for servlet [spring] in context with path [/Foo] threw exception [Could not resolve view with name 'getData' in servlet with name 'spring'] with root cause
javax.servlet.ServletException: Could not resolve view with name 'getData' in servlet with name 'spring'
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1190)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
1
  • @MoritzPetersen Sorry, updated... Commented Sep 3, 2013 at 7:31

1 Answer 1

1

It got fixed when I modified the Accept header in AJAX as mentioned in this post Pass accepts header parameter to jquery ajax.

Updated AJAX:

$.ajax({
    type : 'GET',
    url : "getData.do",
    headers: {          
        Accept : "text/html; charset=utf-8",         
    },    
    error : function() {
        alert('failure');
    },
    success : function(result) {
        alert("Success");
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I thought you want to get JSON? Also, see this answer
@MoritzPetersen I meant, I need the second setting in dispatcher for JSON.
@MoritzPetersen The link you provided won't work as that will only add one more level of filtering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.