Date: 2004-04-29T09:25:55 Editor: 131.191.40.91 <> Wiki: Apache Struts Wiki Page: StrutsCatalogMultipleImageButtonsWithNoJavaScript URL: http://wiki.apache.org/struts/StrutsCatalogMultipleImageButtonsWithNoJavaScript
no comment Change Log: ------------------------------------------------------------------------------ @@ -1 +1,327 @@ -Describe StrutsCatalogMultipleImageButtonsWithNoJavaScript here. +StrutsCatalog: '''I don't know about you ..., but ... I like code. I hate HTML. Here is a way to take care of that recurrent problem of how to get around the browser limitations and use multiple image buttons that crops up continually on the lists.''' + +This code is the composite of the work of lots of people and they also built on those before them. References to them is at the bottom, but I wanted to acknowledge this at the start. + +Here's the button I use. + +{{{ +public class CrackWillowButton implements Serializable { + private String name = null; + private Integer x = null; + private Integer y = null; + + public CrackWillowButton() { + } + + public boolean pressed() { + return !(name == null || name.trim().length() <= 0) || + (x != null || y != null); + } + + public String getName() { + return name; + } + + public Integer getX() { + return x; + } + + public Integer getY() { + return y; + } + + public void setName(String name) { + name = name; + } + + public void setX(Integer x) { + x = x; + } + + public void setY(Integer y) { + y = y; + } +} +}}} + +Here's the class that classifies the buttons into the operation they represent: + +{{{ +public class ButtonOperation implements Serializable { + protected CrackWillowButton submit = new CrackWillowButton(); + protected CrackWillowButton clear = new CrackWillowButton(); + protected CrackWillowButton cancel = new CrackWillowButton(); + protected CrackWillowButton reset = new CrackWillowButton(); + + public ButtonOperation() { + } + + public CrackWillowButton getCancel() { + return cancel; + } + + public CrackWillowButton getClear() { + return clear; + } + + public CrackWillowButton getSubmit() { + return submit; + } + + + public CrackWillowButton getReset() { + return reset; + } + + public void setCancel(CrackWillowButton cancel) { + this.cancel = cancel; + } + + public void setClear(CrackWillowButton clear) { + this.clear = clear; + } + + public void setSubmit(CrackWillowButton submit) { + this.submit = submit; + } + + public void setReset(CrackWillowButton reset) { + this.reset = reset; + } +} +}}} + +Here's an Action using these buttons for a registration form: + +{{{ +public class ButtonAction extends Action { + + public ActionForward execute(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws Exception { + ActionForward forward = ActionForwardLayout.getInputForward(mapping.getInputForward(), request); + ButtonForm pageForm = (ButtonForm) form; + + if (pageForm.getButton().getSubmit().pressed()) { + this.executeRegistration(mapping, form, request, response); + } else if (pageForm.getButton().getClear().pressed()) { + pageForm.reset(mapping, request); + } else if (pageForm.getButton().getCancel().pressed()) { + forward = mapping.findForward("cancel"); + } + + return forward; + } + + public ActionForward executeRegistration(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws Exception { + + // example only + return null; + } +} +}}} + +Here's the code !ActionForm for this action: + +{{{ +public class ButtonForm + extends ActionForm { + private ButtonOperation button = null; + private String firstName = null; + private String lastName = null; + private String email = null; + + public ButtonForm() { + } + + public void reset(ActionMapping mapping, HttpServletRequest request) { + button = new ButtonOperation(); + firstName = null; + lastName = null; + email = null; + } + + public ButtonOperation getButton() { + return button; + } + + public void setButton(ButtonOperation button) { + button = button; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public String getEmail() { + return email; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public void setEmail(String email) { + this.email = email; + } +} +}}} + +Here is the struts action mapping: + +{{{ +<action + path="/button" + type="com.crackwillow.struts.action.ButtonAction" + name="ButtonForm" + scope="request" + input="/button_test.jsp"> + <forward + name="cancel" + path="/button_test.jsp" + redirect="true"/> +</action> +}}} + +Here's the struts form definition: + +{{{ +<form-beans> + <form-bean + name="ButtonForm" + type="com.crackwillow.struts.form.ButtonForm" /> +</form-beans> +}}} + +Here's an example usage: + +{{{ +<%@ page language='java' %> +<%@ page contentType='text/html; charset=UTF-8' %> +<%@ taglib uri='struts-bean' prefix='bean' %> +<%@ taglib uri='struts-html' prefix='html' %> +<%@ taglib uri='struts-tiles' prefix='tiles' %> + +<html:html> + <head> + <title>HTML Buttons Example</title> + <link + rel="STYLESHEET" + type="text/css" + href="resource.do?file_type=css&file_name=index.css"> + </head> + <body + background="resource.do?file_type=gif&file_name=green.gif" + text="#ffffff"> + <h1 + align="center"> + HTML Buttons Example + </h1> + <table + border="0" + align="center" + width="80%" + cellpadding="3" + cellspacing="0"> + <tr> + <td> + <h1> + Registration Form + </h1> + </td> + </tr> + </table> + <html:form + name="ButtonForm" + type="com.crackwillow.struts.form.ButtonForm" method="get" + action='/button.do'> + <table + border="0" + align="center" + width="80%" + cellpadding="3" + cellspacing="0"> + <tr> + <td + width="1%" + nowrap> + First name: + </td> + <td> + <input + type="text" + name="firstName" + value=""> + </td> + </tr> + <tr> + <td + width="1%" + nowrap> + Last name: + </td> + <td> + <input + type="text" + name="lastName" + value=""> + </td> + </tr> + <tr> + <td + width="1%" + nowrap> + Email: + </td> + <td> + <input + type="text" + name="ssn" + value=""> + </td> + </tr> + <tr> + <td + width="1%" + nowrap> + + </td> + <td> + <br><br> + <html:image + property="button.submit" + src="resource.do?file_type=gif&file_name=cw_logo.gif"/> + <html:image + property="button.clear" + src="resource.do?file_type=gif&file_name=cw_logo.gif"/> + <html:image + property="button.cancel" + src="resource.do?file_type=gif&file_name=cw_logo.gif"/> + <html:image + property="button.reset" + src="resource.do?file_type=gif&file_name=cw_logo.gif"/> + </td> + </tr> + </table> + </html:form> + </body> +</html:html> +}}} + +If you want to see the !ResourceAction in this code, see StrutsCatalogEschewUrlForProtocol. + +-- Michael !McGrady --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]