Meaningless Drivel is fun!*
The moose likes JSF and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark ""Fake" Dynamic Field to Form [using rendered]" Watch ""Fake" Dynamic Field to Form [using rendered]" New topic
Author

"Fake" Dynamic Field to Form [using rendered]

Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Hi guys...

I want to do the following:

A select one menu with fields names.
A button to add the field to the form.

BUT! The field is already there! But it was been "rendered" false to start with.
So, my idea is: when the user clicks on the add button, the method that handles the rendered boolean for each field
will then return TRUE, and the ajax update":form" should do the trick and make the field appear.
But didn´t work.

The front end with the select one menu.



Some fields are by default VISIBLE, so no rendered needed.

the methods on managedBean:



As you can see, I have an HASHMAP to control if that field is visible or not.

Did I do something wrong on the front end?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19


#{grupoController.filterIsVisible(filterIdade)}


Is not good practice. You should not be coding parameters on the View if at all possible. Also, I'm not even sure that you can do a general method call in the context of a "rendered" attribute to begin with, Normally you should be making a property reference.

I also cannot make sense of what appears to be a possible page-scope variable "filterIdade". JSF does not support page scope at all. To function properly, "filterIdade" needs to be a property of a backing bean, not an independent entity.


Customer surveys are for companies who didn't pay proper attention to begin with.
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Tim Holloway wrote:

#{grupoController.filterIsVisible(filterIdade)}


Is not good practice. You should not be coding parameters on the View if at all possible. Also, I'm not even sure that you can do a general method call in the context of a "rendered" attribute to begin with, Normally you should be making a property reference.

I also cannot make sense of what appears to be a possible page-scope variable "filterIdade". JSF does not support page scope at all. To function properly, "filterIdade" needs to be a property of a backing bean, not an independent entity.


thanks for the answer.
Well... I got it running Tim.

You right about the bad pratices. But it works.
Maybe it should be an convencion for my project.

The correct xhtml looks like this:


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

"It works" isn't a happy phrase for me. It's often followed by "it broke". And "I haven't the faintest idea of how this ever worked". Or "We can't change that".

Here's a more JSF-friendly expression:


Note that I've changed "filterIsVisible" to a property referencing a Map<String, Boolean>, and made "grupo.idade" be a key into the map.

And, BTW. Backing beans are NOT Controllers. In JSF the Controllers are pre-supplied in the FacesServlet and in the tag internal implementations. Backing beans are Models. Listeners and Action methods are not Controller methods. Action methods, in fact, are business logic.
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Tim Holloway wrote:



Wow! That was very very good!
But I need a method on the backing bean because I use it on the java side as well.

Oh, I thought ManagedBean would be like an controller...
Basiclly i dont have a controller. Only Model and View on JSF
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

The backing bean method is:


So the Java usage would be:


However, "magic" values are not considered ideal even in pure Java code, much less on View Definitions, so it might be better to do this:



You would then be a able to invoke that method directly in Java code and you would also be able to simplify your View Template:


It's always desirable to keep the EL as simple as possible. Even if you don't subscribe to the the "No Logic In View Rule", debugging EL is a

In JSF, you do have Controllers. The difference is, you don't code them yourself (usually!). Most of the Controllers you need are already supplied.
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
I understand.
But if I have a view with 50 fields ?

Wouldn´t be too 'manual' writing one method per field?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

Marco Noronha wrote:I understand.
But if I have a view with 50 fields ?

Wouldn´t be too 'manual' writing one method per field?


Probably. Then again, I think I probably would have used a dataTable for the display with a List or array model, and that would have allowed me to simply use indexing instead of complicated Map expressions.
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Tim Holloway wrote:
Marco Noronha wrote:I understand.
But if I have a view with 50 fields ?

Wouldn´t be too 'manual' writing one method per field?


Probably. Then again, I think I probably would have used a dataTable for the display with a List or array model, and that would have allowed me to simply use indexing instead of complicated Map expressions.


can you please show me an example of that working?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

I recommend looking at a good JSF book.
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Tim Holloway wrote:I recommend looking at a good JSF book.


I read the Core JavaServer Faces Ed. 3.

Any other recomendations?


IT would be really nice of you, if you could
just show me am example
but I don´t want to make you lose your time..

So thanks anyway
 
 
subject: "Fake" Dynamic Field to Form [using rendered]
 
Similar Threads
synchronization validation and display of dialog in a primeface JSF2.0 form
Updating a form in primefaces when a selection is made in the datatable
Issue with jsf value change listener
multiple form in xhtml page nad update different controlls on two different forms
JSF not setting my attribute