Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I prefer List to arrays... Get this very short example working and it will show you how to dynamically select check boxes. </p> <p>The demo lists a number of check boxes labeled by numbers, a box selected from the first list will appear in a second list, boxes selected in the second list will become part of a unordered list.</p> <p>JSP "checkboxes.jsp"</p> <pre><code>&lt;%@taglib prefix="s" uri="/struts-tags"%&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;All Check Boxes&lt;/h1&gt; &lt;s:form action="checkboxes"&gt; &lt;s:checkboxlist name="selectedBoxes" list="allBoxes"/&gt; &lt;s:submit/&gt; &lt;h1&gt;Selected Check Boxes&lt;/h1&gt; &lt;s:checkboxlist name="selectedSelectedBoxes" list="selectedBoxes"/&gt; &lt;s:submit/&gt; &lt;/s:form&gt; &lt;h1&gt;Values chosen from selected Check boxes&lt;/h1&gt; &lt;ul&gt; &lt;s:iterator value="selectedSelectedBoxes"&gt; &lt;li&gt;&lt;s:property/&gt;&lt;/li&gt; &lt;/s:iterator&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Action Class "Checkboxes.java"</p> <pre><code>package struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Checkboxes extends ActionSupport{ public List&lt;Integer&gt; allBoxes = Arrays.asList(1,2,3,4,5,6,7,22,33,44); //ten elements public List&lt;Integer&gt; selectedBoxes = new ArrayList(); public List&lt;Integer&gt; selectedSelectedBoxes = new ArrayList(); } </code></pre> <p>If using conventions this is straight forward if using xml action "checkboxes" maps to class "struts2.Checkboxes"</p> <p>You can see from the example that there can be a disagreement between the displayed state of the selected checkboxes and what is in the unordered list. This would be really easy to correct within the action method but would make the example longer and detract from illustrating the functionality of the check boxes. Further some feel that encapsulating the fields with getters and setters is a good idea but again it would greatly expand the code length.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload