最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Struts2實現(xiàn)對action請求對象的攔截操作方法

 更新時間:2017年11月06日 09:02:39   作者:青之羽  
這篇文章主要介紹了Struts2實現(xiàn)對action請求對象的攔截操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

Struts2的核心功能是action,對于開發(fā)人員來說,使用Struts2主要就是編寫action,action類通常都要實現(xiàn)com.opensymphony.xwork2.Action接口,并實現(xiàn)該接口中的execute()方法。

該方法如下:

   public String execute() throws Exception

   Struts2并不是要求所有編寫的action類都要實現(xiàn)Action接口,也可以直接編寫一個普通的Java類作為action,只要實現(xiàn)一個返回類型為String的無參的public方法即可:

    public String  xxx()

步入正文:

建立一個攔截器對象,當有客戶端的請求要訪問action對象的時候?qū)|發(fā)當前的攔截器對象,來對當前的請求數(shù)據(jù)進行過濾操作。

建立一個登錄界面用于進行用戶名和密碼的輸入操作,當?shù)卿浗缑娈斨械谋韱螌ο螽斨械臄?shù)據(jù)提交到action類對象之前,會被攔截器對象進行攔截操作,攔截器對象會從session對象當中進行注冊信息的獲取操作,通過注冊信息registerMessage是否為空來判斷當前用戶是否有權限對action類對象進行訪問操作,如果registerMessage為null,則當前用戶必須要先進行用戶信息的注冊操作,在注冊頁面當中將registerMessage屬性變量添加到session對象當中去然后才能夠去進行登錄操作,訪問action對象。
建立一個攔截器對象用于實現(xiàn)對所有訪問action對象的請求數(shù)據(jù)進行攔截操作。

1:建立一個攔截器對象MyInterceptor該對象繼承了抽象攔截器對象類。

2:在建立了攔截器對象之后要想進行使用首先要對該攔截器對象進行注冊操作,具體的方式

是在struts.xml當中使用interceptors標簽來實現(xiàn)攔截器的注冊操作

<interceptors>
<interceptor name="MyInterceptor" class="com.interceptots.MyInterceptor"/>
</interceptors>

3:要將當前所注冊的攔截器對象與指定的action類進行綁定操作,所以用interceptor

標簽對象來將其所在的action類與指定的攔截器對象進行綁定操作.

<interceptor-ref name="MyInterceptor"/>

4:注意如果只綁定指定的攔截器對象就會對struts-default.xml對象當中所默認的攔截

器對象進行覆蓋操作,在默認的對象當中所綁定的是一個攔截器棧,該棧當中有二十多個攔截器對

象,所以必須要對原來struts-default.xml文件當中的攔截器進行重新綁定操作.

在自定義的攔截器對象當中實現(xiàn)對action對象進行權限攔截操作:

用戶要想實現(xiàn)登錄來訪問action對象,必須要先注冊一個registerMessage信息到session對象當中才行,否則在請求訪問

action對象的時候,將會被攔截器對象進行攔截操作,發(fā)現(xiàn)當前的session會話當中所獲取到的注冊信息為空時,將會返回到注冊失

敗的頁面當中去.

登錄界面:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
 pageEncoding="UTF-8"%> 
<%@ taglib uri="/struts-tags" prefix="s"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
 <form action="SystemAction"> 
  <s:textfield name="username" label="用戶名"/><br> 
  <s:textfield name="password" label="密碼"/><br> 
  <a href="register.jsp" rel="external nofollow" rel="external nofollow" >進行用戶的注冊</a><br> 
  <a href="destroy.jsp" rel="external nofollow" rel="external nofollow" >進行用戶的注銷</a><br> 
  當前用戶名:${sessionScope.registerMessage } 
  <br> 
  <input type="submit" value="提交"> 
 </form> 
</body> 
</html> 

注冊界面:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
 pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>注冊頁面</title> 
</head> 
<body> 
 <% 
  session.setAttribute("registerMessage","qingzhiyu"); 
  if(session.getAttribute("registerMessage")!=null) 
  { 
   %> 
    <h3>注冊成功</h3> 
   <% 
  } 
 %> 
  
 <a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >進行登錄</a> 
 ${sessionScope.registerMessage } 
</body> 
</html> 

action類實例對象:

package com.action; 
/** 
 * 
 * @author Administrator 
 * 
 */ 
public class SystemAction { 
 private String username; 
 private String password; 
  
