Reputation: 4180
I have a spring 3.1 web application and am trying to make an ajax request, but in firebug I can see my mapping isn't found. I tried various things, but am not able to just reach my controller method. Here are some details:
this I have i my web.xml:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/ctrl/*</url-pattern>
</servlet-mapping>
this I have in my controller:
@RequestMapping(value="/ctrl/test", method = RequestMethod.GET)
public @ResponseBody String test() {
System.out.println("method test()");
return "aString";
}
this I have in my dispatcher-servlet.xml:
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
I have another request mapped to a method in my controller and that one is working, but it's not ajax:
@RequestMapping(value="/ctrl", method = RequestMethod.GET)
public String printWelcome(ModelMap model, HttpSession session) {
initializeTree(session);
return "tree";
}
Anyone can point me in the right direction ?
Upvotes: 4
Views: 3708
Reputation: 4180
I found a solution:
in my web.xml:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/ctrl/*</url-pattern>
</servlet-mapping>
in my controller:
@RequestMapping(value="/test", method = RequestMethod.GET)
public String test(ModelMap model, HttpSession session) {
System.out.println("method test()");
return null;
}
On the client side, I use jQuery:
$.get(
"ctrl/test/",
function(data){
alert(data);
});
I can build on this; my System.out.println is getting printed, so I can reach my controller now
Upvotes: 0
Reputation: 18639
I'm always using ajax based links as *.action or *.ajax for AJAX based requests and *.html mapping for jsp views. Then your dispatcher Servlet should be the following
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.ajax</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
next Step:
if all your ajax commands URI starts with /ctrl I recommend you the following:
@Controller
@RequestMapping(value="/ctrl")
public class AjaxController{
@RequestMapping(value="/test.ajax", method = RequestMethod.GET)
public String test(ModelMap model, HttpSession session) {
System.out.println("method test()");
return "test";
}
}
Don't Forget to add @ResponseBody
annotation to your ajax based mapping.
now your view controller for index.html should be the following:
@Controller
public class ViewController{
@RequestMapping(value="/index.html", method = RequestMethod.GET)
@ResponseBody
public String test(ModelMap model, HttpSession session) {
System.out.println("method test()");
return "index";
}
}
Upvotes: 2
Reputation: 35008
Your dispatcher servlet is not mapped for /ctrl/test
, so it won't get invoked for the AJAX controller.
Change your servlet-mapping
to
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/ctrl/*</url-pattern>
</servlet-mapping>
So that the dispatcher can dispatch both URLs.
For more info, this may help (Weblogic specific but applies in general): http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/components.html#148787
Upvotes: 0