Attach dynamically added values to jsf backing bean property - jsf

I have a jsf page which has a selection popup. User can select the members from this popup and click on 'Add'. After that, the selected members will be displayed on main page. This popuup is opened and closed through javascript and page doesn't refresh in this process.
Now the question is how can I take these selected members up-to backing bean? can I attach them to backing bean property directly?

Related

How to reset form to last loaded values from another form?

My JSF has two forms. Form1 is the main form and it is loaded with existing DB values when the page is loaded. Occasionally, as defined by the business logic, an update submission of Form1 will trigger a PrimeFaces p:dialog, which contains Form2 that collects extra data to complement Form1. If the user provides data that passes validation, the submit h:commandButton in Form2 will invoke a backing bean method that will save the data from both forms. However, if the user cancels the submit button or just closes the the p:dialog, I would like to roll back the state of the view to the last loaded values from the DB.
I have no preference whether to do it by using the PrimeFaces RequestContext from the backing bean in a method that is called by the cancel button or by doing it all in the JSF using either JSF EL or f:ajax. How can this be done?
E.g. RequestContext has a method reset(Collection<String> expressions) but I would like to not be specifying each form field to be reset, instead do all of them.

Using #ViewScoped with master template doesn't reset inputs

I am using a master template (i.e. <ui:composition template> / <ui:define>) where I have a navigation panel on the left, and the content panel in the center. When I go to one of my pages that is #ViewScoped, I edit some of the fields, go to another page which reloads the content area, and then go back to the original page, and the fields are all still populated with data. This implies that the view never ended. I tried #RequestScoped which produces the results I want, but breaks all of the ajax in the page related to that bean.
What is the best way to reset a page to its original state?
Can anyone see what is happening, exactly, regarding my layout and content area (consisting of a ui:include) being updated that would cause this not to be considered a change of View?
By navigating the content of the page using a backing bean, a ui:include updated by ajax, all in the same page, JSF's current view never goes out of scope, which is why the backing ViewScoped beans are never reset after navigating.
One possible fix for this is to redesign the page to use traditional navigation. In other words, lets say you have several p:menuitems. Rather than assigning the actionListener to some backing bean function that sets the content page (followed by an update of the panel containing the ui:include), you would set the "action" of each menuitem to the page to which each corresponds. This has the effect of causing a page to be loaded when a menuitem is clicked.
This generated the requirement that I had to reorganize my template a little so that every page of my site was embedded in it. There's a little bit of flicker as the entire page reloads, but the beans are being reset accordingly.

JSF CommandButton action before submit

I've created JSF 1.1 page.
In the page I have 2 textboxes and commandbutton.
The textboxes are connected to object in the backing bean, and thw object's values are showed there, and the user can edit it as he wants, and when he clicks on the button I want to go to the bean and save the new values in the object.
The bean must be request, not session!
<h:inputText value="myBean.PersonName"><\h:inputText>
<h:commandButton action="myBean.saveEditName"><\h:inputText>
backingBean:
public String saveEditName(){
//Go to database and save the new object
}
The problem is, when I click on the button, the bean is refreshed, the objects returns to its previous values, and then the action is invoked.
Why is that? How can I perform an action before the submit refreshes the bean? (Without using servlet in javascript).
Thanks!
Use Explicit Bean Declarations as Given Below:
<managed-bean>
<managed-bean-name>someName</managed-bean-name>
<managed-bean-class>
somePackage.SomeClass
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

JSF component is not getting reloaded for page refresh

I am new to JSF framework and Facelets as view, I am stuck with a problem now. I have got a page where i show some dropdown menu using <h:selectOneMenu> tag. On each selection i used to fire an ajax event using <f:ajax> event it all are working fine. But my problem is, if i select an option 2 on a select box and if I reloads the page again that particular select box will be selected with option 2 by default. I dont need this. I have to reload the select boxes along with page refresh. Please help me to solve this issue.
The selectbox shows the option that is set in the backing bean (and bound by the value attribute of <h:selectOneMenu>). The behavior after a page refresh depends on the scope of your backing bean. If it is session scoped, a page refresh doesn't reset the bean. You could change the scope to #ViewScoped to get the desired behavior. The bean then will be recreated after a full request.
Just set null to backing bean property that used in selectonemenu value after the selected action or set default value in property get method.

Backing Bean Initialization

I have a variable in backing bean that needs to get reset to null whenever the associated page is opened using the relevant menu link. Is there a way to run a initialization code in the backing bean whenever the relevant menu link is clicked? Contsructor runs only the first time the menu link is clicked. I guess the bean is then retained in the jsf context and is not getting recreated. Is there a way to ensure a new object of that backing bean is created each time the menu link is clicked? Thanks!
Couldn't you just put the bean in request scope?
Another option would be to use a setpropertyactionlistener on the menu. When the menu is clicked, set the value to "null".
You have the following options:
1 . Change the bean to the request-scoped bean
2 . Use the action attribute to call the method on the backing bean to run the initialization code whenever the link is clicked , something like this:
<h:commandLink action="#{myBean.init}" value="My Link" />
And myBean.init() contains the initialization code

Resources