JSF <h:selectOneMenu> issue .? - jsf

Here is my JSF .xhtml code. issue is that JSF value not sent to server when its change & save.its still send to server just earlier values
please help me to sort out this issue
thanks all :-)
<ui:decorate template="/jsf/templates/one_column.xhtml">
<ui:define name="label">
<h:outputText value="Card Status" style="font-weight:bold;" />
</ui:define>
<ui:define name="field">
<h:selectOneMenu value="#{profileController.selectedBean.title}">
<f:selectItems value="#{profileController.titles}"/>
</h:selectOneMenu>
<h:selectOneMenu
value="#{cardManagementController.selectedCardBean.cardStatusId}" >
<f:selectItem itemValue="null" itemLabel="please select"
noSelectionOption="true" />
<f:selectItems
value="#{cardManagementController.listCardTypeStatus}"
var="cardStatus" itemLabel="#{cardStatus.name}"
itemValue="#{cardStatus.statusId}" />
</h:selectOneMenu>
</ui:define>
</ui:decorate>
also here i will show my class codes
private CardBean selectedCardBean = new CardBean();
public void editCard() {
if (selectedCardBean != null) {//THIS selectedCardBean is still shows earlier records
cardService.editCardInformation(selectedCardBean);
selectedCardBean = new CardBean();
editCardEnabled = false;
}
searchCard();
}
Code Added
<!-- Buttons -->
<ui:decorate template="/jsf/templates/one_column.xhtml">
<ui:define name="label">
</ui:define>
<ui:define name="field">
<a4j:commandButton value="Submit" execute="#form"
action="#{cardManagementController.editCard()}"
render="#{rich:clientId('hpnlRes')} #{rich:clientId('hpnlSearch')} #{rich:clientId('hpnlEdit')}" />
<a4j:commandButton value="Back" style="margin-left:10px;"
action="#{cardManagementController.hideEditCard()}"
render="#{rich:clientId('hpnlRes')} #{rich:clientId('hpnlSearch')} #{rich:clientId('hpnlEdit')}" />
</ui:define>
</ui:decorate>

issue with my controller class..i fixed it thanks all
there i get item value
but pass from form its id
thatswhy
<f:selectItems
value="#{cardManagementController.listCardTypeStatus}"
var="cardStatus" itemLabel="#{cardStatus.name}"
itemValue="#{cardStatus.statusId}" />

Related

Opening dialog validates the main form what I don't want

