Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSF and the fly likes Navigating page links with parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Navigating page links with parameters " Watch "Navigating page links with parameters " New topic
Author

Navigating page links with parameters

james loveay
Greenhorn

Joined: Apr 24, 2012
Posts: 10

Hi all,

I'm really hoping that somebody can help me with this one, I'm also hoping that this is not a re-post if so I'm sorry in advance.
The problem that I have is that I have developed a CRUD application with the netbeans ide using the JSF 2.0 framework what I have is a number of tables two called company and department, when the user clicks the link to view the list of companies I have set it up so that there is a link in the table for the user to view the departments linked to that company.

Can anybody advise me the best way to go about this because I want to be able to pass the company parameter which is the companyid to the system to pull out the view for the list of departments linked to that company via the foreign key companyid in the department table.

I am new to JSF so I need to have somebody who has the patience to explain this or even provide a link that will show what I need to go, I have an idea that it involves making use of an actionevent but after much Googling I can't see the wood for the trees.

Many thanks for any help with this.
Fabio Medeiros Faria
Greenhorn

Joined: Mar 18, 2011
Posts: 10

Hi James,

I think there are another way for you to do this. Generally when we use JSF is possible set the value directly in a property of a managed bean.

Search about the tag <f:setPropertyActionListener target="<<MB property>>" value="<<value>>">, this is efficiently.

But, in last case, if you really need put some parameters in an URL, could you use an <hutputLink> and concatenate the parameters after correct url.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

In JSF you don't normally pass that information externally on the URL. Instead, everything is done in the backing beans.

For the common case where you have a table and associated detail edit/view, one of the best references I can give you is Rick Hightower's "JSF for Nonbelievers" series that he published in 4 fairly small but useful pages on the IBM DeveloperWorks website several years ago. JSF has gone through some improvements since then, but the essential processes remain the same.


Customer surveys are for companies who didn't pay proper attention to begin with.
james loveay
Greenhorn

Joined: Apr 24, 2012
Posts: 10

Thank you sooooooo much I'll have a go with the first suggestion, if it doesn't work at least I'll have learned something else. I'll also have a look at the tutorials suggested.

Many thanks, hopefully I'll be able to get back to you soon and let you know how it goes.

James
Fabio Medeiros Faria
Greenhorn

Joined: Mar 18, 2011
Posts: 10

to be more specific, see an example:



and in your page (I'm using jsf with primefaces, but is the same for other UI components or only JSF):


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

Properly speaking, "getListPersons() should be returning a DataModel object, not a collection object. It was mandatory in JSF1. In JSF2, I think that JSF actually constructs a DataModel dynamically and stuff your collection into it, but since the DataModel isn't visible to application code, you end up having to add a Listener to your action when you could have coded a simple action method and pulled the selected row from the DataModel without all that extra view-side coding.
Fabio Medeiros Faria
Greenhorn

Joined: Mar 18, 2011
Posts: 10

I I observed is need, only in cases that table is handled with components, such as: check box, row select.

For simple exhibition it isnt necessary. Of course, if the collection for changed for an DataModel, it's work perfectly too.

[]s
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

JSF has 2 model classtypes.

The SelectItem contains a name/value pair used to generate an HTML OPTION tag.

The DataModel (and its subclasses) wraps an array, collection, or other traversable data source in order to augment it with contextual information that would otherwise require modifying the structure of the underlying wrapped data.

I have tried and failed to find anywhere in the JSF2 docs that says it's actually legal to return a bare collection for the value property, but current popular JSF implementations can do so, whether that's an explicitly-designed feature or just a temporary artefact. Regardless, if you do not construct a DataModel, you will not have access to that contextual information, and specifically, you won't know which row of a table was selected without doing awkward things in the View Definition. Which possibly violates the MVC rules, although I haven't analyzed in-depth to be certain.
Fabio Medeiros Faria
Greenhorn

Joined: Mar 18, 2011
Posts: 10

By curiosity and knowledge, I did a quick search about DataIterator of JSF and some UI components.

In the JEE6 tutorial of Oracle, I found:

The value attribute of an h:dataTable tag references the data to be included in the table. This data can take the form of any of the following:

A list of beans

An array of beans

A single bean

A javax.faces.model.DataModel object

A java.sql.ResultSet object

A javax.servlet.jsp.jstl.sql.Result object

A javax.sql.RowSet object

Extracted from: JEE 6 tutorial

As I said earlier a collection of POJOs to be only displayed will work perfectly, but if I need more interaction, such as: checkbox, rowSeleciton, and others; in these cases will be necessary build a DataModel.

At show case of primeFaces in the DataTable component is used only a simple collection, so I this mode works too: DataTable - PrimeFaces
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

Thanks. I'd prefer the formal specs, but I can't find the formal specs for the HtmlDataTable class. The JavaDoc version is pretty lame.

As I said, I think that if you don't provide a DataModel object for the table's value attribute, I believe it builds one anyway, although that may vary depending on implementation. However, it's comforting to know what the (updated) options are.

The primary advantage to using a DataModel for your example is that it eliminates the need for a separate listener. The reasons I'm not comfortable with using a listener there are that A) it complicates the View definition over simply using an undecorated action method, and B) the action method is no longer directly coupled to the selected row that it (allegedly) acts upon.

I would use a direct object for display-only purposes (based on the tutorial information), but if I needed to act on a selected row in a table view, that's where I'd use a TableModel.
james loveay
Greenhorn

Joined: Apr 24, 2012
Posts: 10

ok I can see sort of where this is going but I still need to get the parameter, so in my list of companies table there is a column which allows the user to view, edit, delete or to navigate to another page called List_1 that will dynamically create the table for the departments linked to the company chosen by the user. So I some how have to get the companyid, pass that to the bean which will then be used as a parameter to feed into the SQL query.
I have the query but have no idea how to grab the companyid which is an into when the user selects the department link, I can navigate to the required page but then that page has to return the table from the bean.

I have something like this in the page; <h:commandLink action="/department/Link_1" value="department"/>
<f: param= (what goes here, help) />

In the bean I have a query set up, to create the dynamic query and return the list<Departments> but I need to pass the id across to the bean, this where I am finding the fault.

If anybody can take the time to explain this I'd be really grateful, again many thanks. I'm sorry for going the wrong way around but I really wanted to ask for as little help as possible and try to work out what I needed to do, but I keep running into a brick wall.

James
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

That's what the DataModel is for. Your action method can invoke the datamodel object's "getRowData()" method to obtain the entire select table row - which presumably will include the object's persistent key, even if you didn't happen to display it in the View.

Rather than confuse you further with my feeble attempts at explanations, I'd like to direct you to a series of articles at IBM Developerworks by Rick Hightower. It's called "JSF for Nonbelievers" and it does exactly and graphically what you're looking to do (and what I do on a nearly daily basis). The actual article is pretty old now, but there's really no change between then and now for that kind of stuff.

The 2 things to remember about JSF are that 1) you rarely have to code "JSF", because it's designed to get most of its functionality from POJO bean properties and methods (the Model wrappers are the exception). 2) Forget about the "go out and do things" a/k/a "pro(ugh)"-active approach that you're used to. JSF is an inverted paradigm where the goodies get delivered to you.
james loveay
Greenhorn

