Using temporary value without backing bean [duplicate] - jsf

This question already has answers here:
Pass input text value to bean method without binding input value to bean property
(2 answers)
Closed 6 years ago.
Inside a primefaces overlayPanel I am asking a user for a value, which will be reused directly afterwards. So I don't want to save it in a backing bean.
Here's the code:
<p:overlayPanel id="ovlpanel" for="copy" hideEffect="fade" style="width:300px">
<h:form>
<p:panel id="grid" header="#{msg.copy_item}">
<h:panelGrid columns="2" cellpadding="5"
styleClass="ui-panelgrid-blank">
<p:outputLabel value="#{msg.number_of_copies}: " for="number_of_copies" />
<p:column>
<p:inputText id="number_of_copies"
value="tempValNumberOfCopies" />
</p:column>
</h:panelGrid>
<p:commandButton
action="#{controller.copyItem(tempValNumberOfCopies)}"
value="#{msg.copy}" />
</p:panel>
</h:form>
</p:overlayPanel>
I want to save the value for tempValNumberOfCopies inside a temporary variable and use it in the action of the button.
Do you maybe have any ideas how to implement that?

You could use the implicit scopes, e.g.
<p:inputText id="number_of_copies"
value="requestScope.tempValNumberOfCopies" />
<p:commandButton action="#{controller.copyItem(requestScope.tempValNumberOfCopies)}"
value="#{msg.copy}" />
or
<p:inputText id="number_of_copies"
value="viewScope.tempValNumberOfCopies" />
<p:commandButton action="#{controller.copyItem(viewScope.tempValNumberOfCopies)}"
value="#{msg.copy}" />

Related

Why does a primefaces view keep a value when I´m setting it as new? [duplicate]

This question already has an answer here:
Dialog not updating after populating model in action method
(1 answer)
Closed 6 years ago.
I´m just trying to get some object when I do double click over a data table row and show it on a modal dialog. I should be missing something because it does it correctly only one time. After the bean object is setted, it keeps the value no matter what I do.
Here is the code:
*.xhtml:
<!-- Modal dialog -->
<p:dialog widgetVar="addNotDlg" id="idAddNotDlb" minHeight="40" modal="true"
resizable="false" draggable="false" style="heigth:500">
<p:panel id="panel">
<p:messages id="messages" />
<h:panelGrid columns="3" cellpadding="5">
<p:outputLabel for="resultado" value="Resultado:" />
<p:inputText id="resultado"
value="#{acuerdoBean.notificacion.resultado}" label="resultado" />
<p:message for="resultado" />
<p:outputLabel for="motivo" value="Motivo:" />
<p:inputText id="motivo"
value="#{acuerdoBean.notificacion.motivo}" label="motivo" />
<p:message for="motivo" />
</h:panelGrid>
<p:commandButton value="Guardar" id="addNewNotButton" actionListener="#{acuerdoBean.addNotificacion()}"
update=":formNotificacion:notificacionesTable"
oncomplete="handleLoginRequest(xhr, status, args)" />
<p:commandButton value="Cerrar" id="closeNewNotButton" onclick="PF('addNotDlg').hide();" />
<h:outputText value="" />
</p:panel>
</p:dialog>
*Bean.java code:
public void generateNewNotificacion() {
this.tiposNotificaciones = pservice.getTiposNotificaciones();
this.tipoNotificacion = new Rstiponotifica();
this.direccionPorDefecto = (Rsdireccion) pservice.findDireccionPorDefecto();
this.notificacion = new Rsnotificacion();
this.notificacion.setRsacuerdo(acuerdo);
this.notificacion.setRstiponotifica(tipoNotificacion);
this.notificacion.setRsdireccion(direccionPorDefecto);
}
The above function is executed in other .xhtml file event. I would expect that my "this.notificacion" object should appear as new in the modal, but it doesn´t. The "this.notification" object was setted previously and it remains in memory.
<p:commandLink onclick="PF('addNotDlg').show();" actionListener="#{acuerdoBean.generateNewNotificacion()}">
<h:outputText value="Añadir nueva notificacion" />
</p:commandLink>
Thanks in advance.
Updating your panel inside the dialog when you click the command link should fix your problem. The commandLink have an update attribute that you can use.
You can see a working example on primefaces website : http://www.primefaces.org/showcase/ui/data/schedule.xhtml

