打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
struts2.x中的Action配置
在struts.xml中配置Action

格式:
<package name="sunny" extends="struts-default" >
   <action name="myregist" class="com.sunny.LoginAction" method="regist">
           <result name="input">/login.jsp</result>
           <result name="error">/error.jsp</result>
           <result name="success">/welcome.jsp</result>
      </action>
</package>
当页面中有标签<s:form action="myregist"/>时,提交后将执行LoginAction类中的regist方法。
基本原则:
    name属性必须指定。表示引用的 url。
 class属性可以省略,省略则表示调用默认的ActionSupport类作为逻辑处理类。
    method表示执行 Action中的哪个方法,这些方法的原型应与execute一致;若省略,则调用execute()方法。使用这个属性可将一个Action定义成多个逻辑Action.
若将package的配置改成:<package name="sunny" extends="struts-default" namespace="/tt"> ,则form标签应改成<s:form action="tt/myregist" /> 包的名称在逻辑上将不同的Action组成一个包,这个包与类本身所在的包无关。
-------------------------------------------------------------------------------------------------
通配符的使用:
若2个action基本相似,如:
   <package name="sunny" extends="struts-default" namespace="/tt">
      <action name="login" class="com.sunny.LoginAction" method="login">
         <result name="input">/login.jsp</result>
         <result name="error">/error.jsp</result>
         <result name="success">/welcome.jsp</result>
      </action>
      <action name="regist" class="com.sunny.LoginAction" method="regist">
         <result name="input">/login.jsp</result>
         <result name="error">/error.jsp</result>
         <result name="success">/welcome.jsp</result>
      </action>
    </package>  
这时可以使用通配将它们统一为一个action配置,改为:
<package name="sunny" extends="struts-default" namespace="/tt">
      <action name="*" class="com.sunny.LoginAction" method="{1}">
         <result name="input">/login.jsp</result>
         <result name="error">/error.jsp</result>
         <result name="success">/welcome.jsp</result>
      </action>
</package>  
这时,表达式{1}表示第1个*,{2}表示第2个*(这个例子中未使用)。若有请求tt/regist,则调用LoginAction类的regist方法,若有tt/login请求,则调用login方法。这些方法都应该不带参数,返回String类型的值。
如下也是一个不错的配置方案:<action name="*_*" class="com.sunny.{1}" method="{2}">,这样,LoginAction类中的login方法,请求url是LoginAction_login.
若<action name="*" class="com.sunny.LoginAction" method="{1}"> 改成<action name="*Action" class="com.sunny.LoginAction" method="{1}">,则遇到tt/registAction请求,调用regist方法,遇到 tt/loginAction请求,则调用login方法。
也可在result子元素中使用表达式,如:
<action name="*">
    <result>/{1}.jsp</result>
</action>
此时表示遇到请求a,则调用a.jsp. 原因:上面未指定Action,则调用默认的ActionSupport类,而该类的execute方法总是返回SUCCESS,而<result></result>未指定结果字符串,表示是默认的SUCCESS的配置,因此,遇到请求a,则转向a.jsp页面。
而下面的配置将列出根路径下的所有文件:
<action name="">
    <result>.</result>
</action>
这个配置一开始时测试未通过,出现HTTP Status 404 - There is no Action mapped for namespace / and action name .的错误。后来发现是浏览器缓存的问题,清除历史记录后再试则OK了。
在action和result的配置段中,还可以使用OGNL表达式来指定属性值,如相应的Action类中有target属性,则<result type="redirect">/${target}.jsp</result>可以转向Action实例中target属性指出的页面。这里的type指出转向的资源的类型,其它结果类型还有 dispatcher类型(默认,转发),redirect-action(直接转向其它Action),plainText(显示指向资源的源代码),freemarker,velocity,chain(Action链式处理的结果类型).
<result type="redirect-action">
   <param name="actionName" >NewAction</param>
   <param name="namespace">/users</param>
</result>
全局结果配置,还是在<package>内配置:
<global-results><result name="error" >/err.jsp</result></global-results>


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
第二章 一个简单的Login 应用程序(Struts2)
第一次接触struts2.......
类拦截器和方法拦截器
2. struts2中一个Action处理多个方法
一个Action内包含多个请求处理方法的处理-JAYSON-搜狐博客
Struts 2 – No result defined for action and result input
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服