SelectOneMenu does not display list items - jsf

EDIT
Here is a generic example that I recreated
I am using JSF2.3 and primefaces 6.2.2
I have 3 SelectOneMenu Components (Country, State, City) that I am trying to populate from a MySql database. The problem is that when I persist for example, country (USA) and in the same session, when I try to add a state, the selectOneMenu list (Country list) does not show the just persisted country value in the drop down list.
The backing bean is #ViewScoped which I think is the issue. But, I feel that this is the right scope. I also noticed that the #PostConstruct init() method is not called just before the dialog is displayed which is the right behavior for a bean with #ViewScoped. It gets called at the beginning of the session.
Not sure if there is an event that I need to look at. Thanks for your help!

Related

Incorrect view update after validation error

View looks like this:
form (can have only one checkbox selected)
----------------------------------------
checkbox_1 (on value change clears checkbox_1 and checkbox_2 values in the backing bean and updates form**. new checkbox_1value is set after all checkbox values are cleared)row_checkbox_1(rendered only ifcheckbox_1` is checked)
checkbox_2 (on value change clears checkbox_1 and checkbox_2 values in the backing bean and updates form**. new checkbox_2value is set after all checkbox values are cleared)row_checkbox_2(rendered only ifcheckbox_2` is checked)
saveButton (on click updates formand a separate messages component)
---------------------------------
Lets state checkbox_1 is selected meaning that row_checkbox_1 components is also visible. Also there are errors in row_checkbox_1 fields.
After saveButtonis pressed the errors are displayed.
Problem: pressing/checking checkbox_2 updates backing bean - checkbox_1 value field in the backing bean gets set to false (debugged and verified by looking at getter) and checkbox_2 value field is set to true. However view does not update correctly since checkbox_1 is rendered as checked while row_checkbox_1 is not rendered at all. Remember row_checkbox_1 is rendered only if checkbox_1 is true. checkbox_2 is rendered and checked as it should be and row_checkbox_2 fields are visible as it should be.
I'm doing all this in PrimeFaces 6.0
ajax attribute restValues="true" helped. Maybe someone could do an explanation why this situation occurs? Would gladly give answer point to them!

Passing parameters: JSF page (form) -> ViewScoped backing bean -> JSP page (confirmation) [duplicate]

I have a browse.xhtml where I browse a list of cars and I want to view the details of the car in details.xhtml when a "View more" button is pressed. Their backing beans are #ViewScoped and are called BrowseBean and DetailsBean, respectively.
Now, I wouldn't like the user/client to see the car ID in the URL, so I would like to avoid using GET params, as presented here and here.
Is there any way to achieve this? I'm using Mojarra 2.2.8 with PrimeFaces 5 and OmniFaces 1.8.1.
Depends on whether you're sending a redirect or merely navigating.
If you're sending a redirect, then put it in the flash scope:
Faces.setFlashAttribute("car", car);
This is available in the #PostConstruct of the next bean as:
Car car = Faces.getFlashAttribute("car");
Or, if you're merely navigating, then put it in the request scope:
Faces.setRequestAttribute("car", car);
This is available in the #PostConstruct of the next bean as:
Car car = Faces.getRequestAttribute("car");
See also:
Injecting one view scoped bean in another view scoped bean causes it to be recreated
How to pass objects from one page to another page in JSF without writing a converter
Note that I assume that you're very well aware about the design choice of having two entirely separate views which cannot exist (be idempotent) without the other view, instead of having e.g. a single view with conditionally rendered content. And that you already know how exactly the view should behave when it's actually being requested idempotently (i.e. via a bookmark, shared link, by a searchbot, etc). If not, then I strongly recommend to carefully read the answer on this question: How to navigate in JSF? How to make URL reflect current page (and not previous one).
Update: in case you're not using OmniFaces, use respectively the following:
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("car", car);
Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("car");
FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("car", car);
Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("car");

Pass an object between #ViewScoped beans without using GET params

