File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes JSF and checkboxes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF and checkboxes" Watch "JSF and checkboxes" New topic
Author

JSF and checkboxes

Charu K.
Greenhorn

Joined: Apr 05, 2005
Posts: 3
Hi,
I'm new to JSF and am trying to use JSF checkbox componenets in my code within a Datatable. Heres the jsp code:

<h ataTable styleClass="dataTable" id="id2" value="#{code.users}"
var="dbRow" border="1" rowClasses="ROW1,ROW2">
<h:column>
<f:facet name="header">
<f:verbatim>Select</f:verbatim>
</f:facet>
<h:selectBooleanCheckbox styleClass="selectBooleanCheckbox"
id="chkUser" value="#{dbRow.BKUSERID}" />
</h:selectBooleanCheckbox>
</h:column>

<h:column>
<f:facet name="header">
<f:verbatim>User ID</f:verbatim>
</f:facet>
<h utputText value="#{dbRow.BKUSERID}" />
</h:column> .................
</h ataTable>

<hx:commandExButton type="submit" value="Remove" action="remove"
styleClass="commandExButton" id="id6"></hx:commandExButton>


Now when I click on this Remove button the page should be refreshed with the selected rows deleted.
My 'Code' bean has the following code:


public Result getUsers() {
HtmlSelectBooleanCheckbox chkUser = (HtmlSelectBooleanCheckbox) Datatable_backupUsers.findComponentInRoot("chkUser");

........
String backUpUserID = (String) chkUser.getValue(); .....

which always returns null for some reason. Can somebody tell me what I'm doing wrong here?

Thanks.
Charu
Charu K.
Greenhorn

Joined: Apr 05, 2005
Posts: 3
Hi Varun,

I saw someplace that you suggest using hidden components as an alternative for storing values corrosponding to the checked boxes. But since multiple check boxes can be selected at runtime I dont see how I can use hidden componenets.Can you suggest something else?

Thanks.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
I don't think you need any hidden component since you are having a check box per record. You data table must be mapped with a collection object. Each object in this collection must be representing a row of your table. Hence each object would have a boolean value depending upon whether the row is checked or not from UI.
Now once user clicks remove button, you can remove all the objects with the boolean property marked as checked.

Also, I notices you hx:commandExButton button needs a proper action. You can search this forum to get the good links. It should be mapped to a backing bean's method.


- Varun
Charu K.
Greenhorn

Joined: Apr 05, 2005
Posts: 3
Hi Varun,
Thanks for replying.
I've used the JSTL ResultSupport class to turn the Resulset to Result and have mapped this to my Datatable which displays the data from the db.
Each column is mapped onto a resulset column.What I dont understand is how to pass this value back to the bean. I dont understand how to use a boolean value for doing this.I've looked around but didnt find a suitable example.My remove button is mapped onto the backing beans method in that it simply refeshes the page by calling the getTable() method again to repopulate the Datatable.Heres my code for a better understanding.

The jsp code with the datatable:
<hx:scriptCollector id="id1">

<h:form styleClass="form" id="id3">

<%-- jsf:codeBehind language="java" location="/JavaSource/pagecode/Datatable_backupUsers.java" --%>
<%-- /jsf:codeBehind --%>

<%request.setAttribute("UserID","sd7782");%>

<h ataTable styleClass="dataTable" id="id2" value="#{code.users}"
var="dbRow" border="1" rowClasses="ROW1,ROW2" cellpadding="10"
columnClasses="firstColumn,secondColumn,thirdColumn">

<h:column>
<f:facet name="header">
<f:verbatim>Select</f:verbatim>
</f:facet>
<h:selectBooleanCheckbox styleClass="selectBooleanCheckbox"
id="chkUser" value="#{dbRow.BKUSERID}" /> //this is the value I want
</h:selectBooleanCheckbox>

</h:column>

<h:column>
<f:facet name="header">
<f:verbatim>User ID</f:verbatim>
</f:facet>
<h utputText value="#{dbRow.BKUSERID}" />
</h:column>

<h:column>
<f:facet name="header">
<f:verbatim>Full Name</f:verbatim>
</f:facet>
<h utputText value="#{dbRow.BKUSERNAME}" />
</h:column>

</h ataTable>

<hx:commandExButton type="submit" value="Add" action="add"
styleClass="commandExButton" id="id5"></hx:commandExButton>
<hx:commandExButton type="submit" value="Remove" action="remove"
styleClass="commandExButton" id="id6"></hx:commandExButton>
<hx:commandExButton type="submit" value="Home"
styleClass="commandExButton" id="id7"></hx:commandExButton>

</h:form>
</hx:scriptCollector>

And the relevant code in the backing bean is:

public Result getUsers() {
Datatable_backupUsers db = new Datatable_backupUsers();
//the jsf:codebehind class

Map request = db.getRequestScope();
String userID = (String) request.get("UserID");
HtmlSelectBooleanCheckbox chkUser = (HtmlSelectBooleanCheckbox)
Datatable_backupUsers.findComponentInRoot("chkUser");

try {
ResultSet resultSet = null;
String query = null;
Connection connection = getConnection();
Statement statement = connection.createStatement();
if (chkUser.isSelected() ) { //Doesnt work
System.out.println(" is selected now...");
// chkUser.getValue() always returns null even when chkbox selected
String backUpUserID = (String) chkUser.getValue();
System.out.println("back up user id: " + backUpUserID );
query = " UPDATE BACKUPUSERS SET USERID='abc'" ;
query = query + " WHERE BKUSERID='" + backUpUserID + "'";
resultSet = statement.executeQuery(query);
}
query = "SELECT * FROM BACKUPUSERS busers, USERS users ";
query= query + "WHERE busers.USERID = users.USERID AND
users.USERID='" + userID + "'";resultSet = statement.executeQuery(query);
return(ResultSupport.toResult(resultSet));
} catch(Exception e) {
System.out.println("in exception " + e);
return(null);
}
}

I dont see what I'm doing wrong especially since I've read code from other components like this in other methods of the bean eg.
HtmlOutputText txtUserID = (HtmlOutputText)
Datatable_backupUsers.findComponentInRoot("txtUID");
String userID= (String) txtUserID.getValue();

Would appreciate any help.
Thanks again.
Brendan Richards
Greenhorn

Joined: Jun 01, 2005
Posts: 3
Here's an alternative / simple way to do multiple checkboxes within a data table. With this method you dont need to bind your checkboxes to anything server side except the ids of the object you want to select.

PHP coders will immediately recognise this...

1) put a plain old tag in your table column and binding the id of the record you want to select.

2) In your action:
- get a HttpServletRequest Object:

- request.getParameterValues("toDelete") will return a String[] of selected ids
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

"Charu K.", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF and checkboxes