 /** 
  * @return the username 
  */ 
 public String getUsername() { 
  return username; 
 } 
 
 /** 
  * @param username the username to set 
  */ 
 public void setUsername(String username) { 
  this.username = username; 
 } 
 
 /** 
  * @return the password 
  */ 
 public String getPassword() { 
  return password; 
 } 
 
 /** 
  * @param password the password to set 
  */ 
 public void setPassword(String password) { 
  this.password = password; 
 } 
 
 public String execute() 
 { 
  System.out.println("已經(jīng)進入到了系統(tǒng)action類當中"); 
  return "success"; 
 } 
} 

攔截器對象:

package com.interceptots; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionInvocation; 
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 
/** 
 * 
 * @author Administrator 
 *自定義一個攔截器對象,該攔截器對象實現(xiàn)抽象類AbstractInterceptor攔截器對象 
 */ 
public class MyInterceptor extends AbstractInterceptor{ 
 
 /* (non-Javadoc) 
  * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation) 
  *對抽象類當中的方法進行重寫操作。invocation(請求,調(diào)用) 
  */ 
 @Override 
 public String intercept(ActionInvocation invocation) throws Exception { 
  System.out.println("執(zhí)行攔截方法"); 
 //從session會話對象當中進行注冊信息的獲取操作 
  String registerMessage=(String) ActionContext.getContext().getSession().get("registerMessage"); 
  /*Map session=(Map)invocation.getInvocationContext().getSession(); 
  String registerMessage=(String) session.get("registerMessage");*/ 
  System.out.println("registerMessage="+registerMessage); 
  if(registerMessage!=null) 
  { 
   //表明本次會話當中用戶已經(jīng)進行了信息的注冊,所以擁有訪問action類對象的權限,所以使用調(diào)度對象來調(diào)用action 
//對象當中所指定的方法來實現(xiàn)業(yè)務的控制操作   
   /*類似于過濾器鏈對象當中的chain方法實現(xiàn)過濾權限的轉(zhuǎn)交操作*/   
   String result=invocation.invoke(); 
   System.out.println("invocation當中的返回值為:"+result); 
   return result; 
  } 
  else 
  {//表明當前請求訪問action對象的用戶并沒有進行信息的注冊所以不具有訪問action對象的權限,所以返回失敗頁面 
   return "fail"; 
  } 
 }  
} 

攔截器對象要想使用,必須要在配置文件當中進行配置操作之后才會起作用

struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
 "http://struts.apache.org/dtds/struts-2.3.dtd"> 
<struts> 
 <package name="default-package" namespace="/" extends="struts-default"> 
 <!--使用interceptors標簽對象來進行一個攔截器對象的注冊操作--> 
  <interceptors> 
   <interceptor name="MyInterceptor" class="com.interceptots.MyInterceptor"/> 
  </interceptors> 
  <action name="SystemAction" class="com.action.SystemAction"> 
   <result name="success">/success.jsp</result> 
   <result name="input">/login.jsp</result> 
   <result name="fail">/fail.jsp</result> 
   <interceptor-ref name="MyInterceptor"/> 
<!--對原有的默認攔截器對象進行綁定操作-->   
   <interceptor-ref name="defaultStack"/> 
  </action> 
 </package> 
</struts> 

登錄成功界面:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
 pageEncoding="UTF-8"%> 
<%@ taglib uri="/struts-tags" prefix="s" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
 <h2>登錄成功</h2> 
 用戶名:${username } 
 <br> 
 密碼: ${password } 
  
 <br> 
 <a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >返回登錄首頁</a> 
 <a href="destroy.jsp" rel="external nofollow" rel="external nofollow" >注銷登錄</a> 
</body> 
</html> 

登錄失敗界面:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
 pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>權限登錄失敗界面</title> 
</head> 
<body> 
 您沒有進行信息的注冊,所以無權進行action對象的訪問操作 
 <a href="register.jsp" rel="external nofollow" rel="external nofollow" >進行用戶注冊</a> 
 <a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >返回登錄界面</a> 
</body> 
</html> 

進行當前用戶信息的注銷界面:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
 pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>進行用戶信息的注銷操作</title> 
</head> 
<body> 
 <% 
  session.removeAttribute("registerMessage"); 
  String registerMessage=(String)session.getAttribute("registerMessage"); 
  if(registerMessage==null) 
  { 
   %> 
    用戶信息注銷成功 
   <% 
  } 
 %> 
 <a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >login</a> 
