struts2 Interceptor详解

本文详细介绍了如何使用Struts2拦截器实现登录功能,包括注册页面设置会话,登录页面实现登录功能,登录类执行逻辑,拦截器类实现登录验证,以及拦截器配置和Web.xml配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是拦截器[Interceptor]
拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
在Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。
看来拦截器这东东的确很牛哦,当然拦截器本身也是一个普通的 Java 对象,而且拦截器还支持插拔的特性,不想用拦截器就不用,喜欢时再加上,不像EJB那么霸道,我想这也算是大家喜欢Struts2的一个重要原因.
提到拦截器支持插拔的特性,不得不提到Struts2中的有关拦截器的另一概念——拦截器栈[理解成拦截器链,感觉好理解点].在Struts2中允许将多个拦截器排列在一起使之成为一个集合,在访问被拦截的Action时就会按照其定义顺序被调用.

1.在注册页面中(index.jsp)注册一下:

 <%
    session.setAttribute("user","lzw");
 %>

2.在登录界面(login.jsp)登录:

<form action="login.action" method="post">
    <table align="center">
        <tr>
            <td>
                用户名:
            </td>
            <td>
                <input type="text" name="userName" />
            </td>
        </tr>
        <tr>
            <td>
                密&nbsp;&nbsp;码:
            </td>
            <td>
                <input type="password" name="password" />
            </td>
        </tr>
        <tr align="center">
            <td colspan="2">
                <input type="submit" value="登录" />
            </td>
        </tr>
    </table>
</form>

3.登录类:LoginAction.java

package com.lzw.action;

import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    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;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("开始执行execute()");
        Map<String, Object> map = ActionContext.getContext().getSession();
        String user = (String) map.get("user");
        System.out.println("The user'name is ---------- " + user);
        return SUCCESS;
    }
}

4.success.jsp页面

welcome   ${userName} to struts2 world。

5.拦截器类:LoginInterceper.java

package com.lzw.intercepter;

import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginIntercepter extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        Map<String, Object> session = invocation.getInvocationContext()
                .getSession();
        String user = (String) session.get("user");
        System.out.println("11---------------- " + user);
        if ("".equals(user) || null == user) {
            System.out.println("22---------------- ");
            return Action.INPUT;
        } else {
            System.out.println(" 33---------------- " + user);
            return invocation.invoke();
        }
    }
}

6.struts2配置文件struts.xml

<!DOCTYPE struts PUBLIC  
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="struts" extends="struts-default">
        <interceptors>
            <interceptor name="authority"
                class="com.lzw.intercepter.LoginIntercepter" />
            <interceptor-stack name="loginStack">
                <interceptor-ref name="authority" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="loginStack" />

        <action name="login" class="com.lzw.action.LoginAction">
            <result>/success.jsp</result>
            <result name="input" type="redirect">/login.jsp</result>
        </action>
    </package>
</struts>

7.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

8.部署到tomcat服务器上,在地址栏上输入:http://localhsot:8080/test来注册,
再重新输入:http://localhost:8080/test/login.jsp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值