File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Accessing a UI component in a managed bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Accessing a UI component in a managed bean" Watch "Accessing a UI component in a managed bean" New topic

Accessing a UI component in a managed bean

Dishpal Bhaluja
Ranch Hand

Joined: Dec 30, 2007
Posts: 96
I am using a data table in jsf and I want to access the rows selected by the user.However I want my logic to that all in my managed bean rather than in th pagecode(backing beans).How can I do it?.Aim is to separate the messy page code and my logic.How can I iterate through the rows selected by user.Help needed.

In anticipation.
Venkat Sadasivam
Ranch Hand

Joined: May 10, 2008
Posts: 139
You can easily achieve this using ExtendedDataModel in Richfaces component.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”<br>
-Martin Fowler
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
You can use the getRowData() method on your data table to get the selected row.

SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Dishpal Bhaluja
Ranch Hand

Joined: Dec 30, 2007
Posts: 96
I know I can get rowdata using that function but how can I get that UIComponent,Datatable.

Supposing I am clicking on a check box to select a particular row.Then if Im calling an action listner its registered as an event generated by checkbox and then I cant get the reference to Datatable.I need to get that reference and use the function ,rowdata.

As I suppose my managed bean to be free from codes that access these components.Can you please suggest a better way to organise the entire thing for clarity.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17423

Offhand, I'd say you're trying to do things inside out and it's making things excessively complex.

JSF is based on Inversion of Control. In IoC, you normally don't have your beans go out and get things or do things. Instead, they wait for things to be injected into them and for requests to be made of them.

Another way of stating it is that the controlling logic is provided by the framework - the bean only provides the action logic and data storage.
You should separate business logic from the backing bean. It's good practice. But the way this is normally done is to implement the logic bean as a separate POJO which is injected into the backing bean either by JSF or an equivalent framework such as Spring. The backing bean's action processors then invoke the business bean's methods as needed,

I have no idea what "selected rows" means. JSF is not a client/server architecture nor is any other web application regardless of platform or programming language. A JSF backing bean can only receive events in a batch as an HTTP GET or POST request dispatches them - there's no back-and-forth like in client/server (and this is why I always stress that JSF is not a true MVC architecture). More to the point, I don't know what you mean by saying a row is "selected". There is a "current row" as a bean receives updates from the client, but this is enumerated by the framework, but that's an enumeration of all rows. There's no selection involved, just fields that updated or not.

An IDE is no substitute for an Intelligent Developer.
Kaue Grecchi

Joined: Mar 07, 2012
Posts: 2
Sorry ressurecting the topic but I have the same problem. I have an IceFaces <ice:dataTable> that iterates from a List (here called "dataList") and generates a table.
dataList has the attributes: id, name, category, scenario and scope.

The table must show 2 columns:
- The "name" of the current data from the list;
- A commandButton that redirects to a page to see it's details (the other attributes);

The question is: how set the "id" attribute in the Managed Bean with the data's "id" from the row wich button was clicked.

Like this:

ROW1 | Button1
ROW2 | Button2
ROW3 | Button3
ROW4 | Button4
ROW5 | Button5

Button3 is clicked, the id "3" is passed to the managed bean, the form redirects to the detailed view of the data and retrieves the "id" of the row 3... Hope I made myself understood.

Tim, as you seem to be the guru around here... where can I get detailed information on the use of JSF managed beans, using FacesContext and PhaseListener kind of stuff? I only find simple examples with them but without information on -what- is really getting done.

Thanks a lot!
I agree. Here's the link:
subject: Accessing a UI component in a managed bean
jQuery in Action, 3rd edition