5. 上面所有的规则可以同时使用。
拦截器的源代码:
package 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类的源代码:
package 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类的配置代码如:
<?xml 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.properties</param> <param name="encoding">UTF-8</param> <param name="separator">_</param> <param name="log4jappenderstdoutlayoutConversionPattern"> conversionPattern </param> </action> </package> </struts> <?xml 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.properties</param> <param name="encoding">UTF-8</param> <param name="separator">_</param> <param name="log4jappenderstdoutlayoutConversionPattern"> conversionPattern </param> </action> </package> </struts>请将log4j.properties文件复制到WEB-INF/classes目录,并在该文件中加入name和price属性。