If I click on the add button (for example the "CreateCategory1" Id) to add one new product category I get the "Name field is required" validation message. This should open one dialog, where I can enter the new category name, nothing else. At this point I don't want to validate the main form, because I just want to add one new product category, which I will be using later. Why is the dialog opening validating the form? How can I avoid this?
Thank you very much!
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:composition >
<h:outputScript library="js" name="warning.js"/>
<h:form id="ProductEditForm" target="_blank">
<div class="ui-fluid">
<p:panelGrid columns="4" columnClasses="ui-grid-col-6,ui-grid-col-2,ui-grid-col-2,ui-grid-col-2" layout="grid" styleClass="ui-panelgrid-blank" >
<p:outputPanel style="height: 70px" >
<p:breadCrumb model="#{breadCrumb.productMenuModel}" style="background: inherit; border: none" />
<h2>#{productController.selected.name}</h2>
</p:outputPanel>
<p:commandButton style="height: 30px; background: #{userController.headerColor}" icon="ui-icon-check" oncomplete="javascript:saved=true" action="#{productController.update}" actionListener="#{breadCrumb.navigateProduct()}" update=":center,:growl" value="#{bundle.Save}" />
<p:commandButton style="height: 30px" icon="ui-icon-close" oncomplete="javascript:saved=true" action="#{productController.refreshSelected()}" actionListener="#{breadCrumb.navigateProduct()}" update=":center,:growl" value="#{bundle.Cancel}"/>
<p:commandButton style="height: 30px" icon="ui-icon-document" update="#([id$=ProductHistoryDateForm])" oncomplete="PF('ProuctHistoryDateDialog').show()" value="Print history" disabled="#{empty productController.selected}" />
</p:panelGrid>
<p:panelGrid columns="2" columnClasses="ui-grid-col-6,ui-grid-col-6" layout="grid" >
<p:panel>
<h3>Main info</h3>
<p:panelGrid columns="2" columnClasses="ui-grid-col-3,ui-grid-col-9" layout="grid" styleClass="ui-panelgrid-blank">
<p:outputLabel value="#{bundle.EditProductLabel_name}" for="name" />
<p:inputText id="name" value="#{productController.selected.name}" title="#{bundle.EditProductTitle_name}" required="true" requiredMessage="#{bundle.EditProductRequiredMessage_name}"/>
<p:outputLabel value="#{bundle.EditProductLabel_originalName}"/>
<p:inputText id="originalName" value="#{productController.selected.originalName}"/>
<h:outputText value="#{bundle.ViewInventoryItemLabel_serialized}"/>
<p:selectBooleanCheckbox disabled="false" value="#{productController.selected.serialized}" />
<p:outputLabel value="#{bundle.EditProductLabel_ean}" for="ean" />
<p:inputText id="ean" value="#{productController.selected.ean}" title="#{bundle.EditProductTitle_ean}" />
<p:outputLabel value="#{bundle.EditProductLabel_itemNr}" for="itemNr" />
<p:inputText id="itemNr" value="#{productController.selected.itemNr}" title="#{bundle.EditProductTitle_itemNr}" />
...
</p:panelGrid>
<h3>Sales</h3>
<p:panelGrid columns="2" columnClasses="ui-grid-col-3,ui-grid-col-9" layout="grid" styleClass="ui-panelgrid-blank">
<p:outputLabel for="defaultSupplier" value="#{bundle.EditProduct_defaultSupplier}"/>
<p:selectOneMenu id="defaultSupplier" filter="true" value="#{productController.selected.defSupplierPartner}" title="#{bundle.EditProduct_defaultSupplier}" effect="fold" editable="false">
<f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{partnerController.items}"
var="partnerDataIdItem"
itemValue="#{partnerDataIdItem}"
itemLabel="#{partnerDataIdItem.name}"/>
</p:selectOneMenu>
<p:outputLabel value="#{bundle.EditProductLabel_purchaseDeliveryTime}" for="purchaseDeliveryTime" />
<p:inputText id="purchaseDeliveryTime" value="#{productController.selected.purchaseDeliveryTime}" title="#{bundle.EditProductTitle_purchaseDeliveryTime}" />
<p:outputLabel value="#{bundle.EditProductLabel_lastPurchase}" />
<p:inputText id="lastPurchase" value="#{productController.selected.lastPurchasePrice}" />
...
</p:panelGrid>
</p:panel>
<p:panel id ="productGroups">
<h3>Product categories</h3>
<p:panelGrid columns="3" columnClasses="ui-grid-col-1,ui-grid-col-1,ui-grid-col-11" layout="grid" styleClass="ui-panelgrid-blank">
<p:commandButton id="CreateCategory1" actionListener="#{productCategoryController.prepareCreate(1)}" update="productGroups, ProductCategoryCreateForm" icon="ui-icon-plus" oncomplete="PF('ProductCategoryCreateDialog').show()"/>
<p:commandButton id="DeleteCategory1" disabled="#{productController.selected.productCategoryId1 == null}" update="category1" actionListener="#{productCategoryController.deleteProductCategory(productController.selected.productCategoryId1)}" icon="ui-icon-minus" />
<p:selectOneMenu id="category1" value="#{productController.selected.productCategoryId1}" label="#{productController.selected.productCategoryId1.name}" effect="fold">
<f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{productCategoryController.getItemsByLevel(1)}"
var="item"
itemLabel="#{item.name}"/>
<p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(1)}"/>
</p:selectOneMenu>
<p:commandButton id="CreateCategory2" rendered="#{productController.selected.productCategoryId1 != null}" actionListener="#{productCategoryController.prepareCreate(2)}" update="productGroups, ProductCategoryCreateForm" icon="ui-icon-plus" oncomplete="PF('ProductCategoryCreateDialog').show()"/>
<p:commandButton id="DeleteCategory2" disabled="#{productController.selected.productCategoryId2 == null}" update="category2" actionListener="#{productCategoryController.deleteProductCategory(productController.selected.productCategoryId2)}" icon="ui-icon-minus" />
<p:selectOneMenu id="category2" rendered="#{productController.selected.productCategoryId1 != null}" label="#{productController.selected.productCategoryId2.name}" value="#{productController.selected.productCategoryId2}" effect="fold">
<f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{productCategoryController.getItemsByLevel(2)}"
var="item"
itemLabel="#{item.name}"/>
<p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(2)}"/>
</p:selectOneMenu>
<p:commandButton id="CreateCategory3" rendered="#{productController.selected.productCategoryId2 != null}" actionListener="#{productCategoryController.prepareCreate(3)}" update="productGroups, ProductCategoryCreateForm" icon="ui-icon-plus" oncomplete="PF('ProductCategoryCreateDialog').show()"/>
<p:commandButton id="DeleteCategory3" disabled="#{productController.selected.productCategoryId3 == null}" update="category3" actionListener="#{productCategoryController.deleteProductCategory(productController.selected.productCategoryId3)}" icon="ui-icon-minus" />
<p:selectOneMenu id="category3" label="#{productController.selected.productCategoryId3.name}" rendered="#{productController.selected.productCategoryId2 != null}" value="#{productController.selected.productCategoryId3}" effect="fold">
<f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{productCategoryController.getItemsByLevel(3)}"
var="item"
itemLabel="#{item.name}"/>
<p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(3)}"/>
</p:selectOneMenu>
<p:commandButton id="CreateCategory4" rendered="#{productController.selected.productCategoryId3 != null}" actionListener="#{productCategoryController.prepareCreate(4)}" update="productGroups, ProductCategoryCreateForm" icon="ui-icon-plus" oncomplete="PF('ProductCategoryCreateDialog').show()"/>
<p:commandButton id="DeleteCategory4" disabled="#{productController.selected.productCategoryId4 == null}" update="category4" actionListener="#{productCategoryController.deleteProductCategory(productController.selected.productCategoryId4)}" icon="ui-icon-minus" />
<p:selectOneMenu id="category4" label="#{productController.selected.productCategoryId4.name}" rendered="#{productController.selected.productCategoryId3 != null}" value="#{productController.selected.productCategoryId4}" effect="fold">
<f:selectItem itemLabel="#{bundle.SelectOneMessage}" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{productCategoryController.getItemsByLevel(4)}"
var="item"
itemLabel="#{item.name}"/>
<p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(4)}"/>
</p:selectOneMenu>
</p:panelGrid>
</p:panel>
</p:panelGrid>
</div>
</h:form>
</ui:composition>
</html>
This is NOT a direct answer but a long extensive comment on what is all wrong with this question and how the real problem could simply be narrowed down by making a [mcve]... with a simple answer at the end.
Remove the composition
Remove the h:outputScript
Remove all styling
Remove the structural divs panelGrid and more
Remove 'bundles' from label and make them static
Remove most inputs (leave one in so that it still demonstrates the problem)
Remove most buttons that open a dialog and leave one (the one you referred to) in so that it still demonstrates the problem
What it could look like then
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:form id="ProductEditForm" target="_blank">
<p:commandButton id="CreateCategory1" actionListener="#{productCategoryController.prepareCreate(1)}" update="productGroups, ProductCategoryCreateForm" oncomplete="PF('ProductCategoryCreateDialog').show()"/>
<p:selectOneMenu id="category1" value="#{productController.selected.productCategoryId1}" label="Category 1">
<f:selectItem itemLabel="Select one" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{productCategoryController.getItemsByLevel(1)}" var="item" itemLabel="#{item.name}"/>
<p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(1)}"/>
</p:selectOneMenu>
<p:selectOneMenu id="category2" rendered="#{productController.selected.productCategoryId1 != null}" label="Category 2" value="#{productController.selected.productCategoryId2}" effect="fold">
<f:selectItem itemLabel="Select one" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{productCategoryController.getItemsByLevel(2)}" var="item" itemLabel="#{item.name}"/>
<p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(2)}"/>
</p:selectOneMenu>
</h:form>
</html>
You could even make it more simple by using plain p:inputText instead of p:selectOneMenus.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:form id="ProductEditForm" target="_blank">
<p:commandButton id="CreateCategory1" actionListener="#{productCategoryController.prepareCreate(1)}" update="productGroups, ProductCategoryCreateForm" oncomplete="PF('ProductCategoryCreateDialog').show()"/>
<p:inputText id="category1" value="#{productController.selected.productCategoryId1}" label="Category 1">
<p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(1)}"/>
</p:inputText>
<p:inputText id="category2" rendered="#{productController.selected.productCategoryId1 != null}" label="Category 2" value="#{productController.selected.productCategoryId2}">
<p:ajax event="change" update="productGroups" listener="#{productController.changeCategory(2)}"/>
</p:inputText>
</h:form>
</html>
And if the problem still occurs an h:inputText with f:ajax And even an h:commandButton with an f:ajax (changing the oncomplete to onevent) , making it less PrimeFaces dependend (and still causing an error).
But now we come to it... The oncomplete that opens the dialog... Is it actually related to opening the dialog? What if you'd use a simple oncomplete="alert('Hi there');" in the commandButton? Does it fail to? 100% sure it does, so your title
Opening dialog validates the main form what I don't want
and your question
"Why is the dialog opening validating the form?"
Are both wrong... What if you remove the oncomplete completely? Still fails? Yes... Hmmmm... So it is is just related to pressing a button. Well what does a button do? It submits a form... validating it... So effectively your question becomes
"How can I prevent a commandButton from submitting and validating my whole form"
And you could also have come here by starting to remove the oncomplete in the first place, by asking yourself the question related to the title:
"IS it related to opening the dialog? What If I don't open the dialog? Where do I do that? In the oncomplete, so what if I remove that").
If you posted your new question in a search engine and added 'PrimeFaces JSF site:stackoverflow.com' to it, you'd after some reading have found:
PrimeFaces disable validation on cancel button
Which is exactly like stated in the comment. Might not be sufficient (you might need to add some other id to the process attribute of fields you do want to submit with the specific button, but you'd have a more detailed to the point question then.
You could, of course also make more than one form, submitting smaller parts
How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?

Omit validation for p:selectOneMenu, for Ajax requests

I have a Jsf page with a fragment with <p:selectOneMenu/> (Prime faces) and an Ajax associated with it. It have the default select item "--- Select One ---" and others items that were created dynamically.
This field is required, so if users submit the form without select, the page show a error message.
The problem occurs when I select one of this others options and go back selecting "--- Select One ---" again. Then the page shows the required field message, even before I submit the form. I try different ways of immediate in <p:ajax> and <p:selectOneMenu> tags but none of them has the expected behavior.
The <p:messages> is declared as below:
-- list.xhtml
<p:messages id="messages" autoUpdate="true" closable="true" escape="false" />
<.... include fragment.xhtml ....>
And the fragment:
-- fragment.xhtml
<p:selectOneMenu id="selectTipoCarro"
value="#{carroBean.carro.tipoCarroEnum}"
required="true">
<f:selectItems value="#{carroBean.listaSelectItemTipoCarroInclusao}" />
<p:ajax update="outputInicioVigenciaWrapper outputLabelCalendarInicioVigenciaWrapper"
listener="#{carroBean.aoMudarTipoCarro}" />
</p:selectOneMenu>
<h:panelGroup id="outputLabelCalendarInicioVigenciaWrapper">
<h:outputLabel id="outputLabelCalendarInicioVigencia"
rendered="#{carroBean.edicaoDataInicioVigenciaDisponivel}"
for="calendarInicioVigencia">
<span>*
#{labels['carro.inicio.vigencia']}: </span>
<p:calendar id="calendarInicioVigencia"
value="#{carroBean.carro.dataInicioVigencia}"
showOn="button"
pattern="dd/MM/yyyy" mask="true"
required="true"/>
</h:outputLabel>
</h:panelGroup>
<h:panelGroup id="outputInicioVigenciaWrapper">
<h:outputLabel for="outputInicioVigencia"
rendered="#{not carroBean.edicaoDataInicioVigenciaDisponivel}">
<span aria-live="polite">
<h:outputText id="outputInicioVigencia"
value="#{carroBean.carro.dataInicioVigencia}"
styleClass="dataFormat"
</h:outputText>
</span>
</h:outputLabel>
</h:panelGroup>
private SelectItem obterSelectItemSelecione() {
SelectItem selectItem = new SelectItem("", "-- Select One --");
return selectItem;
}
private void preencherListaSelectItemTipoCarro(List<SelectItem> select, TipoCarroEnum[] tiposCarrosConsiderados) {
select.clear();
select.add(obterSelectItemSelecione());
for (TipoCarroEnum tipoCarro : tiposCarrosConsiderados) {
select.add(new SelectItem(tipoCarro, tipoCarro.getNome()));
}
}
public void aoMudarTipoCarro() {
getCarro().setDataInicioVigencia(carroService.obterProximaDataInicioVigenciaDisponivel(getCarro().getTipoCarroEnum()));
}
That's the expected behaviour. When adding a p:ajax tag to your p:selectOneMenu you make the value be processed everytime the user changes the input, so it will be validated and rejected if you mark it as required. My favourite workaround for this cases is to include a request param in the button to submit the whole form and check for it in the required attribute. That's it:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:comp="http://java.sun.com/jsf/composite/comp"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head />
<h:body>
<h:form>
<p:messages autoUpdate="true" />
<p:selectOneMenu value="#{val}"
required="#{param['validate']}">
<p:ajax event="change" />
<f:selectItem itemLabel="None" noSelectionOption="true" />
<f:selectItem itemLabel="val1" itemValue="val1" />
</p:selectOneMenu>
<p:commandButton value="Submit" ajax="false">
<f:param name="validate" value="true" />
</p:commandButton>
</h:form>
</h:body>
</html>
See also:
Conditionally skip JSF validation but perform model updates
Explanation
This happens because you have a p:messages component with autoUpdate set to true and you have attached p:ajax to your selectOneMenu so anytime your value changes, p:messages is updated. Therefore, due to the use of required="true" an error messages is shown as soon as you have selected "--- Select One ---".
Solution
Add ignoreAutoUpdate="true" to your p:ajax or
Remove autoUpdate="true" if not necessary

Faces notifications not working

I am having a strange issue with the Faces messages.
Sometimes, the <p:growl>, <p:messages> display correctly the notification after executing some method. But there are specific JSF pages where no notification is shown.
For example:
public String create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("NotaCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
The method is called in
<h:commandLink action="#{notaController.create}" value="#{bundle.CreateNotaSaveLink}"/>
And it does not show the notification. But for other entities as well as for other operations the messages are displayed correctly. This happens also in other functions, where I need feedback and it is not shown as expected.
I've been searching around and read this post and tried some of the suggested solutions, including setting the ajax=false attribute in commandButtons and commandLinks or using the scripts posted, but none of that worked for me, so I ask this question hoping that someone can help me.
Do I have to configure something to make it work correctly? I need this to get fixed by tomorrow and I don't know what else to do.
Update:
These are the JsfUtil methods:
public static void addSuccessMessage(String msg) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
}
public static void addErrorMessage(Exception ex, String defaultMsg) {
String msg = ex.getLocalizedMessage();
if (msg != null && msg.length() > 0) {
addErrorMessage(msg);
} else {
addErrorMessage(defaultMsg);
}
}
This is the Create.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/template.xhtml">
<ui:define name="title">
<h:outputText value="#{bundle.CreateNotaTitle}"></h:outputText>
</ui:define>
<ui:define name="body">
<h:panelGroup id="messagePanel" layout="block">
<h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
</h:panelGroup>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="#{bundle.CreateNotaLabel_fecha}" for="fecha" />
<p:calendar value="#{notaController.selected.fecha}" id="fecha"/>
<h:outputLabel value="#{bundle.CreateNotaLabel_observaciones}" for="observaciones" />
<p:inputTextarea rows="4" cols="30" id="observaciones" value="#{notaController.selected.observaciones}" title="#{bundle.CreateNotaTitle_observaciones}" />
<h:outputLabel value="#{bundle.CreateNotaLabel_tipoNota}" for="tipoNota" />
<p:selectOneMenu id="tipoNota" value="#{notaController.selected.tipoNota}" required="true" requiredMessage="#{bundle.CreateNotaRequiredMessage_tipoNota}">
<f:selectItem itemLabel="---"/>
<f:selectItem itemValue="debito" itemLabel="Débito"/>
<f:selectItem itemValue="credito" itemLabel="Crédito"/>
</p:selectOneMenu>
<h:outputLabel value="#{bundle.CreateNotaLabel_monto}" for="monto" />
<p:inputText id="monto" value="#{notaController.selected.monto}" title="#{bundle.CreateNotaTitle_monto}" required="true" requiredMessage="#{bundle.CreateNotaRequiredMessage_monto}"/>
<h:outputLabel value="#{bundle.CreateNotaLabel_factura}" for="factura" />
<p:selectOneMenu id="factura" converter="#{facturaController.convertidor}" value="#{notaController.selected.factura}" filter="true" filterMatchMode="startsWith" required="true" requiredMessage="#{bundle.CreateNotaRequiredMessage_factura}">
<f:selectItem itemLabel="---"/>
<f:selectItems value="#{facturaController.facturas}" var="factura" itemValue="#{factura}" itemLabel="#{factura.idFactura}" />
</p:selectOneMenu>
</h:panelGrid>
<br />
<h:commandLink action="#{notaController.create}" value="#{bundle.CreateNotaSaveLink}">
<f:setPropertyActionListener value="#{cargaController.crearCargaDefault(sesionMB.usuario)}"
target="#{notaController.selected.carga}"/>
</h:commandLink>
<br />
<br />
<h:commandLink action="#{notaController.prepareList}" value="#{bundle.CreateNotaShowAllLink}" immediate="true"/>
<br />
<br />
<h:link outcome="/inicio" value="#{bundle.CreateNotaIndexLink}"/>
</h:form>
</ui:define>
</ui:composition>
I had the same problem. The faces message wasn't rendering it was being lost. I tried this code and it worked.
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getFlash().setKeepMessages(true);
context.addMessage("successInfo", facesMsg);
I hope it works.

JSF datatable, tomahawk radio

I'm attempting to use a t:radio in my rich:datatable but it's always complaining about the fact that it can't find the component. I did some googling and I have to declare the full name, but I actually think I'm doing that.
Anyone can point me what I'm doing wrong? The code (I'm not going to give a minified version because I'm guessing it's going wrong with the compositions):
letterDetailTemplate.xhtml:
<h:form id="generateLetterForm">
<ui:include src="addStandardLetterMain.xhtml" />
<ui:include src="addStandardLetterText.xhtml" />
<ui:include src="buttons.xhtml" />
</h:form>
addStandardLetterText.xhtml (the radio button is over here, in the slrDataTable):
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:e="http://minfin.regondes2/entity"
xmlns:ccffdecorate="http://java.sun.com/jsf/composite/ccffdecorate"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:l="http://minfin.regondes2/layout"
xmlns:o="http://omnifaces.org/ui"
xmlns:t="http://myfaces.apache.org/tomahawk">
<ui:param name="entityBB" value="#{letterBB}" />
<ui:param name="type" value="Dossier.Letter" />
<l:screenzone id="addStandardLetterTextPanel"
title="#{AppMessages[type.concat('.addStandardLetterText.title')]}">
<h:outputStylesheet name="letterText.css" library="stylesheets" />
<h:outputScript name="letter.js" library="scripts" />
<a4j:region>
<a4j:outputPanel id="letterTextPanel">
<h:inputTextarea id="letterText" name="letterText"
value="#{entityBB.entity.text}" styleClass="letterText" />
<script type="text/javascript">
language: '#{screen.locale.language}'
CKEDITOR.replace( 'generateLetterForm:letterTex', {
});
</script>
</a4j:outputPanel>
<h:panelGroup styleClass="rButtonPanelAlignLeft" layout="block"
rendered="#{dossierContextBB.dossierContext == 'EDIT'}">
<a4j:commandButton
value="#{AppMessages[type.concat('.add.region')]}"
actionListener="#{entityBB.findAvailableRegionStandardLetters()}"
render="slrPopupFormPanel" limitRender="true" status="ajaxStatus"
onclick="LETTER.CKUpdate()"
oncomplete="#{rich:component('addStandardLetterRegion')}.show();" />
</h:panelGroup>
</a4j:region>
<rich:popupPanel id="addStandardLetterRegion" modal="true"
onmaskclick="#{rich:component('addStandardLetterRegion')}.hide()"
autosized="true">
<f:facet name="header">
<h:outputText
value="#{AppMessages['Dossier.Letter.StandardLetter.region.popup.title']}" />
        </f:facet>
