第七节 Spring MVC

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
}
}
 
}


 


转载于:https://my.oschina.net/courage123/blog/662012

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值