struts2 拦截器

执行的流程
    提交请求到服务器端,请求会先经过Struts2的核心过滤器(StrutsPrepareAndExecuteFilter)。
    过滤器的功能是完成了一部分代码功能。
    就是执行了一系列的拦截器,进行一些处理工作。
    可以在struts-default.xml配置文件中看到有很多的拦截器。
    拦截器执行完后,会根据struts.xml的配置文件找到请求路径,找到具体的类,通过反射的方式让方法执行。

总结
    JSP页面-->StrutsPrepereAndExecuteFilter过滤器-->执行一系列拦截器(完成了部分代码)-->执行到目标Action-->返回字符串-->结果页面(result)-->页面跳转
<
package cn.happy.Interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;

import java.util.Map;

/**
 * Created by WuCunJian on 2017/10/25.
 */
public class MyTimerInterceptor implements Interceptor {
    public void destroy() {

    }
    public void init() {
        System.out.println("拦截器已经成功初始化........");
    }
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("对象"+invocation);
        //Action是谁
        Object action = invocation.getAction();
        System.out.println("action============="+action);
        String value;
        //判定session里面key值对应的value是否存在
        Map<String, Object> session = ActionContext.getContext().getSession();
        Object name = session.get("uname");
        String actionName = invocation.getProxy().getActionName();
        invocation.getProxy().getNamespace();//user
        System.out.println("actionName======"+actionName);
        if (actionName.equals("login")) {
            System.out.println("================================Action是Login");
            //user/login.action
            value= invocation.invoke();
        }else if(name!=null) {
            //已经登录了,,放行
            value= invocation.invoke();
            String method = invocation.getProxy().getMethod();
            System.out.println("方法:"+method);
        }else{
            //走到这个else,证明你访问的Action,既不是登录,也不是被授权访问的操作
            value="login";
        }
        System.out.println("逻辑视图名"+value);
        return value;
    }
}
UserInfo的持久层
package cn.happy.entity;

/**
 * Created by Happy on 2017-10-24.
 */
public class UserInfo {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.devMode" value="true"></constant><package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myTimer" class="cn.happy.Interceptor.MyTimerInterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="myTimer"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack></interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> <default-action-ref name="login"></default-action-ref> <action name="login" class="cn.happy.action.MyTimerAction"> <result name="success">/success.jsp</result> <result name="login">/login.jsp</result> </action></package></struts>
login.jsp
 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<div align="center">
    <s:form action="login" method="POST">
        账号:<s:textfield name="user.username"></s:textfield><br/>
        密码:<s:password name="user.password"></s:password>
        <s:submit value="登陆"></s:submit>
    </s:form>
</div>
</body>
</html>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值