一个简单的DispatchAction的应用

1. input.jsp: 只是提供两本基本的按键:update和delete,当然用户可以增加其它的按键,然后在Action加入其它值的
public ActionForward otherValue(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {       }
比如你加入了一个<html:submit property=“method“ value=“addOrder“/>
那么就要在DispatchAction中加入

public ActionForward addOrder(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {
            UserManipulcationForm userManipulcationForm = (UserManipulcationForm) form;
            /*
             * 其它的一些代码
             */
            return mapping.findForward("delete");
        }

 

 

input.jsp的代码:<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
 <body>
  <html:form action="/userManipulcation">
   name : <html:text property="name"/><html:errors property="name"/></br>
   address : <html:text property="address"/><html:errors property="address"/></br>
   <html:submit property="method" value="update"/>
   <html:submit property="method" value="delete"/>
  </html:form>
 </body>
</html>

2. update.jsp: 当用户按update键在input.jsp中,最后转到update.jsp

<html>
  <body>
    This is the Update Page<br>
  </body>
</html>

3. delete.jsp: 当用户按delete键在input.jsp中,最后转到delete.jsp

<html>
  <body>
    This is the Delete Page<br>
  </body>
</html>

 

4. 我的DispatchAction:如下
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.0/xslt/JavaClass.xsl

package com.yourcompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.yourcompany.struts.form.UserManipulcationForm;

/**
 * MyEclipse Struts
 * Creation date: 08-23-2004
 *
 * XDoclet definition:
 * @struts:action path="/userManipulcation" name="userManipulcationForm" input="/form/userManipulcation.jsp" parameter="type" scope="request"
 */
public class UserManipulcationAction extends DispatchAction {

    // --------------------------------------------------------- Instance Variables

    // --------------------------------------------------------- Methods

    /**
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward update(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) {
        UserManipulcationForm userManipulcationForm = (UserManipulcationForm) form;
        /*
         * 其它的一些代码对应其按update键
         */
        return mapping.findForward("update");
    }
   
    public ActionForward delete(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {
            UserManipulcationForm userManipulcationForm = (UserManipulcationForm) form;
            /*
             * 其它的一些代码对应其按delete键
             */
            return mapping.findForward("delete");
        }

}

要注意得就是,对就按的键中的value不同,DispatchAction用自动执行其它想对应value的方法。。

 

其它的:

ActionForm:

//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.0/xslt/JavaClass.xsl

package com.yourcompany.struts.form;

import org.apache.struts.action.ActionForm;

/**
 * MyEclipse Struts
 * Creation date: 08-23-2004
 *
 * XDoclet definition:
 * @struts:form name="userManipulcationForm"
 */
public class UserManipulcationForm extends ActionForm {

    // --------------------------------------------------------- Instance Variables

    /** address property */
    private String address;

    /** name property */
    private String name;
   
    /** method property */
    private String method;

    // --------------------------------------------------------- Methods

    /**
     * Returns the address.
     * @return String
     */
    public String getAddress() {
        return address;
    }

    /**
     * Set the address.
     * @param address The address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * Returns the name.
     * @return String
     */
    public String getName() {
        return name;
    }

    /**
     * Set the name.
     * @param name The name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    public String getMethod() {
        return method;
    }
    public void setMethod(String method) {
        this.method = method;
    }
}

 

 

 

 

Struts-Config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
   <data-sources />
   <form-beans >
      <form-bean name="userManipulcationForm" type="com.yourcompany.struts.form.UserManipulcationForm" />

   </form-beans>

   <global-exceptions />
   <global-forwards />
   <action-mappings >
      <action
         attribute="userManipulcationForm"
         input="/form/userManipulcation.jsp"
         name="userManipulcationForm"
         parameter="method"
         path="/userManipulcation"
         scope="request"
         type="com.yourcompany.struts.action.UserManipulcationAction"
         validate="false" >
         <forward name="update" path="/update.jsp" />
         <forward name="delete" path="/delete.jsp" />
      </action>


   </action-mappings>

   <controller bufferSize="4096" debug="0" />
   <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

 

要注意的就是。。
DispatchAction是通过在struts-config.xml中定义的         parameter="method"
来区别不同的method的值执行不同的方法。。。

就<html:submit property=”method” value=”update”/>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值