Showing posts with label struts2. Show all posts
Showing posts with label struts2. Show all posts

Sunday, April 19, 2009

Alternating row colors using Struts2
<s:iterator value="results" status="status">
<s:if test="#status.odd">
<tr bgcolor="#F8ECE0">
</s:if>
<s:else>
<tr>
</s:else>

Saturday, February 21, 2009

Struts2 nested checkbox tag inside iterator

Using the s:iterator tag and status attribute it's possible to display and update values of complex types within a collection.

For example, using an iterator tag,

<s:iterator value="results" status="status">

<s:hidden value="%{product.sku}" name="results[%{#status.index}].product.sku" />

<input type="checkbox" name="results[<s:property value="%{#status.index}" />].selected" <s:property value="%{selected}" />>

</s:iterator>

within a form tag, the submit action will update the list of results in the Action class, where my action class has

protected List<ProductBean> results;

with getters and setters, and my ProductBean has,

private Product product;

with getters and setters, and

private boolean selected;

/**
* @return the selected
*/
public String isSelected() {
    return selected ? "checked" : "";
}

/**
* @param selected the selected to set
*/
public void setSelected(String selected) {
    this.selected = selected == null || selected.equals("") ? false : true;
}

Note: If you find something useful, please leave a comment, thanks!

Sunday, June 08, 2008

Struts2 checkboxlist UI tag rendering one item per line
After struggling with this UI tag for a some time today, I gave up and was attempting to use s:checkbox inside of an s:iterator to render one item per line, unsuccessfully.

So, upon revisiting s:checkboxlist, I found that it's a relatively painless thing to override the checkboxlist.ftl file described here, http://struts.apache.org/2.0.9/docs/template-loading.html, add a break at the end of this line

<label for="${parameters.name?html}-${itemCount}" class="checkboxLabel">${itemValue?html}</label><br />
and save the file in the webapp/template/simple dir of my web application.