Two different SelectItems return single selected value - jsf

I have a country class:
public class Country{
private Long id;
private String name;
}
and a person class that has two Country fields
public class Person{
private Country nationality;
private Country nationality2;
}
Now in JSF I use <f:selectItems> to return list of countries to select nationalities as following:
<h:form id="form1">
<h:selectOneMenu value="#{mybean.person.nationality.id}">
<f:selectItems value="#{mybean.countryList}" var="var" itemValue="#{var.id}"/>
</h:selectOneMenu>
<h:selectOneMenu value="#{mybean.person.nationality2.id}">
<f:selectItems value="#{mybean.countryList}" var="var" itemValue="#{var.id}"/>
</h:selectOneMenu>
<p:commandButton actionListener="#{mybean.save}" update="sometable #form"/>
</h:form>
Now the weird problem is that when I submit the form the value assigned to the second field (nationality2) is assigned to both nationality and nationality2 regardless of what has been selected for the first field. For example if the selected value for nationality is 1 and the selected value for nationality2 is 2, when I submit the form both fields have the value 2. Why is this occuring?
PS: JSF implementation is Mojarra 2.1.3

Your concrete problem is caused because you're setting copies of the same Country reference as selected value and then manipulating only the id property. All changes made in one reference get reflected in all other references as well.
E.g.
Country country = new Country();
person.setNationality1(country);
person.setNationality2(country);
country.setId(1); // Gets reflected in both nationalities!
You'd better set the whole Country entity as value instead of manipulating its properties. Create a Converter which converts between Country and id:
#FacesConverter(forClass=Country.class)
public class CountryConverter implements Converter {
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return (value instanceof Country) ? ((Country) value).getId() : null;
}
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null;
}
if (!value.matches("\\d+")) {
throw new ConverterException(new FacesMessage("Invalid country ID: " + value));
}
Long countryId = Long.valueOf(value);
MyBean myBean = context.getApplication().evaluateExpressionGet(context, "#{myBean}", MyBean.class);
for (Country country : myBean.getCountries()) {
if (countryId.equals(country.getId())) {
return country;
}
}
throw new ConverterException(new FacesMessage("Unknown country ID: " + value));
}
}
and use it as follows:
<h:selectOneMenu value="#{mybean.person.nationality1}">
<f:selectItems value="#{mybean.countries}" var="country" itemValue="#{country}" itemLabel="#{country.name}" />
</h:selectOneMenu>
<h:selectOneMenu value="#{mybean.person.nationality2}">
<f:selectItems value="#{mybean.countries}" var="country" itemValue="#{country}" itemLabel="#{country.name}" />
</h:selectOneMenu>

Try to give your Second selectOneMenu another name for var. For Example:
<h:selectOneMenu value="#{mybean.person.nationality2.id}">
<f:selectItems value="#{mybean.countryList}" var="var2" itemValue="#{var2.code}"/>
</h:selectOneMenu>
Otherwise you overwrite your changes in the (first) variable var by changing the second selectOneMenu.
EDIT:
If it does not solve the problem, try for tests to create a second countryList (countryList2) and attach it to the second selectOneMenu.
If the value still stays the same, the failure must be in the getters or setters.

Related

How store label in select menu to bean and populate in input text field [duplicate]

