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.
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.
An IDE is no substitute for an Intelligent Developer.
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.
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.
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
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.
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.
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.
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.
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.