springspring-roo

Spring MVC Homepage Controller


I have generated some pages using Spring Roo, and I need to show some items from database on the homepage of the application. So how can I pass model with data to homepage, Roo has added following lines which I have commented out

    <mvc:view-controller path="/" view-name="index"/>

I created a new controller using Roo called Home like this

    @RequestMapping("/")
    @Controller
    public class Home {
            @RequestMapping(method = RequestMethod.GET)
            public String index() {
                return "index";
            }
    }

Now all the pages show Index view, even /login .

Regards and thanks.


Solution

  • Try this:

    Don't map to the "/" url, it will match all requests (as you have seen). Edit: According to comments, the highlighted part of the previous sentence is wrong. The workaround appears to still be valid.

    1. In your web.xml file, configure a welcome file (perhaps index.html).
    2. Use the index file name the @RequestMapping of your index controller (perhaps "/index.html").

    Another option if that is not sufficient

    1. In your web.xml file, configure a JSP welcome file (perhaps index.jsp).
    2. In your JSP welcome file, forward the request to a known URL (maybe /blammy).
    3. Map your index controller to the known URL (ex. @RequestMapping("/blammy")).