I am working on a JSF page which has a dropdown based on List<SelectItem>:
<h:selectOneMenu value="#{bean.selectedItem}">
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>
I need to get both the value and label of the currently selected item. Right now I only get the value. How can I get the label, too?
You can't. That's just how HTML works. You know, JSF is a HTML code generator. The JSF <h:selectOneMenu> generates a HTML <select><option> . The HTML <select> element will only send the value attribute of the selected <option> element. It will not send its label.
But that shouldn't be a big issue. You namely already know both the value and label in the server side, inside the #{bean.availableItems}. All you need to do to get the associated label is to get it by the value as key. I suggest to make it a Map which in turn can also be used in f:selectItems.
Basic kickoff example:
public class Bean {
private String selectedItem; // +getter +setter
private Map<String, String> availableItems; // +getter
public Bean() {
availableItems = new LinkedHashMap<String, String>();
availableItems.put("value1", "label1");
availableItems.put("value2", "label2");
availableItems.put("value3", "label3");
}
public void submit() {
String selectedLabel = availableItems.get(selectedItem);
// ...
}
}
with
<h:selectOneMenu value="#{bean.selectedItem}">
<f:selectItems value="#{bean.availableItems.entrySet()}" var="entry"
itemValue="#{entry.key}" itemLabel="#{entry.value}" />
</h:selectOneMenu>
and in result
<p>Selected label is #{bean.availableItems[bean.selectedItem]}</p>
An alternative is to wrap both name and value in a javabean object representing an entity and set the whole object as value, via a converter.
See also:
Our selectOneMenu wiki page
How to populate options of h:selectOneMenu from database?
Instead of Using Map I tried like this and it's perfectly working for me to get both ItemValue and ItemLabel in the same property by using "ItemValue" attribute in selectItems tag.How ever provided no extra commas in the ItemLabel(#asfas....i had the same problem u mentioned so i selected this approach).
<h:selectOneMenu value="#{company.issueDesc}" required="true" onchange="submit()">
<f:selectItem itemLabel="-- Select a Issue -- " itemValue="0"/>
<f:selectItems value="#{company.issueList}" var="model" itemLabel="#{model.IssueDesc}" itemValue="#{model.Code},#{model.IssueDesc}" >
</f:selectItems>
</h:selectOneMenu>
Basically IssueDesc is String type in Bean Company
public Class Company{
private String issueDesc; // getters and setters
private int code; // getters and setters
private List<T>issueList; // getters and setters.
public void getLblandVal(){
String desc=getIssueDesc();
String[] str_ary=desc.split(",");
String s1=str_ary[0];
String s2=str_ary[1];
// parse **s1** to int;
}
}
What if the the value should be Integer and label String and both are needed in backing bean. Using Map in bean doesn't work because JSF interprets the map key as label. Ideally it would be a LinkedHashMap and search the text by a number.
Seems upside down to search number (value) by a text (key). What if some implementation of JSF adds extra space to test or letter case changes for some reason. Then the value is not found from map.
This will do the trick.
private String getLabel(List<SelectItem> list, String selection) {
String label = "";
for (int i = 0; i < list.size(); i++) {
if(selection.equals((String)list.get(i).getValue())) {
label = (String)list.get(i).getLabel();
break;
}
}
return label;
}
In your example, you would pass in the availableItems as 'list' and selectedItem as 'selection'. This method will return the label value corresponding to the selectedItem.
The following approach may also be useful in getting value and label using List <SelectItem>:
Here, facade, statesFacade fetches list of states from database/enterprise bean.
In view (xhtml page):
<h:selectOneMenu id="statesSelectUi" value="#{applicationBean1.state}">
<f:selectItems value="#{applicationBean1.stateSelectItems}"/>
<f:ajax render="selectedItem" event="change" execute="statesSelectUi"/>
</h:selectOneMenu>
<br/>
<br/>
<h:outputText id="selectedItem" value="#{applicationBean1.selectedState}"/>
In the Managed Bean(applicationBean1.java):
private String state;
/**
* #return the stateSelectItems
*/
public List<SelectItem> getStateSelectItemsItems() {
stateSelectItems.add(new SelectItem("-1","--- Select state ---"));
int statesCount = statesFacade.count();
List<StateSelectItems> states;
states = statesFacade.findAll();
for (int i = 0; i < statesCount; i++) {
stateSelectItems.add(new SelectItem(states.get(i).getStateSlNo(), states.get(i).getStateName()));
}
return stateSelectItems;
}
public String getSelectedState(){
if("".equals(getState()) || getState() ==null){
return "";
}else{
return "Selected State : " + getStateSelectItems.get(Integer.parseInt(getState())).getValue()+", "++ getStateSelectItems.get(Integer.parseInt(getState())).getLabel();
}
}

richfaces valuechange event won't trigger on selectOneMenu

