After rerender, my input is losing its "class" value (JSF 1.2) - jsf

I have an inputtext like this:
<h:inputText id="cpfCnpjDestino"
class="#{actionCadastrarCotacaoDadosGerais.getStyle()}" tabindex="8"
required="false" maxlength="18"
value="#{actionCadastrarCotacaoDadosGerais.cotacaoDTO.cpfCnpjDestino}"
disabled="#{boxCotacao.isTipoFreteFob}"/>
There's a selecteone that depending on its value, rerenders the inputtext disabling it enabling it.
The thing is, after disabling and enabling it, it's losing its class value (its a mask for the input btw).
Is there a way to keep the class value?
EDIT: Actually, after the rerender, the inputtext is losing its class value, not after disabling and enabling.
EDIT: here are the codes
SelectOneMenu
<a4j:outputPanel id="panelTipoFrete">
<h:selectOneMenu id="tipoFrete" tabindex="1"
value="#{actionCadastrarCotacaoDadosGerais.cotacaoDTO.idTipoFrete}"
label="Selecione" class="selectMiddle" required="false">
<f:selectItems
value="#{actionCadastrarCotacaoDadosGerais.tiposFrete}"></f:selectItems>
<a4j:support ajaxSingle="true" event="onchange"
action="#{actionCadastrarCotacaoDadosGerais.renderizarCampoCNPJDestinatario()}"
reRender="cpfCnpjDestino" />
</h:selectOneMenu>
</a4j:outputPanel>
and the method:
public String getStyle(){
return "cpfCnpjCaracteresDestino";
}

Related

f:selectItems in a p:selectOneMenu are not triggering the setter for the given field

This is my current structure for my p:selectOneMenu:
<h:form id="groupSelectionForm">
<p:outputLabel value="Momentane Gruppe:" for="groupSelection" />
<p:selectOneMenu id="groupSelection" value="#{foodPlanManagementBean.selectedGroup}" style="width:150px">
<f:selectItem itemLabel="-" itemValue="#{null}"/>
<f:selectItems value="#{foodPlanManagementBean.getGroups()}" var="group" itemLabel="#{group.name}" itemValue="#{group}"/>
<p:ajax event="change"/>
</p:selectOneMenu>
</h:form>
This results in a checkbox containing a default value given by the single selectItem as well as a few generated options from the selectItems.
However, the setter for the given field "selectedGroup" is only triggering for the selectItem.
The selectItems do not seem to do anything when they are being clicked.
Any ideas?
try to define a listener in ajax component, ex:
<p:ajax id="seasonAjax" event="change" process="#this" listener="#{yourBean.yourMethod}" update="elementThatYouWantToUpdate" />
process = this to process selected element.
In selectItems don't use get method use directly list elements(put get/set in your bean) ex:
<f:selectItems value="#{yourBean.yourList}" var="" itemLabel="" itemValue="" />
If this doesn't work test if you need to use a converter, if selectedGroup is a complex object or pass directly identification of selectGroup( selectedGroup.id)
I hope it helps.

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" />

richfaces tooltip for inputtext

I want to have a tooltip for an inputtext and I need the tooltip to be updated when I complete the inputtext. I place this elements inside a form.
The inputtext takes the value from a backing bean, and the value is set in the bean only when I submit the form. Yhe tooltip also takes the value from the bean.\
Here is some code I use (of course, it is not working):
<h:inputText id="inp" value="#{individual.givenName}">
<a4j:support event="onchange" reRender="inp" />
</h:inputText>
<rich:toolTip id="inp_tip">#{individual.givenName}</rich:toolTip>
I want the tooltip to be updated when I type some text. Any idea how I can do this?
Thanks!
You can get the tooltip updated by implementing something like this:
<a4j:region id="a4jRegion">
<h:panelGroup layout="block" id="divTooltipInputText">
<h:inputText id="inp" value="#{individual.givenName}">
<a4j:support event="onchange" reRender="divTooltipInputText" />
</h:inputText>
<rich:toolTip for="inp" id="inp_tip">#{individual.givenName}</rich:toolTip>
</h:panelGroup>
</a4j:region>
The a4j:region will limit the processing on the onchange event to just the inputText and toolTip.

valuechangelistener does not populate values in text box