<f:facet name="controls">
<h:outputLink value="#"
onclick="#{rich:component('addStandardLetterCentrum')}.hide(); return false;">
X
</h:outputLink>
</f:facet>
<a4j:outputPanel id="slrPopupFormPanel">
<t:selectOneRadio id="slrOption"
value="#{standardLetterBB.selected}" layout="spread"
converter="#{standardLetterBB.converter}">
<f:selectItems value="#{entityBB.availableStandardLetterText}"
var="standardLetter" itemLabel="" itemValue="#{standardLetter}" />
</t:selectOneRadio>
<rich:dataTable id="slrDataTable"
value="#{entityBB.availableStandardLetterText}"
var="standardLetter" noDataLabel="No standard letters found"
rowKeyVar="index">
<rich:column headerClass="ListTitle">
<t:radio for="generateLetterForm:slrDataTable:slrOption" index="#{index}" />
</rich:column>
<e:column entity="#{standardLetter.dossierType}" type="#{type}"
property="type" />
<e:column entity="#{standardLetter}" type="#{type}"
property="numbering" />
<e:columnTranslatable entity="#{standardLetter}" type="#{type}"
property="text" />
</rich:dataTable>
</a4j:outputPanel>
<div class="rButtonPanel">
<h:commandButton value="#{AppMessages['general.action.add']}">
<a4j:ajax event="click" execute="generateLetterForm"
listener="#{entityBB.addMotivationToReport}"
oncomplete="#{rich:component('addStandardLetterRegion')}.hide()"
render="reportMotivationTextPanel" limitRender="true" />
</h:commandButton>
<a4j:commandButton value="#{AppMessages['general.action.cancel']}"
onclick="#{rich:component('addStandardLetterRegion')}.hide()"
limitRender="true" immediate="true" bypassUpdates="true" />
</div>
</rich:popupPanel>
</l:screenzone>
Note that this view works and renders and displays the data from the backingbean when I remove the t:radio.
The errormessage I'm getting is:
SEVERE: java.lang.IllegalStateException: Could not find component 'generateLetterForm:slrDataTable:slrOption' (calling findComponent on component 'generateLetterForm:slrDataTable:0:j_idt258')
javax.faces.FacesException: java.lang.IllegalStateException: Could not find component 'generateLetterForm:slrDataTable:slrOption' (calling findComponent on component 'generateLetterForm:slrDataTable:0:j_idt258')
Change for="generateLetterForm:slrDataTable:slrOption" to for=":generateLetterForm:slrOption" and specify id for your t:radio in datatable also. Don't forget the ":" before generateLetterForm.

