Is it possible to define things externally into Properties files for our annotations on Spring Controllers?
Suppose I have the following Controller
@Controller
@RequestMapping(value = "processModel.jsp")
public class ProcessorController {
@RequestMapping(method = RequestMethod.GET)
public String displayModel() {
//Code to load processor
return "processModel";
}
@RequestMapping(method = RequestMethod.POST, params="submit=Refresh")
public String refreshModel() {
//Code to refresh data
return "processModel";
}
@RequestMapping(method = RequestMethod.POST, params="submit=Save Model")
public String saveModel() {
//Code to save model
return "processModel";
开发者_如何学运维}
}
Assume the following HTML is generated:
<input type="submit" name="submit" value="Save Model" />
<input type="submit" name="submit" value="Refresh" />
It'd be nice to have these params externalized so that we only have to define them once in a properties file. That way if we need to change the label on a submit button in the JSP, we only need to change it in the properties file, rather than in two places.
Is this possible?
Annotation parameter values need to be a literal or refer to a constant field, so the externalized dynamic value cannot be injected into the @RequestMapping annotation. An alternative may be to drive the behavior using some other hidden form variable mapped to a literal(SAVEMODEL/REFRESH) instead of the text that is displayed to the user(you may get a requirement to internationalize the text displayed to the user at some point and this model will break then)
You may also create a modelAndView methods in your controller and you don't have to put a @requestMapping on the head of the controller. So your controller become something like this:
@Controller
public class ProcessorController {
@RequestMapping(value="getView.html" method = RequestMethod.GET)
public ModelAndView displayModel(HttpServletRequest request) {
ModelAndView mav = new ModelAndView();
//Code to load processor
mav.setViewName = "processModel";
return mav;
}
@RequestMapping(value="refreshModel.html" method = RequestMethod.POST)
public ModelAndView refreshModel(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
//Code to refresh data
mav.setViewName="refreshModel";
return mav;
}
@RequestMapping(value="saveModel.html" method = RequestMethod.POST)
public String saveModel(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
//Code to save model
mav.setViewName="saveModel";
return mav;
}
}
After, you just have to create the three jsp files (saveModel.jsp,refreshModel.jsp,processModel.jsp) and you have 3 views in one controller. And thats all
精彩评论