【Struts2学习笔记(2)】Action默认值和配置Action于result各种转发类型

一、Action缺省配置值

<span style="font-size:18px;"><package name="itcast" namespace="/test" extends="struts-default">
        <action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute" >
	<result name="success">/WEB-INF/page/hello.jsp</result>
        </action>
  </package> 
1>假设没有为action指定class,默认是ActionSupport。

2>假设没有为action指定method,默认运行action中的execute() 方法。 3>假设没有指定result的name属性。默认值为success。</span>


二、Action中result的各种转发类型

<span style="font-size:18px;"><action name="helloworld" class="cn.itcast.action.HelloWorldAction">
	<result name="success">/WEB-INF/page/hello.jsp</result>
</action>

(1)result配置类似于struts1中的forward,但struts2中提供了多种结果类型,经常使用的类型有: dispatcher(默认值)、 redirect 、 redirectAction 、 plainText。

(2)在result中还能够使用${属性名}表达式訪问action中的属性,表达式里的属性名相应action中的属性。例如以下:
<result type="redirect">/view.jsp?id=${id}</result>

(3)以下是redirectAction 结果类型的样例,假设重定向的action中同一个包下: 

<action name="redirectAction">
			<result type="redirectAction">helloworld</result>
		</action>

假设重定向的action在别的命名空间下:

<struts>
    
	<package name="itcast" namespace="/control/employee" extends="base">	
		
		<action name="list" class="cn.itcast.action.HelloWorldAction" method="execute">
			<result name="success" type="redirect">/employeeAdd.jsp?username=${username}</result>
		</action>
		
		<action name="redirect">
			<result type="redirect">/employeeAdd.jsp</result>
		</action>
		<action name="redirectAction"> <!-- 不同的命名空间下的 -->
			<result type="redirectAction">
				<param name="actionName">xxx</param> <!-- action的名字-->
				<param name="namespace">/control/department</param>
			</result>
		</action>
	</package>
	
	<package name="other" namespace="/control/department" extends="base">
		<action name="xxx">
			<result>/WEB-INF/page/hello.jsp</result>
		</action>
	</package>
</struts>


(4)plaintext:显示原始文件内容,比如:当我们须要原样显示jsp文件源码 的时候。我们能够使用此类型。

<result name="source" type="plainText ">
	<param name="location">/xxx.jsp</param>
	<param name="charSet">UTF-8</param><!-- 指定读取文件的编码 -->
</result>





版权声明:本文博主原创文章,博客,未经同意不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值