prime faces default value in selectOneMenu - jsf

i have problem with selectonemenu component
in View i have this code:
<p:selectOneMenu id="city" value="#{logisticsController.selectedTrip}" >
<f:selectItem itemValue="#{null}" itemLabel="Wybierz podroz" />
<f:selectItems value="#{logisticsController.tripList}" />
<p:ajax update="pojoPickList"
listener="#{logisticsController.handleTripChange}" />
</p:selectOneMenu>
logisticsController is SessionScoped bean and have init method:
#PostConstruct
public void init(){
SelectedTrip = null ;
}
but every time when i refresh page selectOneMenu shows old value.
i want set default value equals null, thanks for help

First of all, #PostConstruct annotation says to your Injection Framework (Spring or smth else) to execute this method after dependency injections are finished (http://docs.oracle.com/javaee/7/api/javax/annotation/PostConstruct.html).
Just to simplify, we can assume that this method will execute after bean creating.
So, as it's a session scoped bean - it's created only once for user session. That's why method "public void init()" isn't called on page refresh.
There is 2 simple ways how to solve your problem:
1) If it's possible - change your SessionScoped bean to a RequestScoped, after that bean will be created on every request (on every page refresh)
2) You can just add init method invoking in your xhtml page like this:
...
#{logisticsController.init()}
<p:selectOneMenu id="city" value="#{logisticsController.selectedTrip}" >
<f:selectItem itemValue="#{null}" itemLabel="Wybierz podroz" />
<f:selectItems value="#{logisticsController.tripList}" />
<p:ajax update="pojoPickList"
listener="#{logisticsController.handleTripChange}" />
</p:selectOneMenu>
...

Related

Reset selectoneMenu with another selectOneMenu without ajax

I want to reset a selectOneMenu with another selectOneMenu to the default value and I can't use ajax.
First menu:
<h:selectOneMenu value="#{bean.rel}" id="relSelection"
valueChangeListener="#{bean.onRelChange}" onchange="submit();"
immediate="true">
<f:selectItems value="#{bean.rel}" />
</h:selectOneMenu>
Menu I want to reset:
<h:selectOneMenu value="#{bean.domSelected}" id="domSelection"
valueChangeListener="#{bean.onDomChange}" onchange="submit()"
hideNoSelectionOption="true" immediate="true" >
<f:selectItem itemValue="#{null}" itemLabel="Select..."
noSelectionOption="true" />
<f:selectItems value="#{bean.domNames}" var="item"
itemLabel="#{item}" itemValue="#{item}"/>
</h:selectOneMenu>
If the first menu is selected after the second already was selected by the user I want to reset the second menu.
I expected that the second menu would display the noSelectionOption after I set its value to null from the onReleaseChange listener, but it keeps the selected value.
Listener:
public void onRelChange(ValueChangeEvent event) {
domSelected = null; //type String
rel = (String) event.getNewValue();
//som other stuff
}
I already tried the remoteCommand approach described here
by adding this:
<p:remoteCommand name="resetInputs" process="#this relSelection"
actionListener="#{bean.clearMenu}" immediate="true">
<p:resetInput target="domSelection" />
</p:remoteCommand>
In the first menu I called the remoteCommand via onChange="submit(); resetInputs()"
Any other Ideas or Ideas why this is not working?
Thanks!
Primefaces 6.1
JSF 2.2
GlassFish 4.1.1

Primefaces p:tabView: Value of selectOneMenu lost

