创建BaseController 其他Controller继承它
1.在每次进controller时使用 @ModelAttribute 可以在controller之前执行方法
比如用户验证操作
/**
* 每次访问controller都执行
* @Title: show
* @param request
* @return void
* @throws
*/
@ModelAttribute
public void show(HttpServletRequest request, HttpServletResponse response,String jurisdiction) throws Exception {
if (jurisdiction != null && jurisdiction.equals("1")) {
} else {
// 获取Session对象
HttpSession session = request.getSession();
if (session.getAttribute("user") == null) {
response.sendRedirect("/");
return;
}
}
2.将前台传递过来的日期格式的字符串,自动转化为Date类型
@InitBinder 是该匹配Controller的请求执行映射的方法之前
webDataBinder是用于表单到方法的数据绑定的
/**
* 将前台传递过来的日期格式的字符串,自动转化为Date类型
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
// Date 类型转换
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(DateUtils.parseDate(text));
}
});
}
```