Controller for views in root


Yesterday while developping ukazbobra I realized, that I don’t know how to map controller for my index view in root of Spring MVC project. I searched and experimented a bit and here is a solution. Please note I’m using Spring Roo and annotation driven setup.

First thing is to create controller, for example simple one like this:

[code language=“java“]@RequestMapping("/")
@Controller
public class RootController {

@RequestMapping(method = RequestMethod.GET)
public String index(Model model) {
model.addAttribute("foo", "bar");
return "index";
}
. . .
[/code]

Now you have to turn off thing named ParameterizableViewController, which staticaly selects a view for for rendering. So open up webmvc-config.xml and remove line with mapping for index view:

[code language=“xml“]<mvc:view-controller path="/" view-name="index"/>[/code]

That is all, nothing more, nothing less – you have working controller for your index page in root and also for other views.


Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *