springmvc拦截器

springmvc拦截器的执行流程
        处理请求前,被执行preHandle

处理请求后,数据渲染前,被执行postHandle

处理请求后,数据渲染后,被执行afterCompletion

springmvc拦截器实现登录的拦截(转发和重定向)
        index.jsp中的内容

 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>主页</title>
  </head>
  <body>
  <a href="hello">欢迎进入springmvc的世界</a>
  <h1>欢迎</h1>
  <form action="${pageContext.request.contextPath}/login" method="post">
 
    账    号:<input type="text" name="name" id="name" />
 
    密    码:<input type="text" name="password" id="password"/>
 
    <input type="submit"/>
 
  </form>
  </body>
</html>

    springmvc.xml中的配置文件

<?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: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">
    <!--开启自动扫描包-->
    <context:component-scan base-package="org.zmt.*"></context:component-scan>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
 
    <!--注入视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前后缀-->
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
 
    <!--配置拦截器-->
    <mvc:interceptors>
        <!--登录拦截器-->
        <mvc:interceptor>
            <!--拦截-->
            <mvc:mapping path="/**"/>
            <!--不拦截的请求-->
            <mvc:mapping path="/login"/>
            <bean id="loginInterceptor" class="org.zmt.intercertor.LoginInterceptor"></bean>
        </mvc:interceptor>
 
    </mvc:interceptors>
</beans>

 interceptor拦截器中的内容

package org.zmt.intercertor;
 
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拿到浏览器后缀地址
        String uri = request.getRequestURI();
        //拿到账号密码
        String name = request.getParameter("name");
        String pwd = request.getParameter("password");
 
        //后缀为login或者Login时
        if (uri.contains("Login")||uri.contains("login")){
            //对账号或者密码进行判断
            if (name != null & name != "" & pwd != null & pwd != ""){
                return true;
            }else {
 
                //转发
                request.getRequestDispatcher("/index.jsp").forward(request, response);
            }
        }else {
            //重定向
            response.sendRedirect(request.getContextPath()+"/tologin");
        }
        return false;
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
 
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
 
    }
}

        controller中的内容

package org.zmt.controller;
 
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import sun.rmi.runtime.Log;
 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
 
@Controller
public class HelloController {
 
    @RequestMapping("/hello")
    public ModelAndView hello(){
        System.out.println("执行业务逻辑");
        ModelAndView mv = new ModelAndView();
        mv.addObject("lj","说的就是你!");
        mv.setViewName("show");
        return mv;
    }
    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest request){
        String name = request.getParameter("name");
        ModelAndView mv = new ModelAndView();
        mv.addObject("name",name);
        mv.setViewName("login");
        return mv;
    }
 
    @RequestMapping("/tologin")
    public String toLogin(){
        return "index";
    }
}

        登录成功后的页面login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>欢迎</h1>
 
<p>账    号:${name}</p>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值