Init method gets called twice in #Viewscoped bean

I am using schedule component of Primefaces. I am filling it with values from database and when the user selects sth from the selectonemenu an ajax event is triggered (I tried to put just the related code, if there is sth missing pls remind me):
xhtml:
<h:outputText value="Scope :" />
<h:selectOneMenu id="scope" value="#{scheduleView.scope}">
<f:selectItems value="#{lookup.scopeCombo}"/>
<p:ajax process="scope" update="schedule, scheduleForm, scheduleFormPG" listener="#{scheduleView.changeScopeType()}"/>
</h:selectOneMenu>
<p:schedule id="schedule" value="#{scheduleView.model}" editable="true"/>
Backing bean:
#ManagedBean
#ViewScoped
public class ScheduleView implements Serializable {
#PostConstruct
public void init() {
System.out.println("Init ");
scopeChange();
}
public void scopeChange(String scope){
System.out.println("scopeChange ");
model.clear();
events = (List<Event>) commonServis.bringEverythingByCriteria(Event.class, "scope" , scope);
for(int i= 0; i<events.size();i++){
model.addEvent(new DefaultScheduleEvent(events.get(i).getAd(), events.get(i).getStartDate(),events.get(i).getEndDate()));
}
public void changeScopeType() {
System.out.println("changeScopeType ");
scopeChange (scope);
}
The output of the above code is:
Init
scopeChange
When the user changes the value in the selectonemenu:
changeScopeType
Init
scopeChange
It is supposed to go into init method just once. But after the changeScopeType function is triggered it gets into the init method and fills the schedule with unrelated data. I thought it might be related to #Postconstruct annotation but I couldn't find any related explanation. Can anyone understand the reason and offer a solution?
Here is the full page:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://prime.primefaces.org/ui"
template="templates/layout.xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:define name="title">#{labels.schedule}</ui:define>
<ui:define name="content">
<h:form id="scheduleForm">
<h:panelGrid id="scheduleFormPG">
<p:growl id="msgs" />
<p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false">
<h:graphicImage value="resources/images/ajax-loader.gif" />
</p:dialog>
<p:dialog showEffect="explode" hideEffect="explode" resizable="false"
header="warning" widgetVar="confirmationErase" appendToBody="true" modal="true">
<h:outputText value="Are you sure?"/>
<br/>
<p:commandButton value="Yes" actionListener="#{scheduleView.deleteEvent(AE)}"
update="msgs, scheduleFormPG, schedule, wrapperPanel"
onstart="statusDialog.show(),confirmationErase.hide()"
oncomplete="statusDialog.hide(), eventDialog.hide()" process="#parent, scope" />
<p:commandButton value="No" onclick="confirmationErase.hide()" type="button" />
</p:dialog>
<h:outputText value="scope :" />
<h:selectOneMenu id="scope" value="#{scheduleView.scope}">
<f:selectItems value="#{lookup.scopeTypeCombo}"/>
<p:ajax process="scope" update="schedule, scheduleForm, scheduleFormPG" listener="#{scheduleView.changeScopeType()}"/>
</h:selectOneMenu>
</h:panelGrid>
<p:schedule onDateSelectUpdate="wrapperPanel"
onEventSelectUpdate="wrapperPanel" onEventSelectComplete="eventDialog.show()" eventSelectListener="#{scheduleView.onEventSelect}"
onDateSelectComplete="eventDialog.show();" dateSelectListener="#{scheduleView.onDateSelect}" id="schedule" value="#{scheduleView.model}" editable="true"/>
<p:dialog id="dialog111" widgetVar="eventDialog" header="Event Information" showEffect="clip" hideEffect="clip">
<p:panel id="wrapperPanel">
<h:panelGrid id="eventDetails" columns="2">
<h:outputLabel for="eventName" value="Event Name *: " />
<p:inputText id="eventName" value="#{scheduleView.event.title}" required="true"/>
<h:outputLabel value="Start Date:" />
<p:calendar id="sKalender" value="#{scheduleView.event.startDate}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
<h:outputLabel value="End Date:" />
<p:calendar id="eKalender" value="#{scheduleView.event.endDate}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
<h:outputLabel value="All Day:" />
<h:selectBooleanCheckbox id="allDay" value="#{scheduleView.event.allDay}" />
<h:outputLabel value="scope: " />
<h:selectOneMenu id="scopeChoice" value="#{scheduleView.event.scope}">
<f:selectItems value="#{lookup.scopeTypeCombo}"/>
</h:selectOneMenu>
<p:commandButton onclick="confirmationErase.show()" oncomplete="eventDialog.hide()" update="wrapperPanel, msgs, schedule" type="reset" value="Delete" />
<p:commandButton value="Save" actionListener="#{scheduleView.addEvent(AE)}" process="#parent, scope" update="schedule wrapperPanel msgs scheduleForm" oncomplete="eventDialog.hide();"/>
</h:panelGrid>
</p:panel>
</p:dialog>
</h:form>
</ui:define>
</ui:composition>
The problem was this line of code:
<p:ajax process="scope" update="schedule, scheduleForm, scheduleFormPG" listener="#{scheduleView.changeScopeType()}"/>
update was refreshing the whole page, instead it should've only update schedule. When I change it to:
update="schedule"
the problem solved.
Thanks everyone for their effort.

Resources