【原創】Struts 2雜談(1):ValueStack物件的傳送帶機制

銀河使者發表於2009-02-14
本文為原創,如需轉載,請註明作者和出處,謝謝!

    眾所周知,Strut 2的Action類通過屬性可以獲得所有相關的值,如請求引數、Action配置引數、向其他Action傳遞屬性值(通過chain結果)等等。要獲得 這些引數值,我們要做的唯一一件事就是在Action類中宣告與引數同名的屬性,在Struts 2呼叫Action類的Action方法(預設是execute方法)之前,就會為相應的Action屬性賦值。
    要完成這個功能,有很大程度上,Struts 2要依賴於ValueStack物件。這個物件貫穿整個Action的生命週期(每個Action類的物件例項會擁有一個ValueStack物件)。當 Struts 2接收到一個.action的請求後,會先建立Action類的物件例項,並且將Action類的物件例項壓入ValueStack物件中(實際 上,ValueStack對於相當一個棧),而ValueStack類的setValue和findValue方法可以設定和獲得Action物件的屬性 值。Struts 2中的某些攔截器正是通過ValueStack類的setValue方法來修改Action類的屬性值的。如params攔截器用於將請求引數值對映到相 應成Action類的屬性值。在params攔截器中在獲得請求引數值後,會使用setValue方法設定相應的Action類的屬性。
    從這一點可以看出,ValueStack物件就象一個傳送帶,當客戶端請求.action時,Struts 2在建立相應用Action物件後就將Action物件放到了ValueStack傳送帶上,然後ValueStack傳送帶會帶著Action物件經過 若干攔截器,在每一攔截器中都可以通過ValueStack物件設定和獲得Action物件中的屬性值。實際上,這些攔截器就相當於流水線作業。如果要對 Action物件進行某項加工,再加一個攔截器即可,當不需要進行這項工作時,直接將該攔截器去掉即可。
    下面我們使用一個例子來演示這個過程。在這個例子中實現了一個攔截器,該攔截器的功能是將一個屬性檔案中的key-value對對映成相應的屬性的值。如下面是一個屬性檔案的內容:

    name = 超人
    price = 10000

    我們可以在Action類中定義name和price屬性,在Action中引用這個攔截器後,就會自動為屬性賦值。
    在使用該攔截器有如下規則:
    1.  攔截器讀取的屬性檔案路徑由path引數指定。
    2.  屬性檔案的編碼格式由encoding引數指定,預設值是UTF-8。
    3.  如果某個key中包含有“.”(該符號不能出現在識別符號中),則有如下處理方法:
    (1)將Action類的屬性名定義為去掉“.”的key。例如,key為person.name,而屬性名可定義為personname。
    (2)將Action類的屬性名定義為將“.”替換成其他字元的表示符號。例如,key為person.name,而屬性名可定義為person_name,其中“_”由separator引數指定。
    4.  如果key太長,也可以直接使用Action引數進行對映,例如,key為country.person.name,可做如下對映:
      name
      要注意的是,name屬性值不能包含“.”,因此,應將key值中的“.”去掉。現在就可以直接在Action類中定義名為name的屬性的,name屬性的值會與key值相同。
    5.  上面所有的規則可以同時使用。

攔截器的原始碼:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtpackage interceptors;

import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.io.InputStream;
import java.io.FileInputStream;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.ValueStack;

public class PropertyInterceptor extends AbstractInterceptor
{
    
private static final String DEFAULT_PATH_KEY = "path";
    
private static final String DEFAULT_ENCODING_KEY = "encoding";
    
private static final String DEFAULT_SEPARATOR_KEY = "separator";

    
protected String pathKey = DEFAULT_PATH_KEY;
    
protected String encodingKey = DEFAULT_ENCODING_KEY;
    
protected String separatorKey = DEFAULT_SEPARATOR_KEY;

    
public void setPathKey(String pathKey) 
    {
        
this.pathKey = pathKey;
    }

    
public void setEncodingKey(String encodingKey)
    {
        
this.encodingKey = encodingKey;
    }

    
public void setSeparatorKey(String separatorKey)
    {
        
this.separatorKey = separatorKey;
    }

