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 commandButton action passing List doesn't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "commandButton action passing List doesn Watch "commandButton action passing List doesn New topic
Author

commandButton action passing List doesn't work

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Hi guys,

Quick question...

Anyone know why this would return the myObject as expected complete with all fields (except myObject.aList), but the testList would be returned as empty (not null)



Doesn't tell you a lot but here's the Bean.



Both testList and myObject.aList are empty in testMethod2() but the rest of MyObject is fine

Cheers in advance

KS
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

The more your View Definition looks like a program, the less advantage it's getting from the JSF framework. Actions should not be passing parameters. Action methods (as originally defined) expected to do all their processing on JSF beans and/or other resources attached as properties to those beans.


Customer surveys are for companies who didn't pay proper attention to begin with.
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Hmmm, OK. Just wondering though, why I can pass an Object like String, Integer or custom Object back, but not a collection.

But in regards to how JSF 'should' work. What would be the best way of handling the senario.

- testMethod1() sets a List of values
- List is displayed in my xhtml page
- I edit one value from the list and click 'save'
- testMethod2() does stuff.

By the time I get to testMethod2() List is null again. So I'll end up re-init it from something like @PostConstrut and have to do a load of
logic to find the updated entry etc... Would be a lot easier to just pass the updated List back to the Bean like you can with other objects.

Doesn't seem to be doing much more than a bulk standard Servlet httpRequest to me.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

Kevin P Smith wrote:
Doesn't seem to be doing much more than a bulk standard Servlet httpRequest to me.


JSF is not a "bulk standard Servlet httpRequest handler". For that, just use bulk standard Servlets and save the effort.

JSF is a Model/View/Controller framework built using JavaBeans as Models, HTML Forms as the Views, and supplying the Controllers as part of the framework. There's a significantly different paradigm at work. Trying to "call" things from a form is the equivalent of trying to write Java Object-Oriented Code using COBOL Procedure-Oriented techniques.

You didn't provide any context for your View Definition element, but since you mentioned collections, that's a good example.

Too many people want to treat Collections as something you linearly cycle through, one element at a time. But that's not always optimal. We have the ability to do massive amounts of processing in parallel with no iterating required at all. Looking at a tabular display on a web page - or a printout - there's no actual way to tell if the results came from a loop or a parallel operation. So why straitjacket the process by coding the View as an iterative construct when JSF provides an abstract table tag that can be optimized in whatever way works best for the technology available?

The dataTable tag doesn't merely define a VIew as the 2-dimensional gestalt that it actually is, however. It pairs with a Model-wrapping object that decorates the raw model with the extra properties needed to track what element of the Model the user is working with. In short, you don't have to pass a parameter to indicate which row the button you clicked on was, because the dataTable/TableModel can deduce that automatically without burdening the View with executable logic.

JSF is not the Cure for all Ailments. It works very well for a certain class of problem, and it is kind enough to keep out of the way when you need an approach where JSF is not appropriate (such as generating PDFs). However, it carries its own mindset. You can't just try and force it to work "the way you've always done it" in servlets and JSPs. That's only going to lead to code that's frustrating to create and maintain, excessively complex, and fragile. Master the basic principles of JSF and it will not only do wonderful things for you, it will do many of them "auto-magically" and mostly do it using simplified POJO code without the need to invoke arcane JSF functions and data elements.
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
So I guess in the above senario a @ViewScoped would be suited?


This is the example I built this on (but I use Spring/Hib instead of EJB/JPA)

http://code.google.com/p/javaee6-crud-example/source/browse

I goto index.jsf

In doing so, my app passed through a @PostConstruct method in Index class and 'gets' my List and displays it to my view (index.xhtml)

Here I can edit/delete/add rows (user_edit.xhtml). When happy with my changes I click 'save' call the save method which
sends updated List to DB then returns to a confirm screen (which I guess would then re-get the new List from the DB)

And a similar method could be used to display a confirmation view, as with calling the user_edit.xhtml view, I guess.

Will take some getting used to but think I'll get it. I'll expand my testApp and see how it goes.

Cheers
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

They don't pay me enough here to download and analyze webapp projects. Mostly because they don't pay me at all.

However, google "JSF for Nonbelievers". It's a 3-part (4-part?) article on IBM Developerworks by Rick Hightower. Despite the fact that it's now pretty old, I consider it the quickest introduction to best practices for the common CRUD with a table overview.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: commandButton action passing List doesn't work