
Originally Posted by
titiwangsa
or you could have a properties file?
${controllerPaths['login']} ?
or something like that.
Hi titiwangsa,
I like the idea of a properties file instead of a bunch of constant string fields...However the @RequestMapping annotation requires the value of the mapping to be known at compile time (i.e. constant strings).
If the @RequestMapping annotation allows specifying a name for the mapping, we might be able to just hard-code the path into the @RequestMapping and get a map of paths from a custom HandlerMapping implementation to use in either the taglib functions or directly in the JSP. For example:
Controller
Code:
@RequestMapping(name="loginPage", value="/login", method=GET)
public String loginPage(ModelMap model) {
model.addAttribute("controllerPaths", RequestHandler.getMappedPaths());
return "login.jsp";
}
public static final String loginPage() {
return RequestHandler.getMappedPaths().get('loginPage');
}
JSP
Code:
<%@ taglib prefix="ex" uri="http://www.example.com/jsp/jstl/functions" %>
<a href="${controllerPaths('loginPage')}">Login here</a>
<a href="${ex:loginPage()}">Or here</a>
If we changed the mapping from /login to /signin, as long as we keep the name constant, the JSP links will not break. However, this only adds another layer of abstraction that does not offer any protection if the mapping is removed or the name changes (JSP links will be broken and won't be reported by compilers).
I am hoping to either:
1. Have a single place where paths are defined that is referenced by controllers and JSPs. It would be great if that same place is able to encapsulate request param data and all other request-mapping related stuff.
2. OR somehow enable checking of path references in JSPs at compile-time or jsp-compile-time. I think having a taglib that generates URLs sort of fills this capacity if the query params change.
Thanks,
Andy