一、struts2为action的属性注入值
struts2为action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为action
中的属性注入值。注意:属性必须提供setter方法。
public class HelloWorldAction{
private String savePath;
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
......
}
配置文件如下:
<package name="itcast" namespace="/test" extends="struts-default"> <action name="helloworld" class="cn.itcast.action.HelloWorldAction" > <param name="savePath">/images</param> <result name="success">/WEB-INF/page/hello.jsp</result> </action> </package>
上面通过<param>节点为action的savePath属性注入“/images”
你有hello.jsp页面中可以得到注入的属性值,${savePath},则访问test.action时,hello.jsp页面就会打印出/images值。
二、为struts2指定多个配置文件
在大部分应用里,随着应用规模的增加,系统中Action数量也大量增加,导致struts.xml配置文件变得非常臃肿。为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中通过include包含其他配置文件。下面的struts.xml通过<include>元素指定多个配置文件:
<?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> <include file="struts-user.xml"/> <include file="struts-order.xml"/> </struts>
通过这种方式,我们就可以将Struts 2的Action按模块配置在多个配置文件中。