2009-07-24

Struts Dynamic checkboxes

package com.strutsrecipes;  import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping;  public final class CheckboxTestForm extends ActionForm {  // Instance Variables  /*Mountains "pre-selected"...*/ private String[] selectedMountains = {"Everest","K2","Lhotse"};  /*the ten tallest Mountains to iterate through*/ private String[] mountains = {"Everest","K2","Kangchenjunga","Lhotse", "Makalu","Kangchenjunga South", "Lhotse Middle","Kangchenjunga West", "Lhotse Shar","Cho Oyu"};  /*Getter for selectedMountains*/ public String[] getSelectedMountains() { return this.selectedMountains; }  /*Setter for selectedMountains*/ public void setSelectedMountains(String[] selectedMountains) { this.selectedMountains = selectedMountains; }  /*Getter for the mountains*/ public String[] getMountains() { return this.mountains; }  /*Setter for the mountains*/ public void setMountains(String[] mountains) { this.mountains = mountains; } } 

 

 
##########################
 
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

<%-- html code, etc... -->

<html:form
action="/FormAction"
name="testForm"
type="com.strutsrecipes.CheckboxTestForm">

<h4><bean:message key="testForm.instruction"/></h4>

<logic:iterate name="testForm"
property="mountains"
id="mountain">

<%-- create the checkbox and selected attribute -->
<html:multibox property="selectedMountains">
<bean:write name="mountain"/>
</html:multibox>

<%-- create the label, note that "br" tag will format it vertically -->
<bean:write name="mountain"/><br/>

</logic:iterate>

<br/>
<html:submit/><html:reset/>

</html:form>

<%-- some more html code, etc... -->

#########################
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

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

/**
* A simple Action for Checkbox test.
*
* @author Danilo Gurovich
*/
public final class CheckboxTestAction
extends Action {
// -------------------------- OTHER METHODS --------------------------

/**
* The execute method
*
* @param mapping ActionMapping
* @param form CheckboxTestForm
* @param request HttpServletRequest
* @param response HttpServletRespons
* @return success to the confirmation page
* @throws ServletException not thrown, but could be!
* @throws Exception ditto.
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, Exception {

// Extract attributes needed
String[] selectedMountains =
((CheckboxTestForm) form).getSelectedMountains()
;

System.out.println("htmlString RETURNED*\n" +
selectedMountains.toString());

//Save the htmlString in the session for later...
HttpSession session = request.getSession();
session.setAttribute(CheckboxConstants.MOUNTAINS, selectedMountains);

return (mapping.findForward("success"));
}
}
 
 
###################
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

<%-- html code, etc... -->

<logic:iterate id="mountain" property="mountains" name="testForm">
<bean:write name="mountain"/><br/>
</logic:iterate>
<hr size=5 color="black"/>

<%-- some more html code, etc... -->

0 留言: