Skip navigation




Using Struts EventDispatchAction


Discussion
When using more than two

<input type="image" src="image/submit_btn.gif" border="0" alt="Submit"/>


tags as submit button to submit form data to request, Struts 1.1’s LookupDispatchAction doesn’t work.

for example:

<!DOCTYPE HTML 
  PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page 
  contentType="text/html;charset=UTF-8"
    language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
 <title>EventDispatchAction Example</title>
  <META http-equiv="Content-Type" content="text/html; 
  charset=UTF-8"/>
  <link href="css/style.css" 
  rel="stylesheet" type="text/css" >
</head>
<body>
<html:form action="/gkyEventDispatch">
<TABLE border="0" cellspacing="0" cellpadding="0" width="700">
  <TR>
    <TD>Name</TD>
    <TD>
      <html:text property="name" styleClass="textInput"/>
    </TD>
  </TR>
  <TR>
    <TD>
      <html:image property="search" 
        src="image/submit_search_btn.gif" 
        border="0"/>
    </TD>
    <TD>
      <html:image property="add" 
        src="image/submit_add_btn.gif" 
        border="0"/>
      </TD>
  </TR>
</TABLE>
</html:form>
</body>
</html>

Solution

Using EventDispatchAction instead of LookupDispatchAction to implement one form multi-image-submit button.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  "http://struts.apache.org/dtds/struts-config_1_3.dtd" >
<struts-config>
  <form-beans>
    <form-bean name="gkyForm" type="jp.co.gky.form.GkyForm"/>
  </form-beans>
  <action-mappings>
    <action
        path="/gkyInit"
        type="jp.co.gky.action.GkyInitAction"
        name="gkyForm"
        scope="request">
      <forward name="gkyInitOk" path="/gkyInput.jsp"/>
    </action>
    <action
        path="/gkyEventDispatch"
        type="jp.co.gky.action.GkyEventDispatchAction"
        name="gkyForm"
        input="/gkyInput.jsp"
        validate="false"
        parameter="add=doAdd,search=doSearch"
        scope="request">
      <forward redirect="false" 
          name="doAddOk" path="/gkyInit.do"/>
      <forward redirect="false" 
          name="doSearchOk" path="/gkyInit.do"/>
    </action>
  </action-mappings>
  <message-resources parameter="MessageResource"/>
 
</struts-config>

public class GkyEventDispatchAction 
                        extends EventDispatchAction {
  public ActionForward doAdd(
      ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) 
      throws Exception {
    ActionErrors errors = new ActionErrors();
    if(!isTokenValid(request)) {
      errors.add(ActionMessages.GLOBAL_MESSAGE, 
          new ActionMessage("errors.token"));
    }
    resetToken(request);
    if(!errors.isEmpty()) {
      saveErrors(request, errors);
      saveToken(request);
      return mapping.getInputForward();
    }
    // .....
    request.setAttribute("newKeyValue", reqMsg);
    return mapping.findForward("doAddOk");
  }

  public ActionForward doSearch(
      ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    ActionErrors errors = new ActionErrors();
    if(!isTokenValid(request)) {
      errors.add(ActionMessages.GLOBAL_MESSAGE, 
          new ActionMessage("errors.token"));
    }
    resetToken(request);
    if(!errors.isEmpty()) {
      saveErrors(request, errors);
      saveToken(request);
      return mapping.getInputForward();
    }
    //....
    request.setAttribute("searchKeyValue", reqMsg);
    return mapping.findForward("doSearchOk");
  }
}

Leave a comment