I have a problem when I use a p:tabView with dynamic="true", and there is a h:selectOneMenu on one tab, and on the other is a commandLink which is ajax="false". After clicking to the commandLink twice the value of the selectOneMenu is lost.
This problem does not occur when the tabView is dynamic="false".
The value of the h:inputText is not lost, but I see the following warning in the logfile:
org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils decodeUIInput WARNING: There should always be a submitted value for an input if it is rendered, its form issubmitted, and it was not originally rendered disabled or read-only. You cannot submit a form after disabling an input element via javascript. Consider setting read-only to true instead or resetting the disabled value back to false prior to form submission. Component : {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /form/regional/region.xhtml][Class: javax.faces.component.html.HtmlBody,Id: j_id_5][Class: javax.faces.component.html.HtmlForm,Id: TestForm][Class: org.primefaces.component.tabview.TabView,Id: tabviewTest][Class: org.primefaces.component.tabview.Tab,Id: j_id_8][Class: javax.faces.component.html.HtmlInputText,Id: j_id_f]}
Here is the form:
<p:tabView dynamic="true" cache="true" id="tabviewTest">
<p:tab title="Tab 1">
<h:selectOneMenu value="#{Region.dropDownValue}" id="dropDown">
<f:selectItem itemLabel="" itemValue=""/>
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
<f:selectItem itemLabel="4" itemValue="4"/>
</h:selectOneMenu>
<h:inputText value="#{Region.inputValue}" />
</p:tab>
<p:tab title="Tab 2">
<p:commandLink ajax="false"
id="link"
value="Test"
actionListener="#{Region.someActionMethod}" />
</p:tab>
</p:tabView>
And here the Bean:
public class Region {
private Integer dropDownValue = 3;
private String inputValue = "Test";
public void someActionMethod(ActionEvent ev) {
System.out.println("someActionMethod called");
}
public Integer getDropDownValue() {
return dropDownValue;
}
public void setDropDownValue(Integer dropDownValue) {
this.dropDownValue = dropDownValue;
}
public String getInputValue() {
return inputValue;
}
public void setInputValue(String inputValue) {
this.inputValue = inputValue;
}
}
My Environment: Primefaces 5.0/5.1.RC1, Myfaces 2.1/2.2, Tomact 7
Any ideas what could be wrong?
What scope does your ManagedBean have?
When you use a RequestScope you are not able to submit your selectOneMenu with an UICommand component like p:commandLink when you set the ajax attribute to false. The changes are lost in this case.
Here are two possibilities to fix your problem:
Attempt 1: Set your Bean ViewScoped:
In most cases this will work. If you must use special annotations to annotate your beans (like Apache DeltaSpike #ViewAccessScoped for example), try to separate your bean into View and Controller beans, annotating the View with just simple #ViewScope and keeping all the values in it.
Attempt 2: Remove ajax="false" from p:commandLink:
This will work if your use-case allows it. For example, downloading a file with PrimeFaces will require explicit declaration that the ajax is not to be used, so this solution will not be applicable.
add ajax listener
<h:selectOneMenu value="#{Region.dropDownValue}" id="dropDown">
<f:selectItem itemLabel="" itemValue=""/>
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
<f:selectItem itemLabel="4" itemValue="4"/>
<p:ajax event="change" update="#this"/>
</h:selectOneMenu>

Invoke action method on click of h:selectOneMenu

I have the following drop down list:
<h:selectOneMenu value="#{user.favCoffee3}" onclick="">
<f:selectItems value="#{user.favCoffee3Value}" var="c"
itemLabel="#{c.coffeeLabel}" itemValue="#{c.coffeeValue}" />
</h:selectOneMenu>
I would like to launch some method from the bean by drop down list item click. How can I achieve it?
You can use the valueChangeListener attribute, pointing to a method in the managed-bean and add a submit() in the onchange attribute.
The form should look like :
<h:form>
<h:selectOneMenu valueChangeListener="#{bean.valueChanged}"
onchange="submit()">
<f:selectItem itemValue="1" itemLabel="First" />
<f:selectItem itemValue="2" itemLabel="Second" />
</h:selectOneMenu>
</h:form>
And the valueChangeListener method in the managed bean would be:
public void valueChanged(ValueChangeEvent event) {
//do your stuff
}
As part of the basic JSF toolkit, you can use the <f:ajax/> tag to submit (using ajax) your input without need for a full page submit/refresh. Using your code sample
Define the <f:ajax/> tag as a child of the dropdown menu
<h:selectOneMenu value="#{user.favCoffee3}" onclick="">
<f:selectItems value="#{user.favCoffee3Value}" var="c" itemLabel="#{c.coffeeLabel}" itemValue="#{c.coffeeValue}" />
<f:ajax listener="#{user.doSomething}"/>
</h:selectOneMenu>
Define a method (doSomething() in this example) in your backing bean that accepts an instance of AjaxBehaviorEvent
public void doSomething(AjaxBehaviorEvent abe){
//do what you want with your favCoffee3 variable here
}
Further reading:
The <f:ajax/> tag documentation by oracle
Seems to work for a4j:support. Your h:selectOneMenu would look like this:
<h:selectOneMenu value="#{user.favCoffee3}">
<f:selectItems value="#{user.favCoffee3Value}" var="c" itemLabel="#{c.coffeeLabel}" itemValue="#{c.coffeeValue}" />
<a4j:support event="onchange" action="#{user.onSelectOneMenuChange}">
</h:selectOneMenu>
You also need to add the following taglib:
<%# taglib uri="http://richfaces.org/a4j" prefix="a4j"%>

conditional rendered f:selectItem possible problems

I have a page with several h:selectOneMenu or p:selectOneMenu and I want to use the same page for editing and adding data.
When I will edit data I need f:selectItem. I know that this component doesn't have attribute rendered. And I read that I can use <c:if>.
Ok. For example, if I write
<p:selectOneMenu rendered="#{not empty bean.id}"
value="#{bean.selectedId}">
<c:if test="${editableBean != null}">
<f:selectItem itemLable="#{editableBean.name} itemValue=#{editableBean.id} />
</c:if>
<f:selectItems value="#{bean.listItems}" var="item"
itemLabel="#{item.name}" itemValue="#{item.id}"/>
</p:selectOneMenu>
Will it works without any problems in primefaces and with ajax listeners?
The easy solution (but with poor performance) will be to have a boolean editMode attribute in your managed bean to enable/disable the components. Basic example:
<p:selectOneMenu rendered="#{not empty bean.id}" disabled="#{bean.editMode}"
value="#{bean.selectedId}">
<f:selectItems value="#{bean.listItems}" var="item"
itemLabel="#{item.name}" itemValue="#{item.id}"/>
</p:selectOneMenu>
In your bean
#ManagedBean
#ViewScoped
public class Bean {
private int id;
private boolean editMode;
//other attributes...
//getters and setters...
#PostConstruct
public void init() {
//a way to know if the bean it's in edit mode
editMode = (id != 0);
}
}
This solution will have poor performance because every <p:selectOneMenu> will have to load all the data and then select the actual value, but it will do what you want. Another option will be to use this attribute for the rendered property of <p:selectOneMenu> and for an <h:inputText disabled="true" readonly="true" /> (or maybe <h:outputText />). Another basic sample:
<p:selectOneMenu rendered="#{not empty bean.id && not bean.editMode}"
value="#{bean.selectedId}">
<f:selectItems value="#{bean.listItems}" var="item"
itemLabel="#{item.name}" itemValue="#{item.id}"/>
</p:selectOneMenu>
<h:inputText rendered="#{bean.editMode}" value="{bean.selectedText}"
disabled="true" readonly="true" />

JSF / Primefaces: Pre Select an option in a OneMenu

I have a simple OneMenu in JSF:
#ManagedBean
#ViewScoped
public class ProductBean {
...
protected static Map<String, String> priceTypes;
...
getter & setter
}
<p:selectOneMenu id="sizeType" >
<f:selectItems value="#{productBean.priceTypes}" />
</p:selectOneMenu>
In my usecase, i want to preselect[1] an option out of "priceTypes" - how can i do that?
I am using Glassfish 3.1.2.2 with Primefaces 3.4.1
[1] See "selected" at http://www.w3schools.com/tags/tag_option.asp
I am not sure about pfaces, but in plain JSF you just have to set the value attribute in the selectOneMenu tag, and make sure that the value returned by the bean is in the select list.
<p:selectOneMenu id="sizeType" value="#{myBean.sizeType}>
<f:selectItems value="#{productBean.priceTypes}" />
</p:selectOneMenu>
Of course, sizeType must mutch the key of your map.

Resources