Struts2拦截器简单示例
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Struts2拦截器简单示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            攔截器(Interceptor)是Struts 2的核心組成部分。很多功能(Feature)都是構建在攔截器基礎之上的,例如文件的上傳和下載、國際化、轉換器和數據校驗等,Struts 2利用內建的攔截器,完成了框架內的大部分操作。 在Struts 2文檔中對攔截器的解釋為——攔截器是動態攔截Action調用的對象。它提供了一種機制,使開發者可以定義一個特定的功能模塊,這個模塊可以在Action執行之前或者之后運行,也可以在一個Action執行之前阻止Action執行。同時也提供了一種可以提取Action中可重用的部分的方式 攔截器在Struts2中的示意圖:   從上圖可以看出,Struts 2架構的Action被一個或者多個攔截器(攔截器棧)所包圍,所有的用戶請求都會被攔截器所攔截,然后交給Action處理,處理結果以邏輯視圖方式返回給用戶。而這個調用執行流程,是由Struts 2的配置文件來實現的,后面會詳細介紹。攔截器是Struts 2核心部分之一。 當用戶請求到達Struts 2的ServletDispatcher時,Struts 2會查找配置文件,并根據其配置實例化相對的攔截器對象,然后串成一個列表(List),最后一個一個地調用列表中的攔截器 攔截器時序圖如下圖所示:   在Struts 2架構中,Action的調用都是通過攔截器來實現的。有的讀者可能會疑惑,為什么沒有明確說明攔截器,為什么可以直接調用Action?那是因為Struts 2架構如果不做顯式的攔截器配置,則系統會調用默認的攔截器來調用Action,在用戶看來,好像沒有配置攔截器。  Hello World攔截器示例. 程序要演示的是顯示地讓攔截器調用Action,來體會攔截器的用途。 一般情況下我們都會先寫出一個Action,然后配置struts.xml文件 MyAction.java   public class MyAction extends ActionSupport { 
??//以下屬性信息都是從前臺(JSP頁面獲得)
??private String username;
????
??private String mymsg;
????
??private String password1;
????
??private String password2;
????
??private Date birthday;
??public String execute(){
????if(username!=null&&this.getPassword1().equals(this.getPassword2())&&!this.getUsername().trim().equals("")){
????????
??????//輸出調試信息
??????System.out.println("Action信息,正在執行Action....??");
??????return SUCCESS;
????}else{
??????return INPUT;
????}
??}
????
????
??public String getUsername() {
????return username;
??}
??public void setUsername(String username) {
????this.username = username;
??}
??public String getMymsg() {
????return mymsg;
??}
??public void setMymsg(String mymsg) {
????this.mymsg = mymsg;
??}
??public String getPassword1() {
????return password1;
??}
??public void setPassword1(String password1) {
????this.password1 = password1;
??}
??public String getPassword2() {
????return password2;
??}
??public void setPassword2(String password2) {
????this.password2 = password2;
??}
??public Date getBirthday() {
????return birthday;
??}
??public void setBirthday(Date birthday) {
????this.birthday = birthday;
??}
????
} 自定義攔截器MyInterceptor.java public class MyInterceptor extends AbstractInterceptor {
??//攔截方法
??public String intercept(ActionInvocation invocation) throws Exception {
????MyAction myA=(MyAction) invocation.getAction();
????System.out.println("攔截器信息:hello world 攔截器");
????//執行action或者下一個攔截器
????String result=invocation.invoke();
????System.out.println("攔截器信息:Action執行完畢");
????return result;
??}
} 下面我們開始配置struts.xml文件 首先我定義了一個my.xml文件 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
????????"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
????????"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
????????<package name="mynew" namespace="/" extends="struts-default">
????????
??????????<interceptors>
????????????<interceptor name="myInterceptor" class="com.MyInterceptor">
????????????</interceptor>
??????????</interceptors>
????
????<action name="myAction" class="com.MyAction">
??????<result name="success">/success.jsp</result>
??????<result name="input">/index.jsp</result>
??????<!-- 引用默認攔截器 -->
??????<interceptor-ref name="defaultStack"></interceptor-ref>
??????<!-- 引用自定義攔截器 -->
??????<interceptor-ref name="myInterceptor"></interceptor-ref>
????</action>
????????</package>
</struts> 這樣的話就要在struts.xml引入, <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
????????"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
????????"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
????????<constant name="struts.devMode" value="true" />
??<include file="my.xml"></include>
</struts> 如果讀者不習慣的話完全可以寫在struts.xml文件中 index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
????<head>
????</head>
????
????<body>
??????<s:form method="post" action="myAction">????
????????<s:textfield name="username" label="用戶名"></s:textfield>
????????<s:password name="password1" label="密碼"></s:password>
????????<s:password name="password2" label="確認密碼"></s:password>
????<s:submit value="注冊"></s:submit>
??????</s:form>
????</body>
</html> success.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"
????????pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
??<h3>注冊成功</h3>
??用戶名:<s:property value="username"/><p>
??密碼:????<s:property value="password1"/>
</body>
</html> 后臺輸出結果: 攔截器信息:hello world 攔截器
Action信息,正在執行Action....????
2010-11-1 20:39:11 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
攔截器信息:Action執行完畢
                        
                        
                        ??//以下屬性信息都是從前臺(JSP頁面獲得)
