Avoid invoking controller method for visted pages - jsf

I am using JSF and primefaces to develop my UI and using Java and Spring as server side.
In an xhtml page I am calling a controller method which will perform some logics and populates a HashMap in the bean class. In xhtml, the HashMap is been iterated using JSTL foreach loop to create the UI components. I have multiple pages in y application. So each xhtml page willl have previous and next buttons.
Consider, i have Page_1, Page_2, Page_3. All there pasges has set of radio buttons and check boxes. In each page i have next and previous buttons. On click of next button page_1, page_2 will be displayed. On click of previous button in page_2, page_1 will be displayed.
Each page wil call a controller method.
Page_1 (radio_buttons) <------> Page_2(check_boxes) <------> Page_3
My question is, if user checks/selects a radio button/checkbox in page_1 and go to page_2 sing next button in page_1 and when he returns back to page_1, how will I make the radio button to remain in selected state??
The bean which hold the selected radio button/check box values are resetting in the controller method when the page load. So i need to restrict from calling the controller if the page loads for second time. How can i achieve this?

Related

Rendered attribute executing before button action

I have two view htmls(V001 & V002) both of which are to be rendered using the same jsf page. There is a continue button in my JSF Page which performs the action of storing the V001 DOM in Cache and fetches the V002 from Content server and displays it to the user. This is achieved through a managed bean.
In view V002, I have a button which I need to be rendered based on whether the view is V002 or not.
However the issue is the rendered condition is executing before the continue action and hence always evaluate to false since the view change from V001 to V002 happens only when the continue button action is executed.
NB: My bean is request scoped and I am using JSF 1.1

Can Primefaces dialog framework display xhtml from an external domain?

when I read about the Primefaces Dialog Framework (DF), which is "used to open an external xhtml page in a dialog", I assumed this meant you could display xhtml from another domain.
If this is the case, how can I give an absolute url to the method...
requestContext.getCurrentInstance().openDialog(outcome)?
These don't seem to resolve.
If this is not the case, then what is the purpose/advantage of the DF? If I can only display xhtml from within the same app, why not just use the declarative <p:dialog> component? It can be put inside a <ui:composition> if you want to reuse a dialog from multiple pages, for instance.
OpenDialog enables the page author to open an xhtml referenced via a JSF navigation outcome in a dialog, the options are the configuration attributes for dialog like modal, draggable and finally the optional params are the view params to pass parameters to the dialog.
Actually the PrimeFaces blog entry referring to that means to use an internal application view outcome for the dialog to refer to. That bounds the path to your application JSF navigation cases.
Then what's the advantage of the dialog framework? I would say, bringing you the ability to specify the outcome and the dialog attributes at runtime. Let's say you have a car table, where you open a detail dialog when user clicks on one row. Suposing you need to display different content depending on car's branch, it would be easy to control what is going to be rendered inside the dialog:
String outcome = "dialogs/genericCar";
if (car.getBranch.equals("Ferrari")){
outcome = "dialogs/ferrariCar";
}
else if (car.getBranch.equals("Aston Martin")){
outcome = "dialogs/astonCar";
}
RequestContext.getCurrentInstance().openDialog(outcome);
Doing that would be so clean an straight forwarded. Otherwise with the classing p:dialog tag you're limited to a single dialog with conditional rendering inside depending on the content you want to display.
The chance of being able to change the dialog attributes depending on what you want to render seems interesting too.
See also:
PrimeFaces Dialog Framework
Blog entry

what happens to jsf components after render attribute is updated

Given the following scenario : A jsf component's (e.g a CommandButton) render attribute depends on an application scoped managed property. Since the property is shared across all sessions, the following might easily happen : User A loads a jsf page and the button's render attribute is true, so it is rendered. Now user B also loads the page and the render attribute is still true. Now user A clicks the button which causes the property to change its value and the button is not rendered anymore. User B still has the old view and although the render attribute is false now, he can click the button because he didn't update his view in the meantime. What happens now if user B clicks the button?
I thought the button's action is fired anyway because the render attribute is just used for rendering the button and has no influence anymore, once the page is rendered. But after doing some tests it seems to me that the render attribute is also checked again after clicking the button and if the attribute is false then, the action is not performed. Can someone confirm this ?
Disclaimer: I'll ignore the strange design for now.
But after doing some tests it seems to me that the render attribute is also checked again after clicking the button and if the attribute is false then, the action is not performed. Can someone confirm this ?
Yes, that's correct. This is part of safeguard against possibly tampered requests wherein the hacker is trying to simulate the invocation of an action component which is actually not rendered by the server side (such as an admin-only command button which is only rendered when the current user has the admin role). The rendered (and disabled and readonly) attributes are always re-checked during processing the form submit.
In your particular case, you'd like to have a copy of the condition responsible for the rendered attribute in the view scope so that only this copy will be used as long as you're interacting with the same view. This can be achieved by just injecting the application scoped property as a managed property of a view scoped managed bean and then referencing it in the rendered attribute instead.
#ManagedBean
#ViewScoped
public class ViewBean {
#ManagedProperty("#{appBean.rendered}")
private boolean rendered;
// ...
}
with
<h:commandButton ... rendered="#{viewBean.rendered}" />

Populating a page from Data table in modal panel

I'm trying to get some data from a datatable in rich:modal panel
The whole flow is as follows
When clicking on search button on main page, a modal panel pops up with appropriate data & check box
Till this point the application is working fine
After clicking on ok button, selected data should be populated into main page. This is where the code fails
I tried stuff like getRowData, getValues, etc. but in vain. This could be done by keeping the bean in session scope but I have to keep this bean in request scope using Apache MyFaces JSF 1.2
Two ways comes to mind:
Pass an extra request parameter (the search string and the page number?) so that the bean knows which data to preload.
Make use of MyFaces Orchestra to create a conversation scope which lies in between request and session scope and is exactly the scope you're looking for this particular functional requirement.

How to Disable components onClick of a Button in JSF?

In my application i have three buttons, If i click on one button I have to disable all the button till the operation on that button is finished.
I dont know how to disable the other two buttons. Kindly help.
Use JavaScript to get the HTML element from the DOM and then set its disabled attribute.
document.getElementById('clientId').disabled = true;
Note that the 'clientId' is the autogenerated HTML element ID. Rightclick page and View Source to find it out. If this contains prefixes like j_id and so on, then you need to give all parent UINamingContainer components like <h:form>, <h:dataTable>, <f:subview> and so on a fixed component ID, so that the client ID doesn't need to be autogenerated anymore.

Resources