    @Override
    
public String intercept(ActionInvocation invocation) throws Exception
    {
        ActionConfig config 
= invocation.getProxy().getConfig();

        Map
<String, String> parameters = config.getParams();
        
if (parameters.containsKey(pathKey))
        {
            String path 
= parameters.get(pathKey);
            String encoding 
= parameters.get(encodingKey);
            String separator 
= parameters.get(separatorKey);
            
if (encoding == null)
                encoding 
= "UTF-8";
            
if (separator == null)
                separator 
= "";
            path 
= invocation.getAction().getClass().getResource(path)
                    .getPath();
            Properties properties 
= new Properties();
            InputStream is 
= new FileInputStream(path);
            java.io.Reader reader 
= new java.io.InputStreamReader(is, encoding);
            
            properties.load(reader);
            ActionContext ac 
= invocation.getInvocationContext();
            ValueStack stack 
= ac.getValueStack();
            System.out.println(stack.hashCode());
            Enumeration names 
= properties.propertyNames();
            
while (names.hasMoreElements())
            {
                
//  下面會使用setValue方法修改ValueStack物件中的相應屬性值
                String name = names.nextElement().toString();
                
if (!name.contains("."))
                    stack.setValue(name, properties.get(name)); 

                String newName 
= null;
                newName 
= parameters.get(name.replaceAll("\\."""));
                
if (newName != null)
                    stack.setValue(newName, properties.get(name));

                
if (!separator.equals(""))
                {
                    newName 
= name.replaceAll("\\.""");
                    stack.setValue(newName, properties.get(name));
                }               
                newName 
= name.replaceAll("\\.", separator);
                stack.setValue(newName, properties.get(name));
            } 
        }
        
return invocation.invoke();
    }
}

用於測試的Action類的原始碼:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtpackage actions;

public class MyAction
{
    
private String name;
    
private Integer price;
    
private String log4jappenderstdout;
    
private String log4j_rootLogger;
    
private String conversionPattern;

    
public String getName()
    {
        
return name;
    }

    
public void setName(String name)
    {
        
this.name = name;
    }

    
public Integer getPrice()
    {
        
return price;
    }

    
public void setPrice(Integer price)
    {
        
this.price = price;
    }

    
public String getLog4jappenderstdout()
    {
        
return log4jappenderstdout;
    }

    
public void setLog4jappenderstdout(String log4jappenderstdout)
    {
        
this.log4jappenderstdout = log4jappenderstdout;
    }

    
public String getLog4j_rootLogger()
    {
        
return log4j_rootLogger;
    }

    
public void setLog4j_rootLogger(String log4j_rootLogger)
    {
        
this.log4j_rootLogger = log4j_rootLogger;
    }

    
public String getConversionPattern()
    {
        
return conversionPattern;
    }

    
public void setConversionPattern(String conversionPattern)
    {
        
this.conversionPattern = conversionPattern;
    }

    
public String execute()
    {
        System.out.println(
"name:" + name);
        System.out.println(
"price:" + price);
        System.out.println(
"log4jappenderstdout:" + log4jappenderstdout);
        System.out.println(
"log4j_rootLogger:" + log4j_rootLogger);
        System.out.println(
"conversionPattern:" + conversionPattern);
        
return null;
    }
}

Action類的配置程式碼如:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtxml version="1.0" encoding="UTF-8" ?>
DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd"
>
<struts>
    
<package name="struts" extends="struts-default">

        
<interceptors>
            
<interceptor name="property"
                class
="interceptors.PropertyInterceptor" />
            
<interceptor-stack name="myStack">
                
<interceptor-ref name="defaultStack" />
                
<interceptor-ref name="property" />
            
interceptor-stack>
        
interceptors>
        
<action name="test" class="actions.MyAction">
            
<interceptor-ref name="myStack" />
            
<param name="path">/log4j.propertiesparam>
            
<param name="encoding">UTF-8param>
            
<param name="separator">_param>
            
<param name="log4jappenderstdoutlayoutConversionPattern">
                conversionPattern
            
param>

        
action>
    
package>
struts>

  請將log4j.properties檔案複製到WEB-INF\classes目錄,並在該檔案中加入name和price屬性。

測試結果:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtname:中國
price:
34
log4jappenderstdout:org.apache.log4j.ConsoleAppender
log4j_rootLogger:error
, stdout
conversionPattern:%d{ABSOLUTE} %5p %c{
1}:%L - %m%n

    由於property攔截器在defaultStack後引用,因此,在該攔截器中設定的屬性值是最終結果,如果將property攔截器放在 defaultStack前面(將兩個元素掉換一下),就可以通過同名勝Action配置引數或請求引數 來干預最終究輸出結果了。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12921506/viewspace-551928/,如需轉載,請註明出處,否則將追究法律責任。

相關文章