??private String username;
????
??private String mymsg;
????
??private String password1;
????
??private String password2;
????
??private Date birthday;
??public String execute(){
????if(username!=null&&this.getPassword1().equals(this.getPassword2())&&!this.getUsername().trim().equals("")){
????????
??????//輸出調試信息
??????System.out.println("Action信息,正在執行Action....??");
??????return SUCCESS;
????}else{
??????return INPUT;
????}
??}
????
????
??public String getUsername() {
????return username;
??}
??public void setUsername(String username) {
????this.username = username;
??}
??public String getMymsg() {
????return mymsg;
??}
??public void setMymsg(String mymsg) {
????this.mymsg = mymsg;
??}
??public String getPassword1() {
????return password1;
??}
??public void setPassword1(String password1) {
????this.password1 = password1;
??}
??public String getPassword2() {
????return password2;
??}
??public void setPassword2(String password2) {
????this.password2 = password2;
??}
??public Date getBirthday() {
????return birthday;
??}
??public void setBirthday(Date birthday) {
????this.birthday = birthday;
??}
????
} 自定義攔截器MyInterceptor.java public class MyInterceptor extends AbstractInterceptor {
??//攔截方法
??public String intercept(ActionInvocation invocation) throws Exception {
????MyAction myA=(MyAction) invocation.getAction();
????System.out.println("攔截器信息:hello world 攔截器");
????//執行action或者下一個攔截器
????String result=invocation.invoke();
????System.out.println("攔截器信息:Action執行完畢");
????return result;
??}
} 下面我們開始配置struts.xml文件 首先我定義了一個my.xml文件 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
????????"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
????????"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
????????<package name="mynew" namespace="/" extends="struts-default">
????????
??????????<interceptors>
????????????<interceptor name="myInterceptor" class="com.MyInterceptor">
????????????</interceptor>
??????????</interceptors>
????
????<action name="myAction" class="com.MyAction">
??????<result name="success">/success.jsp</result>
??????<result name="input">/index.jsp</result>
??????<!-- 引用默認攔截器 -->
??????<interceptor-ref name="defaultStack"></interceptor-ref>
??????<!-- 引用自定義攔截器 -->
??????<interceptor-ref name="myInterceptor"></interceptor-ref>
????</action>
????????</package>
</struts> 這樣的話就要在struts.xml引入, <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
????????"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
????????"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
????????<constant name="struts.devMode" value="true" />
??<include file="my.xml"></include>
</struts> 如果讀者不習慣的話完全可以寫在struts.xml文件中 index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
????<head>
????</head>
????
????<body>
??????<s:form method="post" action="myAction">????
????????<s:textfield name="username" label="用戶名"></s:textfield>
????????<s:password name="password1" label="密碼"></s:password>
????????<s:password name="password2" label="確認密碼"></s:password>
????<s:submit value="注冊"></s:submit>
??????</s:form>
????</body>
</html> success.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"
????????pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
??<h3>注冊成功</h3>
??用戶名:<s:property value="username"/><p>
??密碼:????<s:property value="password1"/>
</body>
</html> 后臺輸出結果: 攔截器信息:hello world 攔截器
Action信息,正在執行Action....????
2010-11-1 20:39:11 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
攔截器信息:Action執行完畢
轉載于:https://blog.51cto.com/youngforwork/830077
總結
以上是生活随笔為你收集整理的Struts2拦截器简单示例的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 数据结构基础(3) --Permuta
 - 下一篇: 最大似然估计【MLE】与最大后验概率【M