Joined: Apr 24, 2012
Posts: 10

Hi Tim,

I understand what you mean, I have gone through the tutorials and have found them to be very well written and they have helped a lot. The trouble that I now have is that using the netbeans IDE, I went through the project using the wizard which has not really helped because it created facades for each of my tables as well as controllers session beans and entity beans. Which if I'm honest has done nothing to endear me to JSF as it has just caused massive confusion as to where I now need to place my method is it in the controller bean or the facade bean, adding to that what scope do I need to use.

Also what IDE would I be better off using because it is quite clear that using netbeans is not the best for the job after all.

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

Oh, what a lovely straight line you've fed me!

Ask some of our august members that question and their response to the question of the ideal IDE for learning will be "Windows Notepad". Or, for the really snarky ones, EDLIN, or vi. :P

IDEs are invaluable productivity aids, but not so good as learning tools. They do stuff for you so you don't have to bother learning it yourself. Problem is, they can't do everything so it's kind of like being left with a kindly old relative who turns into a roaring fiend once the parents have gone out.

A lot of people use NetBeans for JSF development very successfully. I'm more partial to Eclipse, for historical reasons, but I used NetBeans long before JSF was invented and the product was known as Forte and I've used Intellij. For a while, I used Emacs, which is about as much Java IDE as you can get on a 200MHx Pentium with 98MB RAM. That machine, incidentally is still a critical part of my server farm and probably will be until it dies. Not much oomph, but it only draws 60 watts.

The main thing you need to do is learn JSF without the IDE. From the sound of it, you're trying to employ parts of your ORM Entity Model as JSF Managed Beans and that's not a good fit because of the mostly static nature of JSF beans versus the more dynamic nature of the ORM model objects.

You can set up a JSF managed bean to be an aggregator, decorator, or fa├žade for an Entity model without too much trouble. In the case of a table/detail view like you've been describing, the table's datamodel can be a collection of Entity objects. That, in fact, is one of the main reasons for the existence of the DataModel class - to allow the "piggybacking" of JSF functionality onto POJO objects in a transparent way.

I think what would work best for you is if you'd download the sample code for the JSF For Nonbelievers DeveloperWorks article, unzip it, and import it into a NetBeans project. You'll probably have to make some adjustments to the library setup, since the article is based on JSF 1.1 or thereabouts, but the actual source code is still 100% applicable. Once you see how that works, try slipping in your own datatable, then add in the interface code that invokes your persistency logic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Navigating page links with parameters