SelectAction Sample |
Using SelectAction
This simple example shows how to dispatch submit events from an HTTP form to handler methods.
Action Class
First, create a subclass of SelectAction:
import net.jspcontrols.dialogs.actions.SelectAction; import javax.servlet.ServletException; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletRequest; import java.util.Map; import java.util.HashMap; import java.io.IOException; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForward; public class SelectActionTest extends SelectAction { // Define mapping from button to method protected Map getKeyMethodMap() { Map map = new HashMap(); map.put(getInitKey()+"-ADD", "add"); map.put(getInitKey()+"-DELETE", "delete"); return map; } // Handler of Add button public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { System.out.println("--> add"); return mapping.findForward("addpage"); } // Handler of Delete button public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { System.out.println("--> delete"); return mapping.findForward("deletepage"); } // Handler of unknown button, which name does not start with button prefix public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { System.out.println("--> unspecified"); return mapping.findForward("unspecifiedpage"); } }
Notice, that unspecified is called when request does not contain parameters starting with prefix. If a button with a proper prefix is selected, but action class does not have a handler for this button, then exception is thrown instead of calling unspecified method.
Configuration
Here is the action mapping configuration in struts-config.xml file:
<struts-config> <form-beans> <!-- Struts requires, that a form bean must be defined for each action processing POST request. To comply with this rule, a dynabean is defined here, but it is not used --> <form-bean name="dummyForm" type="org.apache.struts.action.DynaActionForm"/> </form-beans> <action-mappings> <action path="/selectaction" type = "acme.SelectActionTest" name = "dummyForm"> <forward name = "addpage" path="/selectadd.html"/> <forward name = "deletepage" path="/selectdelete.html"/> <forward name = "unspecifiedpage" path="/selectunspecified.html"/> </action> </action-mappings> </struts-config>
Web pages
Starting page, selectinput.html:
<html> <h2>INPUT</h2> <form action="http://localhost:8080/strutsdialog/selectaction.do" method="post"> <input type="submit" name="DIALOG-EVENT-ADD" value="Add button" /><br><br> <input type="submit" name="DIALOG-EVENT-DELETE" value="Delete button fancy caption" /><br><br> <input type="submit" name="DIALOG-EVENT-unknown" value="No corresponding handler (exception)" /><br><br> <input type="submit" name="some-garbage" value="Does not start with prefix (unspecified)" /> </form> </html>
Page shown when "Add" button selected, selectadd.html:
<html> <h2>ADD</h2> </html>
Page shown when "Delete" button selected, selectdelete.html:
<html> <h2>DELETE</h2> </html>
Page shown when unspecified button selected, selectunspecified.html:
<html> <h2>UNSPECIFIED</h2> </html>