Created
March 24, 2011 10:46
-
-
Save andrewspencer/884865 to your computer and use it in GitHub Desktop.
Multiple dynamic checkboxes in Struts 1.x using DisplayTag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.andrewspencer.gist.multiple-checkboxes-struts; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import javax.servlet.http.HttpServletRequest; | |
import org.apache.struts.action.ActionForm; | |
import org.apache.struts.action.ActionMapping; | |
/** | |
* @param T The type of bean to which you want to add checkboxes | |
*/ | |
public class FbMultipleCheckableBeans<T> extends ActionForm { | |
private static final long serialVersionUID = 1L; | |
private Set<String> checkedIds; | |
private List<T> items; | |
/** | |
* Returns "true" if the item with the specified id is checked, | |
* otherwise "false". | |
*/ | |
public String getChecked(String id) { | |
return String.valueOf(this.checkedIds.contains(id)); | |
} | |
/** | |
* Checks the item with the specified id (Struts will call this when | |
* populating the formbean on submit; you can also use it when preparing | |
* the page to pre-check boxes). | |
*/ | |
public void setChecked(String id, | |
@SuppressWarnings("unused") String value) { | |
this.checkedIds.add(id); | |
} | |
public List<T> getCheckableItems() { | |
return items; | |
} | |
public void setCheckableItems(List<T> items) { | |
this.items = items; | |
} | |
@Override | |
public final void reset(ActionMapping mapping, | |
HttpServletRequest request) { | |
super.reset(mapping, request); | |
checkedIds = new HashSet<String>(); | |
} | |
/** | |
* Returns the ids of checked items. This is for you to call in the action | |
* handling the submit, not for Struts. | |
*/ | |
public final Set<String> getCheckedIds() { | |
return checkedIds; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.andrewspencer.gist.multiple-checkboxes-struts; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.apache.struts.action.Action; | |
import org.apache.struts.action.ActionForm; | |
import org.apache.struts.action.ActionForward; | |
import org.apache.struts.action.ActionMapping; | |
public class PrepareMultipleCheckboxesAction extends Action { | |
@Override | |
public ActionForward execute(ActionMapping mapping, ActionForm form, | |
HttpServletRequest request, HttpServletResponse response) { | |
FbMultipleCheckableBeans fb = (FbMultipleCheckableBeans) form; | |
fb.setCheckableItems(loadItems()); | |
for (String id : loadCheckedIds()) { | |
fb.setChecked(id, null); | |
} | |
return mapping.findForward("show"); // to strutsCheckBoxes.jsp | |
} | |
/** | |
* Loads items. The JSP in this example expects each item to have | |
* an "id" and a "label" property. | |
*/ | |
private List<SomeBeanType> loadItems() { | |
// ... | |
} | |
/** | |
* Loads ids of items that are already checked | |
*/ | |
private Collection<String> loadCheckedIds() { | |
// ... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.andrewspencer.gist.multiple-checkboxes-struts; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.apache.struts.action.Action; | |
import org.apache.struts.action.ActionForm; | |
import org.apache.struts.action.ActionForward; | |
import org.apache.struts.action.ActionMapping; | |
public class SaveMultipleCheckboxesAction extends Action { | |
@Override | |
public ActionForward execute(ActionMapping mapping, ActionForm form, | |
HttpServletRequest request, HttpServletResponse response) { | |
FbMultipleCheckableBeans fb = (FbMultipleCheckableBeans) form; | |
for (String id : fb.getCheckedIds) { | |
// do something with the ID of the checked item | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%-- This supposes that | |
1) the formbean for /handleCheckboxes is called "FbMultipleCheckboxes" and is of | |
type FbMultipleCheckableBeans | |
2) the beans in the list property "items" all have a property "label" | |
3) the appropriate CSS style classes have been defined | |
4) JSTL, struts and displaytag taglibs have been imported | |
5) message resources are in place for displaytag and JSTL message tags | |
--%> | |
<html:form action="/handleCheckboxes"> | |
<%-- Here we iterate through the "items" collection of the formbean. --%> | |
<display:table defaultsort="0" sort="external" requestURI="" | |
name="${FbMultipleCheckboxes.items}" uid="item" class="table-style"> | |
<%-- Here we use the "id" property of the "item" bean (one of the | |
elements in the formbean's "items" collection) to find out | |
whether that item is checked in the formbean's "checked" | |
property. --%> | |
<display:column media="html" class="checkbox-column"> | |
<html:checkbox property="checked(${item.id})" | |
styleClass="checkbox" /> | |
</display:column> | |
<display:column property="label" titleKey="messages.label" /> | |
</display:table> | |
<div class="buttons_left"> | |
<input type="submit" class="submit" | |
value="<fmt:message key="button.save"/>" /> | |
</div> | |
</html:form> |
Hi,
I've changed jobs and no longer have access to the source code of that project. The above should be everything you need, assuming you're starting from a project that already uses Displaytag. If you aren't, the Displaytag docs should say what you need to put in struts-config etc. Taglib imports were the standard Struts html tags and displaytag-el.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, the taglibs in the jsp and the lib you used with the needed jars?