I have two .xhtml pages, each containing a form to enter data for a selected object. They only differ in the object to select.
First Page:
<h:outputText value="Buchung:"/>
<h:selectOneMenu converter="#{orderConverter}" value="#{reportBean.selectedOrder}" valueChangeListener="#{reportBean.valueChange}">
<f:selectItems value="#{orderBean.privateOrderList}" var="order" itemValue="#{order.ID}"/>
<a4j:ajax event="valueChange" execute="#this"/>
</h:selectOneMenu>
Second Page
<h:outputText value="Auto:"/>
<h:selectOneMenu converter="#{vehicleConverter}" value="#{orderBean.selectedVehicle}" valueChangeListener="#{orderBean.valueChange}">
<f:selectItems value="#{vehicleBean.vehicleList}" var="vehicle" itemValue="#{vehicle.licenseTag}"/>
<a4j:ajax event="valueChange" execute="#this"/>
</h:selectOneMenu>
The valueChange method of both backingBeans is basically the same, too:
ReportBean:
public void valueChange(ValueChangeEvent event){
if(null!=event.getNewValue()) {
selectedOrder = (Order) event.getNewValue();
}
}
OrderBean:
public void valueChange(ValueChangeEvent event){
if(null!=event.getNewValue()) {
selectedVehicle = (Vehicle) event.getNewValue();
}
}
As you can see, the object is converted before displaying but the conversion is just converting back and forth. Should not be the problem,
orderConverter for page 1 for Example:
#ManagedBean
#FacesConverter(value = "orderConverter")
public class OrderConverter extends GenericConverter implements Converter {
#Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
int i = Integer.parseInt(s);
return OrderDAO.getInstance().findById(i);
}
#Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
return o.toString();
}
}
vehicleConverter finds the object based on the passed string, instead of parsing it to integer in before.
The "toString()" methods concarcenate some strings for identification in the SelectOneMenu, so this should not be the problem.
Yet, page 2 triggers the valueChangeEvent and page 1 does not!
The only difference I see is, that the "itemValue" attribute of the f:selectItems is a String (licenseTag of the vehicle) on page 2 and an integer (order ID) on page 1. Is this the cause for this event not to trigger? If so, how do i solve this when i dont have another identification for order?

How to Compare the values JSF selectonemenu

I am developing a JSF application
I have 2 selectOnemenu controls and submit button.
I need to disable the button if the values of 2 fields are equal
<h:selectOneMenu id="from" value="#{currencyBean.history.fromCurrency}" >
<f:selectItems value="#{currencyBean.currency}" var="c" itemValue="#{c}" itemLabel="#{c.name}"/>
</h:selectOneMenu>
<p:outputLabel for="to" value="to:" />
<h:selectOneMenu id="to" value="#{currencyBean.history.toCurrency}" >
<f:selectItems value="#{currencyBean.currency}" var="c" itemValue="#{c}" itemLabel="#{c.name}"/>
</h:selectOneMenu>
<p:commandButton id="calculateButton" value="Convert" update=":convert :historyPanel" onstart="PF('statusDialog').show()" onsuccess="PF('statusDialog').hide()" validateClient="true" ajax="true" action="#{currencyBean.Calculate()}" />
I tried to use onchange with ajax but everytime I change one dropdown the value of the second drowpdown became null in the backbean so I cannot read it.
Here is my backbean
#Named(value = "currencyBean")
#RequestScoped
public class CurrencyBean implements Serializable {
/**
* Creates a new instance of CurrencyBean
*/
private History history;
private Currency currency;
private Date currentDate;
#Inject
private Loginbean loginBean;
private List<History> historyList;
public List<History> getHistoryList() {
int userId = loginBean.getUserId();
if (userId != -1) {
return new HistoryHelper().GetHistoryListByUser(userId);
}
return null;
}
public CurrencyBean() {
history = new History();
}
public History getHistory() {
return history;
}
public void setHistory(History history) {
this.history = history;
}
public Currency[] getCurrency() {
return Currency.values();
}
public Date getCurrentDate() {
return new Date();
}
public void Calculate() {
int userId = loginBean.getUserId();
if (userId != -1) {
new CurrencyClient().Convert(userId, this.history);
}
}
}
any clue ?
My assumption is that all of your problems come from your managed bean scope. You have #Request scope so every request your managed bean will be removed from container, thus when you define onchange="submit()" (this is only my assumption because you haven't define how you implement onchange attribute) and you select value from one selectBox component values for this component is updated but the first one is still null. When you select second selectBox value updated from first selectBox doesn't exists anymore as managed bean has been removed after first request. You should try with wider scope for instance #ViewScope. If it doesn't help then further informations like implementation onchange attribute will be needed

