I have two Spring MVC actions that in this example takes one parameter from a form when submitted:
public ModelAndView login(HttpServletResponse response, HttpServletRequest request,
@RequestParam String requestedURL )
I would like to know if the attribute requestedURL can refer to a declared variable that actually hold the name of the incoming attribute input name="requestURL" ...
class Core {
static String requestedURL = "requestedURL";
}
pseudo code:
public ModelAndView login(..., @RequestParam String @ReadFrom(Core.RequestedURL) )
Notice the @ReadFrom
This is to avoid redundancy. Right now it is called requestedURL but in the future someone might want to change the input parameter name, this shouldn't be a hardcoded string in the applicaton in my opinion.
and
<input name="<%= Core.requestedURL %>" value="<%= requestedURL %>" />
and is read in the method when submitted. But does the attribute name have to be hardcoded in the incoming parameter of the action method?
Thanks!
Yes, it has to be hardcoded as part of the @RequestParam annotation - either hardcoded or refer to a static final
variable.
The alternative is to take in a Model/Map as an additional parameter in the method and getting the attribute from that:
public ModelAndView login(HttpServletResponse response, HttpServletRequest request,
Model model ){
String requestedURL = model.asMap().get(Core.requestedURL);
}
Update You can refer to a static final variable this way: assuming:
public abstract class Core {
public static final String requestedURL = "requestedURL";
}
public ModelAndView login(..., @RequestParam(Core.requestedURL) String requestedURL)