Bal
Bal

Reputation: 2087

Spring MVC 3.0 - restrict what gets routed through the dispatcher servlet

I want to use Spring MVC 3.0 to build interfaces for AJAX transactions. I want the results to be returned as JSON, but I don't necessarily want the web pages to be built with JSP. I only want requests to the controllers to be intercepted/routed through the DispatcherServlet and the rest of the project to continue to function like a regular Java webapp without Spring integration.

My thought was to define the servlet-mapping url pattern in web.xml as being something like "/controller/*", then have the class level @RequestMapping in my controller to be something like @RequestMapping("/controller/colors"), and finally at the method level, have @RequestMapping(value = "/controller/colors/{name}", method = RequestMethod.GET).

Only problem is, I'm not sure if I need to keep adding "/controller" in all of the RequestMappings and no matter what combo I try, I keep getting 404 requested resource not available errors.

The ultimate goal here is for me to be able to type in a web browser "http://localhost:8080/myproject/controller/colors/red" and get back the RGB value as a JSON string.

Upvotes: 1

Views: 708

Answers (2)

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

You are not correct about needing to add the entire path everywhere, the paths are cumulative-

If you have a servlet mapping of /controller/* for the Spring's DispatcherServlet, then any call to /controller/* will be handled now by the DispatcherServlet, you just have to take care of rest of the path info in your @RequestMapping, so your controller can be

@Controller
@RequestMapping("/colors")
public class MyController{

 @RequestMapping("/{name} 
 public String myMappedMethod(@PathVariable("name") String name, ..){
 }

}

So now, this method will be handled by the call to /controller/colors/blue etc.

Upvotes: 2

Peter Bratton
Peter Bratton

Reputation: 6408

I don't necessarily want the web pages to be built with JSP

Spring MVC offers many view template integration options, from passthrough to raw html to rich templating engines like Velocity and Freemarker. Perhaps one of those options will fit what you're looking for.

Upvotes: 1

Related Questions