1.监听器
主要是监听行为:
(1) 创建和销毁
(2) 添加属性 移除属性 替换属性
应用的时候: ServletContext/HttpSession/HttpServletRequest
启动的时刻:
2.过滤器: 非常非常重要的概念[过滤器链]
过滤器当中 chain.doFilter()方法,访问下一个过滤器或者是下一个资源
url-pattern:自己定义过滤的规则
/* :代表过滤所有的资源[JSP/Servlet/JS/CSS/图片等]
*.jsp
--------------------------------------------------------------------------------------------------------
3.拦截器: 就相当于过滤器
拦截器有且只能拦截Controller
官方解释:
Interceptors located in the handler mapping must implement HandlerInterceptor from the org.springframework.web.servlet package. This interface defines three methods:
preHandle(..) is called before the actual handler is executed;
postHandle(..) is called after the handler is executed;
afterCompletion(..) is called after the complete request has finished.
These three methods should provide enough flexibility to do all kinds of preprocessing and postprocessing.
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截访问Controller的路径映射 -->
<mvc:mapping path="/sys/**"/>
<!-- 自定义拦截器 -->
<bean id="myInterceptor" class="com.shxt.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
Element : interceptors
The ordered set of interceptors that intercept HTTP Servlet Requests handled by Controllers.
Interceptors allow requests to be pre/post processed before/after handling. Each interceptor must
implement the org.springframework.web.servlet.HandlerInterceptor or
org.springframework.web.context.request.WebRequestInterceptor interface. The interceptors in this
set are automatically detected by every registered HandlerMapping. The URI paths each interceptor
applies to are configurable.
Content Model : ((bean | ref) | interceptor)+ //至少出现一次
Element : interceptor
Registers an interceptor that interceptors requests sent to one or more URI paths.
Content Model : (mapping+, exclude-mapping*, (bean | ref)) 标签有先后顺序
mapping+代表至少有配置一个拦截路径
exclude-mapping*代表配置0或多个排除路径
(bean | ref) 代表必须拥有一个bean或者ref标签
<mvc:mapping path=""/>
Attribute : path
A path into the application intercepted by this interceptor. Exact path mapping URIs (such as "/ myPath") are supported as well as Ant-stype path patterns (such as /myPath/**).
Data Type : string
<mvc:mapping path="/sys/**"/> 代表后面可以后任意个路径
<mvc:mapping path="/sys/*"/> 只能代表有一个路径
<mvc:exclude-mapping path="/sys/shxt"/>
<mvc:exclude-mapping path="/sys/test/xy37"/> 排除路径的写法
Session拦截器的示例
@Controller
public class LoginController {
private UserService userService = new UserServiceImpl();
@RequestMapping("/sys/login")
public ModelAndView login(String account,String password,HttpServletRequest request){
ModelAndView mav = new ModelAndView();
try {
User user = userService.login(account, password);
HttpSession session = request.getSession();
session.setAttribute("session_user", user);
//从一个Controller跳转到另一个Controller,建议使用重定向路径方式
mav.setViewName(InternalResourceViewResolver.REDIRECT_URL_PREFIX+"/sys/test");
} catch (Exception e) {
e.printStackTrace();
mav.addObject("account", account);
mav.addObject("message", e.getMessage());
mav.setViewName("jsp/login");
}
return mav;
}
@RequestMapping("/sys/test")
public ModelAndView test(){
ModelAndView mav = new ModelAndView();
mav.setViewName("jsp/main/main");
return mav;
}
@RequestMapping("/chart")
@ResponseBody
public List<Map<String,Object>> chart(){
return userService.chart();
}
}
<!-- 配置拦截器 -->
<mvc:interceptors>
<!-- Session拦截器的配置 -->
<mvc:interceptor>
<!-- 设置拦截路径 -->
<mvc:mapping path="/sys/**"/>
<!-- 排除拦截路径 -->
<mvc:exclude-mapping path="/sys/login"/>
<!-- 定义的Session拦截器 -->
<bean id="sessionInterceptor" class="com.hanpang.base.interceptor.SessionInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
public class SessionInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HttpSession session = request.getSession();
if(session.getAttribute("session_user")==null){//判断Session是否存在
request.setAttribute("message", "您需要登录才能进行访问");
//返回到登录页面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;//结束不再执行
}else{
return true;//继续访问下一个拦截器的preHandle方法或者Controller
}
}
}