JSF SelectOneMenu Browser Back Button - jsf

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

Related

p:selectOneMenu field with onChange not refreshing outputText

I have searched in google and Stack Overflow, and have come to this to try and get it to work however it still doesn't work.
Xhtml:
<p:panel id="testRefresh" header="Basic">
    <h:outputText value="#{sBean.headTest}" />
</p:panel>
<p:selectOneMenu  valueChangeListener="#{sBean.handleChange}">
    <f:selectItem itemLabel="" itemValue="#{null}" />
    <f:selectItem itemLabel="Cream Latte" itemValue="Cream Latte" />
    <f:selectItem itemLabel="Extreme Mocha" itemValue="Extreme Mocha" />
    <f:selectItem itemLabel="Buena Vista" itemValue="Buena Vista" />
    <p:ajax event="change" update=":testRefresh" process="#this" />
</p:selectOneMenu>
sBean.java
private String headTest;
//Getter and Setter for headTest and initialized in #PostSconstruct method
public void handleChange(ValueChangeEvent event) throws InterruptedException {
  headTest = (String) event.getNewValue();
}
Also do you know any editor online to test JSF with xhtml and respective bean? Would be easier to test.
Thanks for help.
In your *.xhtml I added <h:form></h:form> and slightly changed your update statement:
<h:form>
<p:panel id="testRefresh" header="Basic">
<h:outputText value="#{sBean.headTest}"/>
</p:panel>
<p:selectOneMenu valueChangeListener="#{sBean.handleChange}">
<f:selectItem itemLabel="" itemValue="#{null}"/>
<f:selectItem itemLabel="Cream Latte" itemValue="Cream Latte"/>
<f:selectItem itemLabel="Extreme Mocha" itemValue="Extreme Mocha"/>
<f:selectItem itemLabel="Buena Vista" itemValue="Buena Vista"/>
<p:ajax event="change" update="testRefresh"/>
</p:selectOneMenu>
</h:form>
Also do you know any editor online to test JSF with xhtml and respective bean? Would be easier to test.
As far as I know there is no online tool, but you could create a "skeleton" Spring project and test there. A good start to that is the Spring Initializr.
The issue was the throws InterruptedException in the Bean method, messes xhtml access to it.

Update object in bean without reload

On my page i have form with data from database. I want to implement, as default behaviour, writing all changes to db.
I found such example
<h:selectOneMenu value="#{bean.options}" onchange="submit()"
var="#{bean.options}" valueChangeListener="#{bean.changeListernMethod}">
<f:selectItem itemValue="1" itemLabel="option1" />
<f:selectItem itemValue="2" itemLabel="option2" />
<f:selectItem itemValue="3" itemLabel="option3" />
</h:selectOneMenu>
but it not call listener method and reload all page. I can't reload page on every inputText edit or menu selection change. Do you have idea how to achieve such behaviour? Update objects in bean without page reload.
<f:ajax listener="#{bean.changeListernMethod}" render="#form" event="valueChange"/>
This is how you implement partial submit, or in your words: update objects in bean without page reload. The line I typed will fire ajax and update (only) the form whenever a value is selected and is different from the previous selected.
For more info:
https://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/ajax.html

p:watermark clears on updating Component even if there is no value

I'm using Primefaces p:watermark on a p:inputText. Its working fine.
When ever I update the component its loosing watermark,even when there is no content inside the p:inputText
<h:form id="reg_frm">
<p:inputText id="name" value="#{user.name}"/>
<p:watermark value="your name" for="name" id="name_watermark" />
<p:selectOneMenu value="#{user.drpvalue}">
<f:selectItem itemLabel="One" itemValue="1"/>
<f:selectItem itemLabel="two" itemValue="2"/>
<f:selectItem itemLabel="three" itemValue="3"/>
<f:selectItem itemLabel="four" itemValue="4"/>
<p:ajax event="change" update="name name_watermark"/>
</p:selectOneMenu>
</h:form>
How do I retain the Watermark when there is no content in the
p:inputText after update?
Note: Primefaces version - 3.5
According to the PrimeFaces showcase for watermarks: "Watermark displays a hint about input fields by using native placeholder in supported browsers and a javascript solution in others browser compatibility."
PrimeFaces probably adds some hidden javascript stuff to the element which is a parent of both components (input and watermark). In your case that's the form which you would need to update. If you don't want that, put a new panelgroup around both elements and update that, which will have the same effect.
Try this attribute:
oncomplete="PrimeFaces.showWatermarks()"
It was given in Primefaces User's Guide.

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

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.

PrimeFaces calendar won't popup when used together with Tomahawk components

I have an application with a couple of Tomahawk components that I'd like to add a PrimeFaces calendar to. However, the PrimeFaces calendar won't popup unless I remove all the Tomahawk components. How is this caused and how can I solve it?
Edit- Here is part of the code I was talking about. I like the primefaces calendar time selector and want to use it, the tomahawk version is too clumsy. If I remove the tomahawk component the p:calendar works fine, but does not render when any tomahawk components are in my .xhtml (I use a tomahawk scheduler also that I didn't include here).
<h:outputLabel>Enter Appt Date:Time</h:outputLabel>
<t:inputDate popupCalendar="true" type="both" value="#{ScheduleMB.from}"/>
<p:calendar id="clientApptTime" value="#{ScheduleMB.datePrime}"
pattern="HH:mm" timeOnly="true" mode="popup" />
<h:outputLabel>Enter Appt. Length</h:outputLabel>
<h:selectOneMenu id="apptDuration" value="#{ScheduleMB.apptTime}">
<f:selectItem itemValue="45" itemLabel="45 minutes"/>
<f:selectItem itemValue="60" itemLabel="60 minutes"/>
<f:selectItem itemValue="90" itemLabel="90 minutes"/>
<f:selectItem itemValue="120" itemLabel="120 minutes"/>
</h:selectOneMenu>

Resources