Apart from fileUploadListener,how to call a method from bean on onComplete attribute of p:fileUpload [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 7 years ago.
I want to execute a method from backing bean on onComplete attribute of p:fileUpload because I am processing some inputText values on upload action and I want those values to be added to the file in another method on the same upload click, here I don't want to use another button action.
So I am using p:remoteCommand to call the method from bean using the above javascript code. Here I'm calling javascript function on onComplete of p:fileUpload and the script calling the p:remoteCommand which in turn calling the insertProperty() method.But the insertProperty() is not getting called. How is this caused and how can I solve it? Thanks in advance.
The javascript is
<script type="text/javascript">
function addProperties(){
lazyload();
}
</script>
</h:head>
My code is
`
<h:body>
<h:form id="mainformId"
style="background: #A9CEEA !important;margin-top:5px !important;">
<div>
<h:form id="uploadformId">
<p:messages id="msg"/>
<p:remoteCommand name="lazyload" process="#this"
actionListener="#{hubDocsBean.insertProperty}" >
</p:remoteCommand>
<p:panelGrid style="width:100%;">
<p:row>
<p:column colspan="4">
<p:fileUpload fileUploadListener="#{hubDocsBean.fileUpload}"
dragDropSupport="false"
allowTypes="/(\.|\/)(txt|doc|docx|xls|xlsx|pdf)$/"
update=":mainformId:tableformId:docTableId, msg" multiple="false"
process="IagencyId,ImarketId,IvendorId,IstationId" mode="advanced" sizeLimit="52428800" oncomplete="addProperties();" />
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText value="Agency" />
<p:spacer width="5"></p:spacer>
<p:inputText value="#{hubDocsBean.inputagency}" id="IagencyId" />
</p:column>
<p:column>
<h:outputText value="Market" />
<p:spacer width="5"></p:spacer>
<p:inputText value="#{hubDocsBean.inputmarket}" id="ImarketId" />
</p:column>
<p:column>
<h:outputText value="Vendor" />
<p:spacer width="5"></p:spacer>
<p:inputText value="#{hubDocsBean.inputvendor}" id="IvendorId" />
</p:column>
<p:column>
<h:outputText value="Station" />
<p:spacer width="5"></p:spacer>
<p:inputText value="#{hubDocsBean.inputstation}" id="IstationId" />
</p:column>
</p:row>`
By the way, you don't need to have the extra Javascript. You can just use:
oncomplete="lazyload()"
instead of
oncomplete="addProperties();"

p:ajax not updating p:inputText

I am unable to update a <p:inputText> based on the value entered in another <p:inputText> using <p:ajax>. I am using JSF 2 with PrimeFaces 5. The second <p:inputText> is not updating, the listener associated with <p:ajax> is being called and i am getting the correct values in the listener but its not updated on the view.
the view code is:
<p:dialog id="newStdDlg" header="Add new Student" widgetVar="newStdDlg" modal="true">
<h:panelGrid id="newStdDlgPanel" columns="2" cellpadding="5" style="width:100%;">
<p:outputLabel value="First Name *" />
<p:inputText id="studentfname" value="#{addStudentBean.student.firstName}">
<p:ajax event="change" update="studentUsrname" listener="#{addStudentBean.firstNameChange}" />
</p:inputText>
<p:outputLabel value="Last Name *" />
<p:inputText value="#{addStudentBean.student.lastName}"/>
<p:outputLabel value="Father's Name *" />
<p:inputText value="#{addStudentBean.student.fatherName}"/>
<p:outputLabel id="uLbl" value="Username (System Generated) *" />
<p:inputText id="studentUsrname" value="#{addStudentBean.student.user.username}" />
<p:outputLabel value="This temporary password would be mailed to user: " />
<p:outputLabel id="stdpassword" value="#{addStudentBean.student.user.password}"/>
</h:panelGrid>
<p:commandButton value="Create Student"
actionListener="#{addStudentBean.addNewStudentAction}"
style="margin-left:auto;margin-right:auto;display:block;"/>
</p:dialog>
and the listener of the session scoped managed bean is :
public void firstNameChange() {
System.out.println("In AddStudentBean().firstNameChange()..........");
System.out.println("The value of student.getFirstName: "+student.getFirstName());
System.out.println("updating system generated username as: "+student.getFirstName()+String.valueOf(new UserDAO().getUserCount()+1));
student.getUser().setUsername(student.getFirstName()+String.valueOf(new UserDAO().getUserCount()+1));
student.getUser().setPassword(KaaloUtils.getPassword());
}
Like Jaqen mentioned in comments the comments use h:form inside dialog.
If you want to update the component from ManagedBean you can do that by using org.primefaces.RequestContext's update method.
RequestContext.getCurrentInstance().update("COMPONENT_ID_TO_UPDATE")
If you feel like this method is too Cohesive, you can update from Facelet only, make sure remember to not place p:dialog in a h:from instead use h:form inside p:dialog .

How to make an element conditionally required in JSF [duplicate]

This question already has an answer here:
Skip required validation and invoke the application
(1 answer)
Closed 8 years ago.
I have a simple input text element in my form.
The user has 2 possible actions to take on that form: 1) approve the form and 2) reject the form. So there are 2 buttons at the bottom of the form for the user to click on.
If the user clicks on approve the comment inputField should be optional. If the user clicks on reject the comment inputField should be required.
Here is a simple representation of the form:
<h:form>
<p:inputText id="comment" required="???" .../>
<p:commandButton id="approve" ... />
<p:commandButton id="reject" ... />
</h:form>
Try this.
<h:form id="forma">
<p:inputText id="comment"
value="#{userView.firstname}"
title="comment"
required="#{not empty param[reject.clientId]}"/>
<p:message for="comment"/>
<p:commandButton id="approve"
binding="#{approve}"
value="approve"
process="#form"
update="#form"/>
<p:commandButton id="reject"
binding="#{reject}"
value="reject"
process="#form"
update="#form"/>
</h:form>
Your question looks similar to Dynamic required validation for different buttons?.
Modify the required attribute as follows:
<h:form id="form">
<p:inputText id="comment" required="#{not empty param['form:reject']}" ... />
<p:commandButton id="approve" ... />
<p:commandButton id="reject" ... />
</h:form>

primefaces dialog with inputtext as parameter

I would like to give the content of an inputText as a parameter to a function in a backing bean, I know that I could send the content in the backing bean, but it doesn't make much sense in my object model. I know that in the datatable, you can provide a function with the name of the "var", I thought I could use the same mechanism by passing the ID of the inputText, but it always sends null. Is there a way to do it ?
<p:dialog id="dialog" header="Reason for obsolescence" modal="true" widgetVar="dlg">
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="reason" value="Reason for obsolescence :" />
<p:inputText value="default" id="reason" required="true" label="reason" />
<p:commandButton id="provideReason" value="Ok" update=":form:tabView:table-metadata"
action="#{tableMetadataController.setSelectedObsolete(reason)}"
oncomplete="dlg.hide()"/>
</h:panelGrid>
</h:form>
</p:dialog>
Thanks

Resources