Springmvc第三讲学习笔记,拦截器Interceptor的使用
来源:互联网 发布:win7精简优化版 编辑:程序博客网 时间:2024/05/22 12:07
简单登录拦截器的实现
思路:
1.jsp页面提交User对象的两个属性,name,pwd
2.拦截器实现HandlerInterceptor接口,在请求处理的方法之前执行。preHandler,postHandle,afterCompletion拦截器中拥有一个list<String>属性,用于设置允许请求的后缀,在springmvc的配置文件中使用List注入
先判断session中有没有user对象,有的话说明已经登录了,返回真,放行。
否则,判断url请求名的后缀是否可以匹配到指定的list中所允许的后缀名。
仍然没有的话,就重定向到登录页面
3.编写Controller,
接收hello.do的传递的user的name和pwd,如果是帐号密码是对的,
在session中把user放进去。
4.配置springmvc配置文件,<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**">
<bean class="MyInterceptor">
1.jsp页面:(配置了视图解析器)
/index.jsp
<body>
<form action="hello.do" method="post">
<!--提交的name和pwd直接可以匹配到user对象中的属性 -->
name:<input type="text" name="name"/>
pwd: <input type="text" name="pwd"/>
<input type="submit" value="登录"/>
</form>
</body>
/WEB-INF/hello.jsp
<body>
<h1>登录成功</h1>
姓名:${name } <br>
密码:${pwd} <br>
<a href="add.do">add.do</a>
</body>
2.拦截器的代码实现
public class LoginInterceptor implements HandlerInterceptor {
private List<String> allowRequest;//bean的List注入,需要set方法
public void setAllowRequest(List<String> allowRequest) {
this.allowRequest = allowRequest;
}
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
Object handler) throws Exception {
//先判断session会话中是否存放了user,是的话说明已经登录了
Object user = req.getSession().getAttribute("user");
if(user!=null){
return true;
}
//req.getRequestURL()返回的是一个StringBuffer(),再通过toString()方法拿到url的字符串
String url = req.getRequestURL().toString();
//从外部的bean注入的list遍历允许通过的请求名
for(String temp:allowRequest){
System.out.println("在拦截器中的allowRequest:"+temp);
//如果是允许的请求,则拦截器放行
if(url.endsWith(temp)){
return true;
}
}
//仍然没有通过登录拦截器,执行重定向,要通过req拿到全路径。req.getContextPath()拿到项目根路径
resp.sendRedirect(req.getContextPath()+"/index.jsp");
return false;//拦截!
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("Controller方法执行之后执行");
}
@Override
public void afterCompletion(HttpServletRequest req,
HttpServletResponse resp, Object handler, Exception ex)
throws Exception {
System.out.println("清理工作执行");
}
3.Controller,spring中有视图解析器
package cn.jea.controller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(User user,HttpSession session){
//设置默认接收两个参数,一个是页面的User对象,一个是服务器创建的HttpSession对象
ModelAndView mv = new ModelAndView();
mv.setViewName("index");//先设置为跳转到/web-inf/index.jsp/页面,它与/index.jsp 一模一样。
System.out.println("controller接收到name:"+user.getName());
System.out.println("controller接收到pwd:"+user.getPwd());
//如果帐号密码相同,重新设置mv的跳转的视图名称,为hello,跳转/web-inf/hello.jsp/页面下面,并封装前台的name和pwd数据
if(user.getName().equals("siggy")&&user.getPwd().equals("1234")){
session.setAttribute("user", user);//把前台的属性放到session中
mv.setViewName("hello");//从视图解析器中匹配到 prefix+"hello"+suffix 页面,
//就是/web-inf/hello.jsp/
mv.addObject("name", user.getName());//所有希望被封装的数据必须存放的ModelAndView中,这里跟Action区别开来.在页面拿到list//mv.addObject("list",list) 页面通过${拿到list数据 } //必须通过addObject或者无视图解析器的ModelMap map.addAttribute("list",list)
mv.addObject("pwd", user.getPwd());
System.out.println("HelloContrller hello.do run");
return mv;
}
return mv;
}
//添加一个add请求,如果没有登录的话,不输出add,从拦截器就拦截了。成功登录后执行它会输出"add"
@RequestMapping("/add")
public String add(){
System.out.println("add");
return "redirect:/index.jsp";
}
}
class User{
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
}
}
4.Spring配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置视图解析器-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="cn.jea.controller"/>
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--访问所有页面都需要经过这个拦截器-->
<mvc:mapping path="/**"/>
<!--拦截器的bean注入的List注入,从外部配置允许通过的请求名称-->
<bean class="cn.jea.interceptor.LoginInterceptor">
<property name="allowRequest">
<list>
<value>hello.do</value>
<!-- <value>add.do</value> -->
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
阅读全文
0 0
- Springmvc第三讲学习笔记,拦截器Interceptor的使用
- SpringMVC 中的Interceptor 拦截器---学习笔记
- springMVC中Interceptor拦截器的使用
- SpringMVC 笔记--拦截器--interceptor
- springMVC使用interceptor拦截器
- SpringMVC的interceptor拦截器
- SpringMVC的拦截器Interceptor
- SpringMVC的拦截器Interceptor
- 【springMVC】springMVC中使用Interceptor拦截器
- SpringMVC中使用Interceptor拦截器(仅作笔记学习)
- SpringMVC中使用Interceptor拦截器的使用
- SpringMvc Interceptor拦截器的配置与使用
- 使用SpringMVC的拦截器interceptor解决项目路径问题
- SpringMVC中使用Interceptor拦截器
- SpringMVC中使用Interceptor拦截器
- SpringMVC中使用Interceptor拦截器
- SpringMVC中使用Interceptor拦截器
- SpringMVC中使用Interceptor拦截器
- C++程序员学习发展方向分析和指导(C++入门学习指导建议必看)
- Kotlin 中的 类 和 继承 (一)
- Java UDP多人聊天系统
- 山东2016年高考高校理工科录取人数统计
- MFC框架仿真---(2)动态创建
- Springmvc第三讲学习笔记,拦截器Interceptor的使用
- android Toast设置弹框大小
- 《深入理解java虚拟机》读书笔记——GC与内存分配策略
- java实现二分查找法
- JNI(java Native Interface)linux下 java调用本地c函数
- java日历类和包装类的注意点
- linux程序无源码调试的方法
- VS2013/MFC编程入门之一
- 对python字符串操作的几种方法(函数)