</body> 
</html> 

程序的運行結果:

登錄界面

注冊界面

登錄成功界面:

如果沒有進行用戶注冊直接進行登錄操作

總結

以上所述是小編給大家介紹的Struts2實現(xiàn)對action請求對象的攔截操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • springboot集成Mybatis的詳細教程

    springboot集成Mybatis的詳細教程

    今天給大家?guī)淼倪€是關于springboot的相關知識,文章圍繞著springboot集成Mybatis的詳細教程展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • IDEA編譯亂碼Build Output提示信息亂碼

    IDEA編譯亂碼Build Output提示信息亂碼

    IDEA編譯的時候亂碼,Build Output提示信息亂碼,本文就詳細的介紹一下解決方法,有需要的同學可以了解一下
    2021-06-06
  • 解決IDEA Maven下載依賴時報錯ERROR - #org.jetbrains.idea.maven - Cannot reconnect.

    解決IDEA Maven下載依賴時報錯ERROR - #org.jetbrains.ide

    這篇文章主要介紹了解決IDEA Maven下載依賴時報錯ERROR - #org.jetbrains.idea.maven - Cannot reconnect.問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 貨拉拉大數(shù)據(jù)對BitMap的探索實踐詳解

    貨拉拉大數(shù)據(jù)對BitMap的探索實踐詳解

    這篇文章主要為大家介紹了貨拉拉大數(shù)據(jù)對BitMap的探索實踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • java和javascript中過濾掉img形式的字符串不顯示圖片的方法

    java和javascript中過濾掉img形式的字符串不顯示圖片的方法

    這篇文章主要介紹了java和javascript中過濾掉img形式的字符串不顯示圖片的方法,以實例形式分別講述了采用java和javascript實現(xiàn)過濾掉img形式字符串的技巧,需要的朋友可以參考下
    2015-02-02
  • 解析MyBatis源碼實現(xiàn)自定義持久層框架

    解析MyBatis源碼實現(xiàn)自定義持久層框架

    這篇文章主要介紹了手撕MyBatis源碼實現(xiàn)自定義持久層框架,涉及到的設計模式有Builder構建者模式、??模式、代理模式,本文通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • Java fastjson2 解析JSON用法詳解

    Java fastjson2 解析JSON用法詳解

    Fastjson2是Fastjson的升級版,提供了更好的性能和擴展性,它支持多種方式解析JSON數(shù)據(jù),包括將JSON字符串轉(zhuǎn)換為Java對象、嵌套的JSON對象和數(shù)組,以及轉(zhuǎn)換成Map或List,本文介紹Java fastjson2 解析JSON用法,感興趣的朋友一起看看吧
    2025-02-02
  • Java實現(xiàn)5種負載均衡算法(小結)

    Java實現(xiàn)5種負載均衡算法(小結)

    負載均衡是將客戶端請求訪問,通過提前約定好的規(guī)則轉(zhuǎn)發(fā)給各個server,本文主要介紹了Java實現(xiàn)5種負載均衡算法,具有一定的參考價值,感興趣的可以了解一下
    2022-06-06
  • Invalid?bound?statement?(not?found)出現(xiàn)原因以及解決辦法

    Invalid?bound?statement?(not?found)出現(xiàn)原因以及解決辦法

    這篇文章主要給大家介紹了關于Invalid?bound?statement?(not?found)出現(xiàn)原因以及解決辦法的相關資料,文中給出了詳細的解決方法,需要的朋友可以參考下
    2023-07-07
  • Struts2中ognl遍歷數(shù)組,list和map方法詳解

    Struts2中ognl遍歷數(shù)組,list和map方法詳解

    這篇文章主要介紹了Struts2中ognl遍歷數(shù)組,list和map方法詳解,需要的朋友可以參考下。
    2017-09-09

最新評論

宁德市| 铁岭县| 乌海市| 丰都县| 黎平县| 博湖县| 顺义区| 中西区| 棋牌| 防城港市| 昔阳县| 宁远县| 中西区| 黄梅县| 平安县| 汝州市| 辽宁省| 南溪县| 宕昌县| 溧水县| 壤塘县| 霍城县| 丰镇市| 仪征市| 秭归县| 新河县| 临猗县| 耿马| 柞水县| 宁武县| 浦东新区| 方正县| 清涧县| 平顶山市| 鹤峰县| 横山县| 沈丘县| 北海市| 镇巴县| 如东县| 临夏县|