struts2为action的属性注入值、指定多个配置文件

一、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按模块配置在多个配置文件中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值