Struts2自定义拦截器:登录拦截

1、创建Action类:
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport implements SessionAware {

private String message;
private Map mySession;

public void setSession(Map<String, Object> mySession) {
this.mySession = mySession;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String execute() {
if ((message == null) || message.isEmpty()) {
addActionError("请输入信息后提交!");
return INPUT;
} else {
addActionMessage("您输入的信息是:" + message);
mySession.put("loginFlag", "login");
return SUCCESS;
}

}

}
-----------------------------------------------------------------------------------------------------------------------------
2、创建拦截器:LoginStatus.java
package com.Myfirststruts.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginStatus extends AbstractInterceptor {

@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if(action instanceof Login){
return invocation.invoke();
}
Map session = invocation.getInvocationContext().getSession();
String login = (String)session.get("loginFlag");
if(login != null && login.equals("login")){
return invocation.invoke();
}
else {
((ActionSupport)action).addActionMessage("来自拦截器的消息:您还没有登录。请先登录!");
return "input";
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
3、在struts.xml注册并使用拦截器:
<package name="default"  namespace="/" extends="struts-default">   <!-- 加上namespace="/"  重要-->
        <interceptors>
            <interceptor name="loginStatus" class="com.Myfirststruts.action.LoginStatus" />
        </interceptors>
        <action name="login" class="com.Myfirststruts.action.Login">
            
            <result >/login.jsp</result>
            <result name="input">/login.jsp</result>
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="loginStatus" />
        </action>
        <action name="others">
            <result>/others.jsp</result>
            <result name="input">/login.jsp</result>
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="loginStatus" />
        </action>
    </package> 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

4、 创建jsp页面:
login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:actionmessage/>
<s:actionerror/>
<s:form action="login">
<s:textfield name="message" label="Message" />
<s:submit name="submit" />
</s:form>
<s:a action="others">单击此处,访问others Action!</s:a>
---------------------------------------------------
others.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
到达此页面,表示您是在输入信息后进行的提交。<br>
<s:a href="login.jsp">返回到输入页面!</s:a>
----------------------------------------------------------------------------------------------------------
5、最后最重要一步就是:
发布web项目,重新启动tomcat服务器,访问login.jsp时不要在myeclipse的自带测试器上测试,谨记!!!
======================================================end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值