トップページ >  Struts >  DispatchAction
初版2007/02/09: 最終更新日2013/11/23
  DispatchAction
目次
DispatchAction
index.jsp
ActionForm
struts-config.xml
DispatchAction
StrutsのDispatchActionを継承すると、一つのフォームで複数のボタンの処理を振り分けることが可能になります。
以下のような画面があるとします。

ログイン画面(index.jsp)


execボタンもcancelボタンも一つのアクションで振り分けを行い、それぞれの画面に遷移できるようにします。
これを実現するにはActionクラスがDispatchActionを継承する必要があります。
以下、execボタンを押した場合、execメソッドが実行され、cancelボタンを実行した場合、cancelメソッドが実行されるように振り分けられています。

package jp.co.confrage.action;

import java.io.IOException;

import javax.servlet.ServletException;
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;

public class LoginDispatchAction extends DispatchAction {

    public ActionForward exec(ActionMapping mapping, ActionForm _form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        
        return mapping.findForward("success1");
    }

    public ActionForward cancel(ActionMapping mapping, ActionForm _form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        
        return mapping.findForward("success2");
    }
}

index.jsp
上図のindex.jspのソースは以下の通りです。valueの値がそのままメソッド名となります。propertyはstruts-config.xmlのactionタグのparameter属性と同じにします。

<%@ page contentType="text/html; charset=Windows-31J" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<html:html>
  <head>
    <title>サンプルログイン</title>
    <meta http-equiv="Content-Type" 
                  content="text/html; charset=Windows-31J">
  </head>
<body>

<html:form action="/dispatch_form">
  Login画面<br /><br />
  <html:text property="id" size="10" maxlength="8" />
  <html:text property="password" size="10" maxlength="8" />
  <html:submit property="dispatch" value="exec" >実行</html:submit>
  <html:submit property="dispatch" value="cancel" >取消</html:submit>
</html:form>
</body>
</html:html>

ActionForm
ActionFormは通常のように記述します。今回はテキストフォームが二つあるのでそのプロパティとゲッターセッターを用意します。

package jp.co.confrage.form;

import org.apache.struts.action.ActionForm;

public class LoginDispatchForm extends ActionForm{
	private String id = null;
	
	private String password = null;

	public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

	public void setPassword(String password) {
        this.password = password;
    }
	
    public String getPassword() {
        return password;
    }
    
}

struts-config.xml
struts-config.xmlは以下のように記述します。

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE struts-config PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
        "http://struts.apache.org/dtds/struts-config_1_3.dtd">

    <form-bean name="dispatchform" type="jp.co.confrage.form.LoginDispatchForm">
      <form-property name="id" type="java.lang.String" initial="id" />
      <form-property name="password" type="java.lang.String" initial="pass" />
    </form-bean>
  </form-beans>

  <action-mappings>
    <action path="/sample_form"
      type="jp.co.confrage.action.TestValidatorLoginAction"
     name="test_form"
     scope="request"
     input="/WEB-INF/pages/error.jsp">
      <forward name="success" path="/WEB-INF/pages/login.jsp" />
    </action>
    <action path="/dispatch_form"
     type="jp.co.confrage.action.LoginDispatchAction"
     parameter="dispatch"
     name="dispatchform"
     input="/WEB-INF/pages/error.jsp">
      <forward name="success1" path="/WEB-INF/pages/login2.jsp" />
      <forward name="success2" path="/WEB-INF/pages/login3.jsp" />
    </action>
</action-mappings>

<message-resources parameter="MessageResources"/>

</struts-config>

以下、index.jspです。



execボタンを押すとlogin2.jspに遷移します。