h:selectOneMenu with values from an enum becomes null after reload

I followed the advice from How to use enum values in f:selectItem(s) to build a h:selectOneMenu that takes the values of an enum. This works fine until I reload the page.
If I reload the page (Strg+R) what a user could do, the #{bean.relationship} property becomes null. All other properties, such as strings, numbers, etc. remain as they were before (Bean is #ViewScoped and #ManagedBean).
Here is the code from the JSF:
<h:selectOneMenu value="#{bean.relationship}">
<f:selectItems value="#{bean.relationshipTypes}"
var="types" itemValue="#{types}" itemLabel="#{types}" />
</h:selectOneMenu>
<h:inputText value="#{bean.name}" />
Here the code from the Enum:
public enum RelationshipType {
Family,
Friend
}
Here the code from the Bean:
private RelationshipType relationship; // plus getter & setter
private RelationshipType[] relationshipTypes;
private String name; // plus getter & setter
public RelationshipType[] getRelationshipTypes() {
return RelationshipType.values();
}
The enum is part of a larger entity. For ease of display a shortend version. Any idea?
#Geinmachi: Yet, other values stored in the bean are still there after the reload (e.g. name). So, why not for the property related to the enum?
I have used enums for data types
Enum
public enum Datatypes {
INT("Int"), FLOAT("Float"), DOUBLE("Double"), STRING("String"), DATE("Date"), DATETIME(
"DateTime"), BLOB("Blob");
private final String dataType;
private Datatypes(String dataType) {
this.dataType = dataType;
}
public String getDataType() {
return this.dataType;
}
#Override
public String toString() {
return dataType;
}
}
Bean
private List<Datatypes> datatypes = Arrays.asList(Datatypes.values());
public List<Datatypes> getDatatypes() {
return datatypes;
}
public void setDatatypes(List<Datatypes> datatypes) {
this.datatypes = datatypes;
}
xhtml
<p:selectOneMenu value="#{bean.dataType}">
<f:selectItem itemLabel="Select Data type"/>
<f:selectItems value="#{bean.dataTypes}" />
</p:selectOneMenu>
it working for me.

Using a "Please select" f:selectItem with null/empty value inside a p:selectOneMenu

