Struts2 Actions动作


当前第2页 返回上一页

让我们输入一个单词为“SECRET”,你会看到如下页面:

现在输入除“SECRET”之外的任何词,你会看到如下页面:

创建多个Actions

你会需要频繁定义多个action来处理不同的请求,并为用户提供不同的URL,因此你将如下定义的不同类:

package cn.Vue5教程.struts2;
import com.opensymphony.xwork2.ActionSupport;

   class MyAction extends ActionSupport{
      public static String GOOD = SUCCESS;
      public static String BAD = ERROR;
   }

   public class HelloWorld extends ActionSupport{
      ...
      public String execute()
      {
         if ("SECRET".equals(name)) return MyAction.GOOD;
         return MyAction.BAD;
      }
      ...
   }

   public class SomeOtherClass extends ActionSupport{
      ...
      public String execute()
      {
         return MyAction.GOOD;
      }
      ...
   }

你将如下在struts.xml文件中的配置这些action:

<?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>
 <constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">
      <action name="hello" 
         class="cn.Vue5教程.struts2.HelloWorld" 
         method="execute">
         <result name="success">/HelloWorld.jsp</result>
         <result name="error">/AccessDenied.jsp</result>
      </action>
      <action name="something" 
         class="cn.Vue5教程.struts2.SomeOtherClass" 
         method="execute">
         <result name="success">/Something.jsp</result>
         <result name="error">/AccessDenied.jsp</result>
      </action>
   </package>
</struts>

正如你在上面的例子中看到的,action的结果SUCCESS和ERROR是重复的。为了解决这个问题,建议您创建一个包含结果的类。



标签:Struts2

返回前面的内容

相关阅读 >>

Struts2 数据库访问

Struts2 注释

Struts2 spring集成

Struts2 验证框架

Struts2 异常处理

Struts2 文件上传

Struts2 actions动作

Struts2 tiles集成

Struts2 本地化国际化(i18n)

Struts2 数据标签

更多相关阅读请进入《Struts2》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...