(Struts2学习篇) Struts2 拦截器解析

java哥 阅读:219 2021-03-31 23:03:53 评论:0

版本:struts2.5.5

此实例实现功能:用户需要指定用户名登陆,登陆成功进入相应页面执行操作,否则返回到登陆页面进行登陆,当直接访问操作页面(登陆后才能访问的页面)时则不允许,须返回登陆页面。

 

1、Struts.xml配置文件

<package name="authority" extends="struts-default">   
<!-- 定义一个拦截器 -->   
        <interceptors>   
            <interceptor name="authority"   
                class="com.vixuan.skydrive.interceptot.LoginInterceptor">   
            </interceptor>   
            <!-- 拦截器栈 -->   
            <interceptor-stack name="mydefault">   
                <interceptor-ref name="defaultStack" />   
                <interceptor-ref name="authority" />   
            </interceptor-stack>   
        </interceptors>           
        <!-- 定义全局Result -->   
        <global-results>   
            <!-- 当返回login视图名时,转入/login.jsp页面 -->   
            <result name="login">/login.jsp</result>   
        </global-results> 
         
        <action name="login" class="com.vixuan.skydrive.action.LoginAction">   
            <result name="success">/welcome.jsp</result>   
            <result name="error">/login.jsp</result>   
            <result name="input">/login.jsp</result>   
        </action>          
         <action name="show" class="com.vixuan.skydrive.action.ShowAction">   
            <result name="success">/show.jsp</result>   
            <!-- 使用此拦截器 -->   
            <interceptor-ref name="mydefault" />   
        </action>           
</package>


登入Action(LoginAction)

package com.vixuan.skydrive.action; 
 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 
import com.vixuan.skydrive.util.*; 
 
public class LoginAction extends ActionSupport { 
 
	/** 
	 *  
	 */ 
	private static final long serialVersionUID = 1L; 
	private String username; 
	private String password; 
	//判断值是否合法 
	private boolean isInvalid(String value) {      
		return (value == null || value.length() == 0);      
	}     
		 
	@Override 
	public String execute() throws Exception { 
		// TODO Auto-generated method stub 
		 if (isInvalid(getUsername()))      
             return INPUT;      
      
         if (isInvalid(getPassword()))      
             return INPUT; 
          
         if(this.getUsername().equals("root")&& this.getPassword().equals("123456")){   
             ActionContext.getContext().getSession().put(Constants.USER,getUsername());   
             ActionContext.getContext().getSession().put(Constants.PASS,getPassword());   
             return "success";   
         }   
 
		return ERROR; 
	} 
	//set 和  get 
	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; 
	} 
	 
	 
	 
 
} 


显示Action(ShowAction)

package com.vixuan.skydrive.action; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
public class ShowAction extends ActionSupport { 
 
	/** 
	 *  
	 */ 
	private static final long serialVersionUID = 1L; 
 
	@Override 
	public String execute() throws Exception { 
		// TODO Auto-generated method stub 
		return SUCCESS; 
	} 
	 
	 
 
}


登入拦截器Interceptor(LoginInteceptor)

package com.vixuan.skydrive.interceptot; 
 
import java.util.Map; 
 
import com.opensymphony.xwork2.Action; 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionInvocation; 
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 
import com.vixuan.skydrive.util.*; 
 
public class LoginInterceptor extends AbstractInterceptor { 
 
	/** 
	 *  
	 */ 
	private static final long serialVersionUID = 1L; 
 
	@Override 
	public String intercept(ActionInvocation invocation) throws Exception { 
		// TODO Auto-generated method stub 
		 // 取得请求相关的ActionContext实例    
        ActionContext ctx = invocation.getInvocationContext();   
        Map session = ctx.getSession();   
        String user = (String) session.get(Constants.USER);   
   
        // 如果没有登陆,或者登陆所有的用户名不是yuewei,都返回重新登陆    
   
        if (user != null && user.equals("root")) {   
            System.out.println("验证通过");   
            return invocation.invoke();   
        }   
   
        ctx.put("error", "你还没有登录");   
 
		return Action.LOGIN; 
	} 
 
}


相关jsp源代码:

登入界面----login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>     
    <title>用户登入</title>    
    <!--  
    <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">  
    -->   
  </head>   
     
  <body>   
  <form action="login.action" method="post">   
        用户名:<input type="text" name="username"><br>   
       密码:<input type="password" name="password"><br>   
  <input type="submit" value="submit">   
  </form>   
      
</body>   
</html>   


 

拦截界面------welcomejsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>   
      
    <title> 欢迎界面</title>   
       
 <!--  
 <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">  
 -->   
   
  </head>   
     
  <body>   
  <h1>登录成功后显示此页面</h1>   
  <a href="show.action">show</a>   
  </body>   
</html>  


成功界面--------show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
  <head>   
       
    <title>show界面</title>   
       
    <!--  
    <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">  
    -->   
   
  </head>   
     
  <body>   
    Show This Page    
        	登录后执行此页面<br>   
  </body>   
</html>  


 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们