I have a browse.xhtml where I browse a list of cars and I want to view the details of the car in details.xhtml when a "View more" button is pressed. Their backing beans are #ViewScoped and are called BrowseBean and DetailsBean, respectively.
Now, I wouldn't like the user/client to see the car ID in the URL, so I would like to avoid using GET params, as presented here and here.
Is there any way to achieve this? I'm using Mojarra 2.2.8 with PrimeFaces 5 and OmniFaces 1.8.1.
Depends on whether you're sending a redirect or merely navigating.
If you're sending a redirect, then put it in the flash scope:
Faces.setFlashAttribute("car", car);
This is available in the #PostConstruct of the next bean as:
Car car = Faces.getFlashAttribute("car");
Or, if you're merely navigating, then put it in the request scope:
Faces.setRequestAttribute("car", car);
This is available in the #PostConstruct of the next bean as:
Car car = Faces.getRequestAttribute("car");
See also:
Injecting one view scoped bean in another view scoped bean causes it to be recreated
How to pass objects from one page to another page in JSF without writing a converter
Note that I assume that you're very well aware about the design choice of having two entirely separate views which cannot exist (be idempotent) without the other view, instead of having e.g. a single view with conditionally rendered content. And that you already know how exactly the view should behave when it's actually being requested idempotently (i.e. via a bookmark, shared link, by a searchbot, etc). If not, then I strongly recommend to carefully read the answer on this question: How to navigate in JSF? How to make URL reflect current page (and not previous one).
Update: in case you're not using OmniFaces, use respectively the following:
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("car", car);
Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("car");
FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("car", car);
Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("car");

Refreshing a component bind to request-scoped bean

Does anyone have a solution for such a problem:
In my app I'm using a complex, programmatically build dashboard based on the primefaces dashboard. To overcome problems with nonunique id's of the panels building the dashboard, I'm binding this component to a request-scoped bean. I'd also like to rebuild the dashboard based on some changable parameters after clicking a commandButton.
The problem is, that the getter for the dashboard is fired in the Apply Request Values phase, way before the actionListener of the commandButton is fired (in the Invoke Application phase). So, although the dashboard is rebuild eventually, it's not beeing refreshed in the rendered response.
On the other hand, if I try to set immediate attribute of the button to true, the actionListener is fired in the Apply Request Values phase, but still after the getter. Than the lifecycle goes directly to the Render Response phase, and the outcome is the same.
Anyone?
Thank you for the answer. Let me add a bit detail to my problem.
I store a model of a sports tournament as a property of a session scoped bean. It goes like this: the bean has a property "tournament". This class has a list of groups, each with it's table of matches. The idea was to use three different programmatically built components as renderers of this tournament model.
The dashboard would be used for drag-and-drop edition of contestant placement in groups. For viewing match tables and editing their matches I use a tab panel, with panel grid for every table. Finally, I use a panel grid to show a tournament tree. Every of those three components render some part of the model for the user to edit.
Since the model (and therefore those rendering components) are dynamically build depanding on chosable parameters like number of groups for example, i had a problem with id uniqnes when binding them to a session-scoped bean. So I bound them to a request scoped bean. With every request changing the model (mostly ajax) I wanted to rerender those components depending on the parameters set by the user (also stored in the session scoped bean).
The problem is, that when I rebuild the model in the invoke application phase (in a action listener fired by the "rebuild-my-model" button), the components bound to a request-scoped bean have already been "get-ed" from the bean (or so it seems), and they do not refresh on the page.
I would be very gratefull for a clue to what i'm doing wrong, and perhaps a suggestion, if the approach mentioned above is completelly stupid :)
The problem is, that the getter for the dashboard is fired in the Apply Request Values phase, way before the actionListener of the commandButton is fired
I'm not sure why exactly that forms a problem for you. Perhaps you're incorrectly doing business logic in the getter method instead of in the action listener method? Or perhaps you're manually creating the component instead of referencing the JSF-created one and thus always overridding the one in the JSF view?
A proper JSF getter method basically look like this:
public UIComponent getDashboard() {
return dashboard;
}
It should not contain any other line of code. The same applies to the setter method by the way. Any actions wherein you need to manipulate the component's children needs to be done in an action(listener) method, not in a getter/setter method.

Commandlink action and #viewscoped weird behaviour

I have some code generated by netbeans wizard 'JSF pages from entity classes'.
For those who don't use netbeans I will briefly describe what the wizard does.
It creates a JSF page List.xhtml which contains a datatable with a fixed size of ten rows and two commandlinks for scrolling its content (prev 10 and next 10).
The JSF page uses a managedbean with session scope and the above mentioned commandlinks return the String 'List'.
If I change the managed bean scope to #viewscoped it is re-created every time I push a commandlink. According to me it is a weird behavior because the view actually doesn't change (it always List.xhtml ) and I would have expected a smarted view scope mechanism.
I then changed the action associated to the commandlinks with a new one which does the same things but returns void. Now the #viewscope mechanism works well but I'm not sure it is correct to use an action method with a signature like this
public void doSomething()
// instead of
public String doSomething()
My concern is that a different JSF implementation can act in an impredictable way with that kind of actions.
Thanks
Filippo
What do you return in public String doSomething()?
If you return null (or an empty String) it should not re-create the view.
If you return a navigation case then the view is re-created, regardless whether it is the same that you are currently in.

Resources