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

it2025-07-26  6

一、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>

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

转载于:https://www.cnblogs.com/bhlsheji/p/4842279.html

相关资源:数据结构—成绩单生成器
最新回复(0)