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?
The convention used by Spring MVC is to use the name of the class and remove the “Controller” suffix, then change the name to a lower case and return it as the mapping with a leading “/”. For example “WelcomeController” would return as mapping to “/welcome*”, i.e. to any URL that starts with “welcome”.
@PanadolChong accomodate Spring MVC is still used in some legacy applications, and Spring boot does majority of the configurations under-hood so to have a better understanding of how things work internally, Spring MVC might help.
The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With