Struts2實(shí)現(xiàn)對(duì)action請(qǐng)求對(duì)象的攔截操作方法
Struts2的核心功能是action,對(duì)于開發(fā)人員來(lái)說(shuō),使用Struts2主要就是編寫action,action類通常都要實(shí)現(xiàn)com.opensymphony.xwork2.Action接口,并實(shí)現(xiàn)該接口中的execute()方法。
該方法如下:
public String execute() throws Exception
Struts2并不是要求所有編寫的action類都要實(shí)現(xiàn)Action接口,也可以直接編寫一個(gè)普通的Java類作為action,只要實(shí)現(xiàn)一個(gè)返回類型為String的無(wú)參的public方法即可:
public String xxx()
步入正文:
建立一個(gè)攔截器對(duì)象,當(dāng)有客戶端的請(qǐng)求要訪問(wèn)action對(duì)象的時(shí)候?qū)?huì)觸發(fā)當(dāng)前的攔截器對(duì)象,來(lái)對(duì)當(dāng)前的請(qǐng)求數(shù)據(jù)進(jìn)行過(guò)濾操作。
建立一個(gè)登錄界面用于進(jìn)行用戶名和密碼的輸入操作,當(dāng)?shù)卿浗缑娈?dāng)中的表單對(duì)象當(dāng)中的數(shù)據(jù)提交到action類對(duì)象之前,會(huì)被攔截器對(duì)象進(jìn)行攔截操作,攔截器對(duì)象會(huì)從session對(duì)象當(dāng)中進(jìn)行注冊(cè)信息的獲取操作,通過(guò)注冊(cè)信息registerMessage是否為空來(lái)判斷當(dāng)前用戶是否有權(quán)限對(duì)action類對(duì)象進(jìn)行訪問(wèn)操作,如果registerMessage為null,則當(dāng)前用戶必須要先進(jìn)行用戶信息的注冊(cè)操作,在注冊(cè)頁(yè)面當(dāng)中將registerMessage屬性變量添加到session對(duì)象當(dāng)中去然后才能夠去進(jìn)行登錄操作,訪問(wèn)action對(duì)象。
建立一個(gè)攔截器對(duì)象用于實(shí)現(xiàn)對(duì)所有訪問(wèn)action對(duì)象的請(qǐng)求數(shù)據(jù)進(jìn)行攔截操作。
1:建立一個(gè)攔截器對(duì)象MyInterceptor該對(duì)象繼承了抽象攔截器對(duì)象類。
2:在建立了攔截器對(duì)象之后要想進(jìn)行使用首先要對(duì)該攔截器對(duì)象進(jìn)行注冊(cè)操作,具體的方式
是在struts.xml當(dāng)中使用interceptors標(biāo)簽來(lái)實(shí)現(xiàn)攔截器的注冊(cè)操作
<interceptors> <interceptor name="MyInterceptor" class="com.interceptots.MyInterceptor"/> </interceptors>
3:要將當(dāng)前所注冊(cè)的攔截器對(duì)象與指定的action類進(jìn)行綁定操作,所以用interceptor
標(biāo)簽對(duì)象來(lái)將其所在的action類與指定的攔截器對(duì)象進(jìn)行綁定操作.
<interceptor-ref name="MyInterceptor"/>
4:注意如果只綁定指定的攔截器對(duì)象就會(huì)對(duì)struts-default.xml對(duì)象當(dāng)中所默認(rèn)的攔截
器對(duì)象進(jìn)行覆蓋操作,在默認(rèn)的對(duì)象當(dāng)中所綁定的是一個(gè)攔截器棧,該棧當(dāng)中有二十多個(gè)攔截器對(duì)
象,所以必須要對(duì)原來(lái)struts-default.xml文件當(dāng)中的攔截器進(jìn)行重新綁定操作.
在自定義的攔截器對(duì)象當(dāng)中實(shí)現(xiàn)對(duì)action對(duì)象進(jìn)行權(quán)限攔截操作:
用戶要想實(shí)現(xiàn)登錄來(lái)訪問(wèn)action對(duì)象,必須要先注冊(cè)一個(gè)registerMessage信息到session對(duì)象當(dāng)中才行,否則在請(qǐng)求訪問(wèn)
action對(duì)象的時(shí)候,將會(huì)被攔截器對(duì)象進(jìn)行攔截操作,發(fā)現(xiàn)當(dāng)前的session會(huì)話當(dāng)中所獲取到的注冊(cè)信息為空時(shí),將會(huì)返回到注冊(cè)失
敗的頁(yè)面當(dāng)中去.
登錄界面:
<%@ 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" >進(jìn)行用戶的注冊(cè)</a><br> <a href="destroy.jsp" rel="external nofollow" rel="external nofollow" >進(jìn)行用戶的注銷</a><br> 當(dāng)前用戶名:${sessionScope.registerMessage } <br> <input type="submit" value="提交"> </form> </body> </html>
注冊(cè)界面:
<%@ 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>注冊(cè)頁(yè)面</title> </head> <body> <% session.setAttribute("registerMessage","qingzhiyu"); if(session.getAttribute("registerMessage")!=null) { %> <h3>注冊(cè)成功</h3> <% } %> <a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >進(jìn)行登錄</a> ${sessionScope.registerMessage } </body> </html>
action類實(shí)例對(duì)象:
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)進(jìn)入到了系統(tǒng)action類當(dāng)中"); return "success"; } }
攔截器對(duì)象:
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 *自定義一個(gè)攔截器對(duì)象,該攔截器對(duì)象實(shí)現(xiàn)抽象類AbstractInterceptor攔截器對(duì)象 */ public class MyInterceptor extends AbstractInterceptor{ /* (non-Javadoc) * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation) *對(duì)抽象類當(dāng)中的方法進(jìn)行重寫操作。invocation(請(qǐng)求,調(diào)用) */ @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("執(zhí)行攔截方法"); //從session會(huì)話對(duì)象當(dāng)中進(jìn)行注冊(cè)信息的獲取操作 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) { //表明本次會(huì)話當(dāng)中用戶已經(jīng)進(jìn)行了信息的注冊(cè),所以擁有訪問(wèn)action類對(duì)象的權(quán)限,所以使用調(diào)度對(duì)象來(lái)調(diào)用action //對(duì)象當(dāng)中所指定的方法來(lái)實(shí)現(xiàn)業(yè)務(wù)的控制操作 /*類似于過(guò)濾器鏈對(duì)象當(dāng)中的chain方法實(shí)現(xiàn)過(guò)濾權(quán)限的轉(zhuǎn)交操作*/ String result=invocation.invoke(); System.out.println("invocation當(dāng)中的返回值為:"+result); return result; } else {//表明當(dāng)前請(qǐng)求訪問(wèn)action對(duì)象的用戶并沒有進(jìn)行信息的注冊(cè)所以不具有訪問(wèn)action對(duì)象的權(quán)限,所以返回失敗頁(yè)面 return "fail"; } } }
攔截器對(duì)象要想使用,必須要在配置文件當(dāng)中進(jìn)行配置操作之后才會(huì)起作用
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標(biāo)簽對(duì)象來(lái)進(jìn)行一個(gè)攔截器對(duì)象的注冊(cè)操作--> <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"/> <!--對(duì)原有的默認(rèn)攔截器對(duì)象進(jìn)行綁定操作--> <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" >返回登錄首頁(yè)</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>權(quán)限登錄失敗界面</title> </head> <body> 您沒有進(jìn)行信息的注冊(cè),所以無(wú)權(quán)進(jìn)行action對(duì)象的訪問(wèn)操作 <a href="register.jsp" rel="external nofollow" rel="external nofollow" >進(jìn)行用戶注冊(cè)</a> <a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >返回登錄界面</a> </body> </html>
進(jìn)行當(dāng)前用戶信息的注銷界面:
<%@ 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>進(jìn)行用戶信息的注銷操作</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>
程序的運(yùn)行結(jié)果:
登錄界面
注冊(cè)界面
登錄成功界面:
如果沒有進(jìn)行用戶注冊(cè)直接進(jìn)行登錄操作
總結(jié)
以上所述是小編給大家介紹的Struts2實(shí)現(xiàn)對(duì)action請(qǐng)求對(duì)象的攔截操作方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring@Autowired與@Resource的區(qū)別有哪些
這篇文章主要為大家詳細(xì)介紹了@Autowired與@Resource的區(qū)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02dubbo服務(wù)引用之創(chuàng)建Invoker流程詳解
這篇文章主要為大家介紹了dubbo服務(wù)引用二之創(chuàng)建Invoker流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Spring Boot 通過(guò)AOP和自定義注解實(shí)現(xiàn)權(quán)限控制的方法
這篇文章主要介紹了Spring Boot 通過(guò)AOP和自定義注解實(shí)現(xiàn)權(quán)限控制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11MyBatis中模糊查詢使用CONCAT('%',#{str},'%')出錯(cuò)的解
這篇文章主要介紹了MyBatis中模糊查詢使用CONCAT('%',#{str},'%')出錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java版超大整數(shù)階乘算法代碼詳解-10,0000級(jí)
這篇文章主要介紹了Java版超大整數(shù)階乘算法代碼詳解-10,0000級(jí),具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01