I'm populating a <p:selectOneMenu/> from database as follows.
<p:selectOneMenu id="cmbCountry"
value="#{bean.country}"
required="true"
converter="#{countryConverter}">
<f:selectItem itemLabel="Select" itemValue="#{null}"/>
<f:selectItems var="country"
value="#{bean.countries}"
itemLabel="#{country.countryName}"
itemValue="#{country}"/>
<p:ajax update="anotherMenu" listener=/>
</p:selectOneMenu>
<p:message for="cmbCountry"/>
The default selected option, when this page is loaded is,
<f:selectItem itemLabel="Select" itemValue="#{null}"/>
The converter:
#ManagedBean
#ApplicationScoped
public final class CountryConverter implements Converter {
#EJB
private final Service service = null;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try {
//Returns the item label of <f:selectItem>
System.out.println("value = " + value);
if (!StringUtils.isNotBlank(value)) {
return null;
} // Makes no difference, if removed.
long parsedValue = Long.parseLong(value);
if (parsedValue <= 0) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Message"));
}
Country entity = service.findCountryById(parsedValue);
if (entity == null) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Message"));
}
return entity;
} catch (NumberFormatException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Message"), e);
}
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value instanceof Country ? ((Country) value).getCountryId().toString() : null;
}
}
When the first item from the menu represented by <f:selectItem> is selected and the form is submitted then, the value obtained in the getAsObject() method is Select which is the label of <f:selectItem> - the first item in the list which is intuitively not expected at all.
When the itemValue attribute of <f:selectItem> is set to an empty string then, it throws java.lang.NumberFormatException: For input string: "" in the getAsObject() method even though the exception is precisely caught and registered for ConverterException.
This somehow seems to work, when the return statement of the getAsString() is changed from
return value instanceof Country?((Country)value).getCountryId().toString():null;
to
return value instanceof Country?((Country)value).getCountryId().toString():"";
null is replaced by an empty string but returning an empty string when the object in question is null, in turn incurs another problem as demonstrated here.
How to make such converters work properly?
Also tried with org.omnifaces.converter.SelectItemsConverter but it made no difference.
When the select item value is null, then JSF won't render <option value>, but only <option>. As consequence, browsers will submit the option's label instead. This is clearly specified in HTML specification (emphasis mine):
value = cdata [CS]
This attribute specifies the initial value of the control. If this attribute is not set, the initial value is set to the contents of the OPTION element.
You can also confirm this by looking at HTTP traffic monitor. You should see the option label being submitted.
You need to set the select item value to an empty string instead. JSF will then render a <option value="">. If you're using a converter, then you should actually be returning an empty string "" from the converter when the value is null. This is also clearly specified in Converter#getAsString() javadoc (emphasis mine):
getAsString
...
Returns: a zero-length String if value is null, otherwise the result of the conversion
So if you use <f:selectItem itemValue="#{null}"> in combination with such a converter, then a <option value=""> will be rendered and the browser will submit just an empty string instead of the option label.
As to dealing with the empty string submitted value (or null), you should actually let your converter delegate this responsibility to the required="true" attribute. So, when the incoming value is null or an empty string, then you should return null immediately. Basically your entity converter should be implemented like follows:
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null) {
return ""; // Required by spec.
}
if (!(value instanceof SomeEntity)) {
throw new ConverterException("Value is not a valid instance of SomeEntity.");
}
Long id = ((SomeEntity) value).getId();
return (id != null) ? id.toString() : "";
}
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null; // Let required="true" do its job on this.
}
if (!Utils.isNumber(value)) {
throw new ConverterException("Value is not a valid ID of SomeEntity.");
}
Long id = Long.valueOf(value);
return someService.find(id);
}
As to your particular problem with this,
but returning an empty string when the object in question is null, in turn incurs another problem as demonstrated here.
As answered over there, this is a bug in Mojarra and bypassed in <o:viewParam> since OmniFaces 1.8. So if you upgrade to at least OmniFaces 1.8.3 and use its <o:viewParam> instead of <f:viewParam>, then you shouldn't be affected anymore by this bug.
The OmniFaces SelectItemsConverter should also work as good in this circumstance. It returns an empty string for null.
If you want to avoid null values for your select component, the most elegant way is to use the noSelectionOption.
When noSelectionOption="true", the converter will not even try to process the value.
Plus, when you combine that with <p:selectOneMenu required="true"> you will get a validation error, when user tries to select that option.
One final touch, you can use the itemDisabled attribute to make it clear to the user that he can't use this option.
<p:selectOneMenu id="cmbCountry"
value="#{bean.country}"
required="true"
converter="#{countryConverter}">
<f:selectItem itemLabel="Select"
noSelectionOption="true"
itemDisabled="true"/>
<f:selectItems var="country"
value="#{bean.countries}"
itemLabel="#{country.countryName}"
itemValue="#{country}"/>
<p:ajax update="anotherMenu" listener=/>
</p:selectOneMenu>
<p:message for="cmbCountry"/>
Now if you do want to be able to set a null value, you can 'cheat' the converter to return a null value, by using
<f:selectItem itemLabel="Select" itemValue="" />
More reading here, here, or here
You're mixing a few things, and it's not fully clear to me what you want to achieve, but let's try
This obviously causes the java.lang.NumberFormatException to be thrown
in its converter.
It's nothing obvious in it. You don't check in converter if value is empty or null String, and you should. In that case the converter should return null.
Why does it render Select (itemLabel) as its value and not an empty
string (itemValue)?
The select must have something selected. If you don't provide empty value, the first element from list would be selected, which is not something that you would expect.
Just fix the converter to work with empty/null strings and let the JSF react to returned null as not allowed value. The conversion is called first, then comes the validation.
I hope that answers your questions.
In addition to incompleteness, this answer was deprecated, since I was using Spring at the time of this post :
I have modified the converter's getAsString() method to return an empty string instead of returning null, when no Country object is found like (in addition to some other changes),
#Controller
#Scope("request")
public final class CountryConverter implements Converter {
#Autowired
private final transient Service service = null;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try {
long parsedValue = Long.parseLong(value);
if (parsedValue <= 0) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "The id cannot be zero or negative."));
}
Country country = service.findCountryById(parsedValue);
if (country == null) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "", "The supplied id doesn't exist."));
}
return country;
} catch (NumberFormatException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Conversion error : Incorrect id."), e);
}
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value instanceof Country ? ((Country) value).getCountryId().toString() : ""; //<--- Returns an empty string, when no Country is found.
}
}
And <f:selectItem>'s itemValue to accept a null value as follows.
<p:selectOneMenu id="cmbCountry"
value="#{stateManagedBean.selectedItem}"
required="true">
<f:selectItem itemLabel="Select" itemValue="#{null}"/>
<f:selectItems var="country"
converter="#{countryConverter}"
value="#{stateManagedBean.selectedItems}"
itemLabel="#{country.countryName}"
itemValue="${country}"/>
</p:selectOneMenu>
<p:message for="cmbCountry"/>
This generates the following HTML.
<select id="form:cmbCountry_input" name="form:cmbCountry_input">
<option value="" selected="selected">Select</option>
<option value="56">Country1</option>
<option value="55">Country2</option>
</select>
Earlier, the generated HTML looked like,
<select id="form:cmbCountry_input" name="form:cmbCountry_input">
<option selected="selected">Select</option>
<option value="56">Country1</option>
<option value="55">Country2</option>
</select>
Notice the first <option> with no value attribute.
This works as expected bypassing the converter when the first option is selected (even though require is set to false). When itemValue is changed to other than null, then it behaves unpredictably (I don't understand this).
No other items in the list can be selected, if it is set to a non-null value and the item received in the converter is always an empty string (even though another option is selected).
Additionally, when this empty string is parsed to Long in the converter, the ConverterException which is caused after the NumberFormatException is thrown doesn't report the error in the UIViewRoot (at least this should happen). The full exception stacktrace can be seen on the server console instead.
If someone could expose some light on this, I would accept the answer, if it is given.
This is fully working to me :
<p:selectOneMenu id="cmbCountry"
value="#{bean.country}"
required="true"
converter="#{countryConverter}">
<f:selectItem itemLabel="Select"/>
<f:selectItems var="country"
value="#{bean.countries}"
itemLabel="#{country.countryName}"
itemValue="#{country}"/>
<p:ajax update="anotherMenu" listener=/>
</p:selectOneMenu>
The Converter
#Controller
#Scope("request")
public final class CountryConverter implements Converter {
#Autowired
private final transient Service service = null;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.trim().equals("")) {
return null;
}
//....
// No change
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value == null ? null : value instanceof Country ? ((Country) value).getCountryId().toString() : null;
//**** Returns an empty string, when no Country is found ---> wrong should return null, don't care about the rendering.
}
}
public void limparSelecao(AjaxBehaviorEvent evt) {
Object submittedValue = ((UIInput)evt.getSource()).getSubmittedValue();
if (submittedValue != null) {
getPojo().setTipoCaixa(null);
}
}
<p:selectOneMenu id="tipo"
value="#{cadastroCaixaMonitoramento.pojo.tipoCaixa}"
immediate="true"
required="true"
valueChangeListener="#{cadastroCaixaMonitoramento.selecionarTipoCaixa}">
<f:selectItem itemLabel="Selecione" itemValue="SELECIONE" noSelectionOption="false"/>
<f:selectItems value="#{cadastroCaixaMonitoramento.tiposCaixa}"
var="tipo" itemValue="#{tipo}"
itemLabel="#{tipo.descricao}" />
<p:ajax process="tipo"
update="iten_monitorado"
event="change" listener="#{cadastroCaixaMonitoramento.limparSelecao}" />
</p:selectOneMenu>

Resources