1:ModelAndView
@RequestMapping("/viewall")
public ModelAndView viewAll(String name,String pwd,HttpServletRequest request){
ModelAndView mv= new ModelAndView();
request.getSession().setAttribute("user", name);
mv.setViewName("/hello1");
mv.addObject("msg","恭喜 "+name+" 登录成功!");
return mv;
}
2:配置请求过滤器,编码格式设为UTF-8,避免中文乱码
<!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
<filter>
<filter-name>springUtf8Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springUtf8Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3:编写拦截器 实现HandlerInterceptor接口
public class Test1Interceptor implements HandlerInterceptor{
private Logger log = LoggerFactory.getLogger(Test1Interceptor.class);
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
log.debug("执行到了afterCompletion1方法");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView mv)
throws Exception {
//可以通过ModelAndView参数来改变显示的视图,或修改发往视图的方法。
log.debug("执行到了postHandle1方法");
// mv.addObject("msg","这里传回的是被拦截器修改之后的消息!");
// mv.setViewName("/hello2");
}
/**
* 返回值:表示我们是否需要将当期的请求拦截下来
* false:请求会被终止
* true:请求会被继续运行
* Object object:表示的是被拦截的请求的目标对象
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
编码格式设为UTF-8,避免中文乱码
// request.setCharacterEncoding("UTF-8");
log.debug("执行到了preHandle1方法");
String path = request.getServletPath();
if("/login".equalsIgnoreCase(path)){
return true;
}
if("/viewall".equalsIgnoreCase(path)){
return true;
}
if(request.getSession().getAttribute("user") == null){
//如果用户没有登录,就终止请求,并发送到登录页面
request.getRequestDispatcher("/login").forward(request, response);
return false;
}
return true;
}
}
4:注册拦截器
<!-- 注册拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/list"/>
<bean class="com.com.interceptor.TestInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
5:多个拦截器执行顺序
6:WebRequestInterceptor 实现拦截器 向mvc中注册 和上面一样,不同的是preHandle不会反悔
public class threeInterpector implements WebRequestInterceptor {
public void preHandle(WebRequest request) throws Exception {
// TODO Auto-generated method stub
request.setAttribute("username", "zhang",WebRequest.SCOPE_REQUEST);
}
public void postHandle(WebRequest request, ModelMap model) throws Exception {
// TODO Auto-generated method stub
System.out.println(request.getAttribute("username", WebRequest.SCOPE_REQUEST));
}
public void afterCompletion(WebRequest request, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
7:拦截器和过滤器
1.过滤器是JavaEE标准,采用函数回调的方式进行。是在请求进入容器之后,还未进入Servlet之前进行预处
理,并且在请求结束返回给前端这之间进行后期处理。依赖于servlet容器k=可以过滤静态资源文件
2.拦截器依赖于框架容器,基于反射机制,只过滤请求