Here is my jsp:
<h:selectOneMenu value="#{member.dependentName}" onchange="this.form.submit()"
immediate="true" valueChangeListener="#{member.getDependentAddress}">
<f:selectItems value="#{member.dependentList}" />
</h:selectOneMenu>
<h:inputText value="#{member.personName}" immediate="true" />
<h:inputText value="#{member.dob}" immediate="true" />
And this, the function valuechangelistener fires.
public void getDependentAddress(ValueChangeEvent e) {
setPersonName((getDependentsList().get(e.getNewValue().toString())
.getDependentName()));
setDob(getDependentsList().get(e.getNewValue().toString()).getBirth());
System.out.println("New dob value : " + dob);
System.out.println("New name value : " + personName);
FacesContext.getCurrentInstance().renderResponse();
}
The two sysouts give the new value in the console but once the page loads, the fields are blank. I have tried all scopes for the bean. No go. What am i missing?
Thanks
You missed nothing. You've just something too much. To get it to work, you should remove immediate="true" from the to-be-changed components.
<h:selectOneMenu value="#{member.dependentName}" onchange="this.form.submit()"
immediate="true" valueChangeListener="#{member.getDependentAddress}">
<f:selectItems value="#{member.dependentList}" />
</h:selectOneMenu>
<h:inputText value="#{member.personName}" />
<h:inputText value="#{member.dob}" />
The immediate="true" on an UIInput component will cause its validations phase to take place in apply request values phase instead. This gives you the opportunity to use FacesContext#responseComplete() inside a valueChangeListener method skip other components which doesn't have immediate="true" set from being processed. As you have now, with immediate="true", they are also processed.
Please note that this is essentially a hack from the old JSF 1.x ages. If you're already using JSF 2.x, you should be using <f:ajax listener> instead.
<h:selectOneMenu value="#{member.dependentName}">
<f:selectItems value="#{member.dependentList}" />
<f:ajax listener="#{member.getDependentAddress}" render="name dob" />
</h:selectOneMenu>
<h:inputText id="name" value="#{member.personName}" />
<h:inputText id="dob" value="#{member.dob}" />
with
public void getDependentAddress() {
Dependent dependent = getDependentsList().get(dependentName); // Isn't that actually a Map instead of List?
personName = dependent.getDependentName();
dob = dependent.getBirth();
}

JSF dataTable with selectOneListbox

I have a dataTable that lists some objects and I want to set a property for those objects using a selectOneListbox. This is my dataTable
<h:dataTable value="#{someHandler.entities}"
binding="#{someHandler.dataTable}" var="entitiy">
<h:column>
<f:facet name="header">
<t:outputText value="Level" />
</f:facet>
<h:selectOneListbox id="level" value="#{entitiy.level}" size="1"
valueChangeListener="#{someHandler.changeLevel}"
onchange="submit()">
<f:selectItem itemValue="-" itemLabel="-" />
<f:selectItem itemValue="ALL" itemLabel="ALL" />
(and so on)
</h:selectOneListbox>
</h:column>
<h:column>
<f:facet name="header">
<t:outputText value="Name" />
</f:facet>
<h:outputText value="#{entitiy.name}" />
</h:column>
</h:dataTable>
The valueChangeListener looks like this:
public void changeLevel(ValueChangeEvent event) {
String newLevel = (String) event.getNewValue();
Logger logger = (Logger) dataTable.getRowData();
logger.setLevel(Level.toLevel(newLevel));
}
(dataTable is an HtmlDataTable object.)
However, the event object is always the same - no matter which row the selectOneListbox was in. (It seems always the logger in the first row). The Logger object I get is also not the one I want.
Any ideas? Thank you!
And anothers questions? Is the entitiy.setLevel() method called even though I have a valueChangeListener? I use entitiy.level because I want to show the chosen level as a default for those entity.
Thank you!
There are two potential problems here:
First, the onchange="submit()" submits the entire form. The valueChangeListener will be invoked on all input elements of which the submitted value differs from the initial value.
You need to preset the value behind #{entitiy.level} with the same value as the default menu option. E.g. in the constructor.
public Entity() {
level = "-";
}
Or, better, make the default value null.
<f:selectItem itemValue="#{null}" itemLabel="-" />
so that the valueChangeListener won't be invoked when the particular menu is not changed.
Or, when you are already on JSF 2.x (please always mention exact JSF impl/version in your JSF questions), you can use <f:ajax> tag for this without the need for a valueChangeListener with a hacky onchange="submit()".
Second, you need to ensure that the datatable value #{someHandler.entities} returns exactly the same list during the submit as it was during the initial request. So, do the data loading in the bean (post)constructor. In JSF 2.x you'd like to put the bean in the view scope as well.
Unrelated to the concrete problem, you can also just use <h:selectOneMenu> instead of a <h:selectOneListbox size="1">.

Resources