Can i use immediate property in JSF to refresh my html page without calling any backing bean methods?? - jsf

Can i use immediate property in JSF to refresh my html page without calling any backing bean methods?? i am new to jsf please suggest & also explain use of immediate

Imagine that you have this code:
<h:form>
<h:selectOneMenu id="subscriptions"
value="#{subscriptionController.subscriptions}"
immediate="true">
<f:selectItem id="item1" itemLabel="News" itemValue="1" />
<f:selectItem id="item2" itemLabel="Sports" itemValue="2" />
<f:selectItem id="item3" itemLabel="Music" itemValue="3" />
<f:selectItem id="item4" itemLabel="Java" itemValue="4" />
<f:selectItem id="item5" itemLabel="Web" itemValue="5" />
</h:selectOneMenu>
</h:form>
Regarding the immediate attribute is a boolean flag indicating that component events should be sent to registered event listeners immediately rather than after the validation phase of the JSF request processing lifecycle. The immediate flag allows you bypass JSF validation for a particular component. Reference here.
JSF life cycle is not easy to understand but you can check a good explanation here: JSF Life cycle
At a next level of experience you can add ajax to improve even more your performance. And the most recommended is to use libraries in that case, for example RichFaces and PrimeFaces.
Regards,

According to your requirement it seems that you don't need to use immediate attribute.
Just use f:ajax to process(execute) and update(render) particular elements or group of elements. It will not harm the performance.
And to know JSF Lifecycle and detail of immediate attribute please check Baluc's Blog

Use reRender attribute by using richfaces components for refresh the page or particular region of a page.

Related

<h:selectOneMenu with conditional <f:selectItems shows options twice

I want to use a selectOneMenu to have a user choose a value. In some cases I want to disable one of the values shown in the menu. I tried using render on both the selectItems as well as selectOneMenu as well as added a ui:fragment around the Menu but I always get all the values from both lists shown. Any ideas how to prevent that?
Here my current last try that again resulted in twice the list and the item in question once enabled and once disabled in it:
<ui:fragment rendered="#{cc.attrs.showP==true}">
<h:selectOneMenu id="type" binding="#{cc.type}">
<f:selectItems value="#{typeDAO.findAll()}"/>
</h:selectOneMenu>
</ui:fragment>
<ui:fragment rendered="#{cc.attrs.showP==false}">
<h:selectOneMenu id="type" binding="#{cc.type}">
<f:selectItems value="#{typeDAO.findAll()}" var="item" itemDisabled="#{item=='P'}"/>
</h:selectOneMenu>
</ui:fragment>
Your concrete problem is caused because you're binding physically multiple components to the same variable.
<h:selectOneMenu ... binding="#{cc.type}" />
<h:selectOneMenu ... binding="#{cc.type}" />
If the getter behind binding returns non-null, then JSF will use it instead of creating a new one. Basically, the second tag will reuse the component created in the first tag and set/add all attributes/items to it.
Your particular case can be solved in at least two ways:
Use JSTL to build the JSF component tree conditionally instead of using JSF to render the HTML output conditionally. You shouldn't have physically multiple components in the JSF component tree sharing the same binding let alone the same id.
<c:if test="#{cc.attrs.showP}">
<h:selectOneMenu id="type" binding="#{cc.type}">
...
</h:selectOneMenu>
</c:if>
<c:if test="#{not cc.attrs.showP}">
<h:selectOneMenu id="type" binding="#{cc.type}">
...
</h:selectOneMenu>
</c:if>
Make your code DRY. I.e. get rid of all code duplication.
<h:selectOneMenu id="type" binding="#{cc.type}">
<f:selectItems value="#{typeDAO.findAll()}" var="item" itemDisabled="#{not cc.attrs.showP and item eq 'P'}" />
</h:selectOneMenu>
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?
JSTL in JSF2 Facelets... makes sense?
Guess I found it - bit weird to answer my question though. I think it's because the possible values of the menu are created before the rendered attributes are evaluated and since I did bind both menus to the same variable/id I got all items of the two menus. Thus I now used different names and then in my composite component have some logic that checks which one is used and continues to use the right value. Works :-)
Bit weird to me is this thing that as a developer u have to know when the attribute list is built in comparison to when the rendering happens. I recently had a similar issue with foreach and repeat. Is there any way to know these things as part of some overarching concept that I can remember or is that really case by case?
Thanks guys!

How to communicate back and forth between client and server-side in JSF?

