pass a list as input parameter in jsf - jsf-2

I have a page in jsf, where the user enters all the data and clicks on submit button, and it has to be saved in the database. Please find the code i'm currently using.
<h:column>
<f:facet name="header">
<h:outputText value="Registration"/>
</f:facet>
<h:form>
<h:commandLink id = "submit" value="submit" action="#{bean.submitDate}">
<f:param name="sNum" value="#{list.sNumber}" />
<f:param name="firstName" value="#{list.fName}" />
<f:param name="lastname" value="#{list.lName}" />
...
</h:commandLink></h:form>
</h:column>
i wanted know if i can send whole data at a time or I have to send all the fields individually itself as above

Although I don't see any input fields in your code snippet:
You can send all input elements together. Just let one single <h:form> ... </h:form> wrap all your input elements and use one h:commandLink or h:commandButton inside the form to submit all data together.

Related

Primefaces dialog client validation not displaying

I have a Primefaces 5.0 modal dialog on a composite control. The dialog has a few field marked as required. When the button on the dialog is clicked I expected the client validation to fire and it seems it does as the backing bean is not called, but the messages are not displaying:
<p:dialog id="dialogCorporateShareHolder" header="Shareholder (Company)"
widgetVar="dlgCorporateShareHolder" modal="true">
<p:panelGrid columns="3" id="corporateShareHolderDetails">
<h:outputLabel for="companyName" value="Company name"/>
<p:inputText id="companyName" required="true"
value="#{cc.attrs.corporateShareHolder.companyName}"
label="Company name"/>
<p:message for="companyName"/>
<f:facet name="footer">
<p:commandButton id="submitCorporate" value="Submit"
update="corporateShareholders"
actionListener="#{cc.attrs.controller.addCorporateShareHolder}"/>
</f:facet>
</p:panelGrid>
</p:dialog>
The dialog displays, the validation gets called (I'm pretty sure), but no message is displayed.
You only need to update the p:message or a parent too, for example with
update="corporateShareholders corporateShareholderDetails"
Btw. I believe best practises are to
have the dialog(s) last in xhtml, outside of other forms
have a form inside the dialog itself

Button with Immediate="true" clears currently displayed validation messages

I have a commandButton that redisplays the page with help on a field.
This needs to work without JavaScript.
I don't want to trigger validation so I add immediate = true.
This works fine, however, any validation messages that were on the page disappear, whereas I want them to stay there.
<h:commandButton rendered="#{cc.attrs.helpId!= null}" styleClass="helpButton" value="?" type="submit" action="#{visibleHelp.toggle(cc.clientId)}" immediate="true" />
...
<h:panelGroup rendered="#{visibleHelp[cc.clientId] != null}" id="#{cc.attrs.name}_helpText" styleClass="help-text">
<h:outputText value="#{help[cc.attrs.helpId]}"/>
</h:panelGroup>
...
Is this just how it is, or am I missing something?
That's expected behavior. The faces messages are request scoped. Submitting a form creates a new request. With immediate="true" the input components not having that attribute won't be validated and hence not generate faces messages. On a synchronous (non-ajax) postback, the initial messages "disappear" because of a full page reload.
Just submit by ajax instead so that the page won't be fully reloaded.
<h:panelGroup id="helpButton">
<h:commandButton rendered="#{cc.attrs.helpId!= null}" styleClass="helpButton"
value="?" action="#{visibleHelp.toggle(cc.clientId)}" immediate="true">
<f:ajax render="helpButton helpSection" />
</h:commandButton>
</h:panelGroup>
...
<h:panelGroup id="helpSection">
<h:panelGroup rendered="#{visibleHelp[cc.clientId] != null}"
id="#{cc.attrs.name}_helpText" styleClass="help-text">
<h:outputText value="#{help[cc.attrs.helpId]}"/>
</h:panelGroup>
</h:panelGroup>
True, this involves under the covers still JavaScript, but there's no other feasible solution, or you must remove immediate="true" so that validation is re-executed for those input components.

h:selectOneMenu's f:selectItems not rendering on pop-up display

I have pop-up with h:selectOneMenu. This pop-up will be displayed on a4j:commandLink click.
h:selectOneMenu has "value" attribute is not getting called on pop-up render (I debugged it using System.out.println()).
Because of this value is not being updated on pop-up display, I am seeing last operation value, even though I want to see empty list on new operation.
Any suggestions?
Note: Another observation is, if "add" some button in pop-up which refreshes the h:selectOneMenu, then it is working fine.
My code looks something like this:
<rich:popupPanel id="CModalPanel"
autosized="true"
resizeable="false"
moveable="true" domElementAttachment="parent">
<f:facet name="header">
<h:outputText value="Select list"/>
</f:facet>
<h:form id="cListID">
<a4j:outputPanel id="panelID">
<h:panelGroup>
<h:selectOneMenu id="cListMenu"
value="#{myBean.currvalue}" >
<f:selectItems value="#{myBean.plistItems}" />
</h:selectOneMenu>
</h:panelGroup>
<h:panelGroup>
<a4j:commandLink id="refreshButton"
actionListener="#{anotherBean.addMethod}"
render=":cListID" >
</a4j:commandLink>
</h:panelGroup>
</a4j:outputPanel>
</h:form>
</rich:popupPanel>
You need to update the content of <rich:popupPanel> before opening it.
<a4j:commandLink ... render=":cListID" oncomplete="#{rich:component('CModalPanel')}.show()" />
here's a related bug https://community.jboss.org/thread/169542

p:message attached to a h:form

I have two forms in my page, in the first formulary are input fields with validation, and in the other a dataTable. The first formulary has a message area to show validation errors, and the second formulary has a message area to show problems with the retrieval of the data (this message area is updated from myController.search).:
<h:form id="form1">
<p:message id="messageForm1" for=":form1" />
<p:inputText id="text1" value="#{myBean.data}" required="true" />
....
<p:commandButton value="Search" actionListener"#{myController.search}"
update=":form2:dataTable :form2:messageForm2 :form1:messageForm1" process="text1, ..." />
</h:form>
<h:form id="form2">
<p:message id="messageForm2" for="messageForm2" />
<p:dataTable>
...
</p:dataTable>
</h:form>
The problem is that the messages in the form2 are working, but the validation messages in form1 are never shown
The description of the for attribute of the p:message tag says:
Id of the component whose messages to display.
So you can't specify the ID of the form. You have to specify the ID of a single form component. e.g. <p:message id="messageForm1" for="text1" />. But then you have to create one message element for every form component.
An alternative would be to use the p:messages tag. For examples see here: http://www.primefaces.org/showcase-labs/ui/messages.jsf

How to get an ajax event to fire from within a p:dialog

So I have been stumped most of the day today trying to call a method in my backing bean when a component changes values. Everything works correctly if my components are outside of a <p:dialog>, however when nested inside a dialog the server side never happens. Originally I was working with the <p:calendar> control, but noticed it also didn't work with a simple inputText. The following example works:
<h:panelGrid columns="3">
<h:outputText value="Start"/>
<p:inputText id="startOfRange" value="#{myBean.startRange}" valueChangeListener="#{myBean.startChanged}">
<p:ajax event="change" update="play"/>
</p:inputText>
<h:outputText id="play" style="margin-left: 10px;" value="#{myBean.startRange}"/>
</h:panelGrid>
My backing bean method looks like
public void startChanged(ValueChangeEvent event) { }
This method is invoked after a tab out of the inputText, however when wrapped like the following the method never gets invoked:
<p:dialog widgetVar="efDlg" zindex="10" appendToBody="true" header="Create Custom Date Range"
modal="true" height="375" width="450" resizable="false" closable="false" >
<h:panelGrid columns="4">
<h:outputText value="Start"/>
<p:inputText id="startOfRange" value="#{myBean.startRange}" valueChangeListener="#{myBean.startChanged}">
<p:ajax event="change" update="play"/>
</p:inputText>
<h:outputText id="play" style="margin-left: 10px;" value="#{myBean.startRange}"/>
<p:inputText id="endOfRange" value="#{myBean.endRange}" valueChangeListener="#{myBean.endChanged}"/>
</h:panelGrid>
<div class="customRangeButtons">
<span>
<p:commandButton value="Cancel" onstart="efDlg.hide();" actionListener="#{myBean.cancelCustomDateRange}" update="pGraphs"/>
</span>
<span style="margin-left: 300px;">
<p:commandButton value="Ok" type="submit" onstart="efDlg.hide();" action="#{myBean.saveCustomDateRange()}" update="pGraphs"/>
</span>
</div>
</p:dialog>
Note I am using Primefaces 2.2.1 with Mojarra 2.1.0. Any ideas as to why I can't get the startChanged method to be called when the value has changed?
Maybe I'm wrong about this (I don't have my project code at home to verify) but isn't this:
<p:ajax event="change" update="play"/>
supposed to be:
<p:ajax event="valueChange" update="play"/>
I know that most of the Ajax event values are like the JavaScript event handlers with the 'on' removed, but valueChange is a special value in JSF Ajax tags.
For the record, I sometimes pair up two Ajax child tags inside an inputText, like:
<p:ajax event="valueChange" process="#this" update="whatever"/>
<p:ajax event="keyUp" process="#this" update="whatever"/>
This will deal with each keystroke at it is entered as well as copy-and-paste and loss of focus on the parent input text.
I have also come across the same problem. I am also using a single form and no form inside the dialog. As long as I do not have the appendToBody="true" for my dialog the input values and ajax and submit happens correctly. As soon as I add that attribute with true, for better/correct rendering, the input values are not accepted.

Resources