Pages

Saturday, November 22, 2008

The CHECKBOXLIST component - struts 2

This a vary usefull UI component comes with struts 2 which allows user
for multiple selection. If we use normal HTML checkbox component, we have to write a custom function to get the selected values from a list of check boxes. Struts 2 provides a convenience way of fetching selected from a list of check boxes. Struts 2 ValueStack will resolve an array of string for the selected check boxes.


I will further demonstrate this with a simple example. Suppose I want to create a check box list which allows user to select several users from the list. This is my User class.

public class User{
private int userId,
privte Stirng userName;

// -------
}

Then I should create the HTML markup for the component.

<s:form action="deleteUser" method="POST" >
<s:checkboxlist list="users" name="users" listKey="userId"
listValue="userName">
</s:checkboxlist>
<s:submit value="update"></s:submit>
</s:form>

Here, the value of list attribute "users" should be prepopulated with an action class, and then that action class should result into this page. Don't be confuse, I have use same for the value of the name attribute. You can use any meaningful one for this. Then you have to write the action class to fetch the checked list as fallows.

package com.axiohelix.pda.actions;

import com.opensymphony.xwork2.ActionSupport;

public class DeleteUser extends ActionSupport{
private String[] users;

public String[] getUsers() {
return users;
}
public void setUsers(String[] users) {
this.users = users;
}
public String execute(){
String[] userId = this.getUsers();
// --------------------------
return SUCCESS;
}
}

1 comments:

  1. Hi Semika...

    The blog you are maintaining is very good..very good examples....can you post any examples with pagination of struts 2 without using display tag ???? or combination of dojo with struts2 etc....will follow your blog regularly ....thanks in advance...

    ReplyDelete

Share

Widgets