Most of my problems in JSF, so far, seem to boil down to this - communication from (static) client-side to (dynamic) server-side, and vice-versa; for instance, for re-rendering components.
An example: enabling/disabling a button (commandButton) that depends on the selection of a selectoneradio.
What is the correct way to communicate the selection of the selectoneradio (client to server) and then ajaxingly updating the commandButton (server to client)?
By using <f:ajax>.
Here's an example which enables the button when the second item is selected.
<h:selectOneRadio value="#{bean.selectedItem}">
<f:selectItem itemValue="1" itemLabel="First item" />
<f:selectItem itemValue="2" itemLabel="Second item" />
<f:ajax render="button" />
</h:selectOneRadio>
<h:commandButton id="button" disabled="#{bean.selectedItem != 2}" />
Make sure that the #{bean} is a #ViewScoped one so that the state is remembered across postbacks. Else it'll fall back to default values when you press the submit button.
That said, I strongly recommend to go through a decent JSF book. The above is usually already covered in 1st chapter.

JSF SelectOneMenu Browser Back Button

I have a problem where I am using a SelectOneMenu for navigation:
<p:selectOneMenu value="#{navigator.outcome}">
<f:selectItem itemLabel="Select item.." />
<f:selectItems value="#{navigator.menuItems}" />
<p:ajax event="change" listener="#{navigator.navigate}" />
</p:selectOneMenu>
This works fine except when I navigate back it doesnt display "Select Item.."
The navigator bean is request scoped (I've tried view scoped as well).
I've disabled caching using a WebFilter.
You need to turn autocomplete off at the form level.
With JSF 2.1 you cannot do this.
Either wait until JSF 2.2 or uses omnifaces:
See link:
how to do autocomplete="off" at form level in JSF

h:selectOneRadio doesn't work with ajax change event

i have two radio buttons and no one is selected by default, and it's mandatory to select one of them, so here's what i did:
<div id="payment_method">
<h:message for="sel_payment_method" style="Color:red;"/>
<h:selectOneRadio id="sel_payment_method" required="true" requiredMessage="Please select a payment method" value="#{myBean.selectedPaymentMethod}">
<f:selectItem itemLabel="Debit or Credit Card" itemValue="credit" />
<f:selectItem itemLabel="Checking Account" itemValue="checking" />
<f:ajax event="change" render="credit_inputs_fragment checking_inputs_fragements" />
</h:selectOneRadio>
</div>
the selectedPaymentMethod property is a string and its default value is null
and the credit_inputs_fragment is a ui:fragement that contains a div
ISSUE: when clicking on a radio buttons the two fragments to be changed are not affected.
please advise, thanks.
You're not entirely clear in describing the concrete problem, but I can see at least two potential problems in the code posted so far:
The <f:ajax event="change"> is wrong in case of radiobuttons and checkboxes. It should be event="click". Or, better, just remove it altogether. It defaults to the right one already. See also What values can I pass to the event attribute of the f:ajax tag?
The component referenced in <f:ajax render> must be a fullworthy JSF UI component and always be rendered to the client side. If the component is by itself never rendered to the HTML, then JavaScript simply can't find it to update its content. You basically need the conditionally rendered components in another component which is always rendered. See also Why do I need to nest a component with rendered="#{some}" in another component when I want to ajax-update it?
i followed the example code in the following link, and it solved my issue:
JSF: Dynamically change form

ValueChangeListener problem

While calling the ValueChangeListener in JSF based on value change in dropdown, it is calling all the ValueChangeListner that are on that page.
There are two valueChangeListener in DataTable, while changing value in one dropdwon the 2nd one also executing.
<t:column id="avlId" styleClass="coltextcenteralign">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="#{bundle['travelLocalAccommodation.title.availability']}" />
<h:outputText value="*" style="color:red" />
</h:panelGroup>
</f:facet>
<t:selectOneMenu value="#{accomDtls.availability}" immediate="true"
valueChangeListener="#{TravelProcessingBB.localAccommodationBB.setAvailableFlag}" forceid="true" id="avl"
onchange="return availabilityAlert('#{accomDtls.prepopulatedFlag}','avl[#{table_count}]')"
styleClass="dropDownStyle" style="width:50">
<f:selectItem itemValue="Y" itemLabel="Yes" />
<f:selectItem itemValue="N" itemLabel="No" />
</t:selectOneMenu>
</t:column>
The value change alone won't automatically call the valueChangeListener. You need to submit the form as well. A commonly used "hack" is to call form.submit() using JavaScript during the onchange event. This will however submit the entire form. Truly the valueChangeListener will be triggered for all changed fields of the form.
To fix this in JSF 1.x, you need to hassle somewhat with the immediate attribute to skip all other form components from validating and with component binding so that you can properly get/set the other component's value. Long story short, I ever wrote an article about that: populate child menus in JSF 1.2.
In JSF 2.0, this is however easier to achieve with help of <f:ajax> tag. If you're really using JSF 2.0 (your current question doesn't indicate that), then let me know if you need an example.

Resources