Application Routes with Spring MVC

26,132

Solution 1

I found a solution, as follows:

1) I create a Routes class

public class Routes {

    private static HashMap<String, String> routes;

    public static final String host = "/mywebapp";
    public static final String home = "/home";
    public static final String login = "/login";
    public static final String logout = "/logout";

    private static void setRoutes()
    {       
        if(routes == null)
        {
            routes = new HashMap<String, String>();

            routes.put("host", host);
            routes.put("home", host + home);
            routes.put("entrar", host + entrar);
            routes.put("sair", host + sair);
        }
    }   

    public static HashMap<String, String> getRoutes()
    {
        setRoutes();

        return routes;
    }

    public static String getRoute(String destin)
    {
        setRoutes();

        return routes.get(destin);
    }

}

2) I use on my Controller... Now it's possible set a RequestMapping

@Controller
public class HomeController extends AbstractController {

    @RequestMapping(Routes.home)
    public String home(ModelMap model)
    {
        preRender(model);       
        return Routes.home;
    }

}

3) I set Routes to use on my views

public abstract class AbstractController {

    protected void preRender(ModelMap model) {
        model.addAttribute("routes", Routes.getRoutes()); 
    }

}

4) And it's available now to use on the views

<body>
    <p>Mary is singing.</p>
    <p><a href="${routes.home}">Home</a></p>
</body>

Solution 2

check out the SpringMVC router project on GitHub:

https://github.com/resthub/springmvc-router

it is an implementation of the play's routes file for SpringMVC

Share:
26,132
claudioivp
Author by

claudioivp

Updated on October 23, 2020

Comments

  • claudioivp
    claudioivp over 3 years

    I'm looking for a way to manage the route's use on my WebApp. Basically, I have three places that could share a router pattern. And I could send this pattern to use on my views, through expression language.

    @Controller
    public class LoginRuasController
    {
        @RequestMapping("/system/index")
        public String logout(ModelMap model, HttpSession session)
        {
            return "system/index";
        }   
    
        @RequestMapping("/system/logout")
        public String logout(ModelMap model, HttpSession session)
        {
            session.setAttribute("xxx", null);
            return "redirect:/system/login";
        }
    }
    

    patterns:

    /system/index
    system/index
    redirect:/system/login
    

    views:

    <a href="#{Routes.newuser}">Triple X</a>
    

    Initially, RequestMapping requests a constant value, so this generate a problem to implement a Route class with static return. Is there any solution available?

  • Tim Bender
    Tim Bender about 8 years
    "latest commit February 2015"
  • UrLicht
    UrLicht about 8 years
    Old question, I know, but for future searchers: you can also use a combination of @ControllerAdvice and @ModelAttribute to add a reference to the routes class to the model associated with every controller.
  • claudioivp
    claudioivp over 7 years
    do you have an example?