I have a simple CRUD application written in JSF 2.2 and PrimeFaces 3.5. The database consists of two tables - Clients and Orders. I have a view with datatable where all clients are listed and in the last column are three buttons - for eidt, delete and showing the orders for each client. When edit button is clicked the data for the selected client should be loaded in a modal dialog form but no matter which button is pressed in the dialog always is loaded data for the client in the first row. Same for the delete button.
Here is the code of the view:
<h:form id="form">
<p:growl id="msgs" showDetail="true" />
<p:commandButton id="addNewClientButton" type="button"
onclick="add.show();" value="Add new client"
action="clientBeans.newClient" />
<br />
<p:dataTable var="client" value="#{clientBeans.getAllClients()}"
border="true">
<p:column headerText="Client Id">
<h:outputText value="#{client.clientId}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{client.name}" />
</p:column>
<p:column headerText="Address">
<h:outputText value="#{client.address}" />
</p:column>
<p:column headerText="Email">
<h:outputText value="#{client.email}" />
</p:column>
<p:column headerText="Options">
<p:commandButton value="Edit" id="editClientButton"
action="#{clientBeans.editClient}" type="button"
update=":form:editClient" onclick="edit.show();">
<f:setPropertyActionListener
target="#{clientBeans.client}"
value="#{client}" />
</p:commandButton>
<p:commandButton action="#{clientBeans.deleteClient}"
value="Delete"
id="deleteClientButton" type="button"
onclick="remove.show();"
icon="ui-icon-circle-close">
<f:setPropertyActionListener
target="#{clientBeans.client.clientId}"
value="#{client}" />
</p:commandButton>
<p:commandButton
action="#orderBeans.getSpecificOrders(client.clientId)}"
value="Orders">
<f:setPropertyActionListener
target="#{clientBeans.client.clientId}"
value="#{client.clientId}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<h:form>
<p:dialog id="addNewClient" header="Add new client" widgetVar="add"
modal="true" resizable="false">
<p:panelGrid columns="2" border="false">
<h:outputLabel for="name" value="Name: " />
<p:inputText id="name" value="#{clientBeans.client.name}"
label="name" required="true" />
<h:outputLabel for="address" value="Address: " />
<p:inputText id="address"
value="#{clientBeans.client.address}"
label="address" required="true" />
<h:outputLabel for="email" value="Email: " />
<p:inputText id="email" value="#{clientBeans.client.email}"
label="email" required="true" />
<f:facet name="footer">
<p:commandButton
action="#{clientBeans.createClient}" value="Save"
icon="ui-icon-check" style="margin:0">
</p:commandButton>
</f:facet>
</p:panelGrid>
</p:dialog>
</h:form>
<h:form>
<p:dialog id="editClient" header="Edit client" widgetVar="edit"
modal="true" resizable="false">
<h:panelGrid columns="2" id="displayClient">
<h:outputLabel for="id" value="Id: " />
<h:outputText id="id" value="#{client.clientId}" label="id" />
<h:outputLabel for="name" value="Name: " />
<p:inputText id="name" value="#{client.name}" label="name"
required="true" />
<h:outputLabel for="address" value="Address: " />
<p:inputText id="address" value="#{client.address}" label="address"
required="true" />
<h:outputLabel for="email" value="Email: " />
<p:inputText id="email" value="#{client.email}" label="email"
required="true" />
<f:facet name="footer">
<p:commandButton
action="#{clientBeans.updateClient}" value="Save"
icon="ui-icon-check" style="margin:0">
</p:commandButton>
</f:facet>
</h:panelGrid>
</p:dialog>
</h:form>
<h:form>
<p:dialog id="deleteClient"
header="Are you sure you want to delete this client?"
widgetVar="remove" modal="true" resizable="false">
<p:panelGrid columns="2">
<h:outputLabel for="id" value="Id: " />
<h:outputText id="id" value="#{client.clientId}" label="id" />
<h:outputLabel for="name" value="Name: " />
<h:outputText id="name" value="#{client.name}" label="name" />
<h:outputLabel for="address" value="Address: " />
<h:outputText id="address" value="#{client.address}" label="address" />
<h:outputLabel for="email" value="Email: " />
<h:outputText id="email" value="#{client.email}" label="email" />
<f:facet name="footer">
<p:commandButton action="#{clientBeans.confirmDeleteClient}"
value="Delete" icon="ui-icon-circle-close" />
</f:facet>
</p:panelGrid>
</p:dialog>
</h:form>
I haven't gone through all the code, but I could quickly spot that you have misused <p:commandButton>: the attribute type="button" should only be used for invoking custom JavaScript (client) code, it is therefore not compatible with action and actionListener, that are suitable for invoking server actions. Remove this attribute and ensure that the managed beans's method #{clientBeans.editClient} is invoked.
Look at PrimeFaces documentation, commandButtons with type="button" are called "Push Buttons".
Second, use oncomplete instead of onclick, in order to show the dialog after the ajax method has completed its job.
Related
I have a primefaces dialog and what I want to do is process inputtext before commandbutton action starts. Inside myController.sendToPostBox method, myController.rejectionReason string returns null. Here is my view code.When I remove process attribute, commandbutton doesn't works.
<h:form>
....
<p:dialog id="myPanel"
widgetVar="myPanelId"
resizable="true"
appendToBody="true"
draggable="true"
height="200"
width="300">
<p:panelGrid id="myPanelGridId" style="width: 250px;" styleClass="panelGridWithoutBorder">
<p:row>
<p:column colspan="2">
<p:inputTextarea style="width: 250px;" value="#{myController.rejectionReason}"/>
</p:column>
</p:row>
<p:row>
<p:column>
<p:commandButton value="Save"
oncomplete="if (!args.validationFailed) myPanel.hide();"
process="myPanelId"
action="#{myController.sendToPostBox()}"/>
</p:column>
<p:column>
<p:commandButton value="Close" />
</p:column>
</p:row>
</p:panelGrid>
</p:dialog>
</h:form>
Just place the <h:form> inside the dialog (instead dialog inside the <h:form>)
Explanation:
When you use appendToBody="true" you dialog is being transferred outside of the form that wraps it to the body of the generated html , and since there is no form inside the dialog you can't really submit it.
Also take a look at this thread: Proper Construct for Primefaces Dialog
You need something like this:
<h:form id="formDialog">
<p:commandButton id="basic" value="Basic Dialog" onclick="PF('dlg1').show();" type="button" />
</h:form>
<p:dialog id="basicDialog" header="Basic Dialog" widgetVar="dlg1">
<h:form id="formUser">
<h:panelGrid id="grid" columns="2">
<h:outputLabel value="Username:" />
<p:inputText id="user" value="#{user.name}" />
<h:outputLabel value="Password:" />
<p:inputText id="password" value="#{user.password}" />
<h:commandButton value="Login" id="btn" process="#formUser" actionListener="#{user.login}" />
</h:panelGrid>
</h:form>
</p:dialog>
"login" in the actionListener tag is a method in the User(bean) class, that needs the #ManagedBean annotations.
I have a jsf page having a popup button that opens a following dialog popup:
</h:form>
<p:dialog id="dialog" header="Select different user"
widgetVar="dlg" appendToBody="true">
<h:form id="form23">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="username" value="Username:" />
<p:inputText value="#{loginBean.username}"
id="username" required="true" label="username" />
<h:outputLabel for="password" value="Password:" />
<h:inputSecret value="#{loginBean.password}"
id="password" required="true" label="password" />
<f:facet name="footer">
<p:commandButton id="loginButton" value="Login" />
</f:facet>
</h:panelGrid>
</h:form>
</h:form>
But instead of opening as popup it is getting displayed outside the page itself. the dialog is outside the main form. Help!!
I tried that but still the same problem persists..below is my modified code..
<h:form id="create-ticket">
<p:dialog id="dialog" header="Select different user" widgetVar="dlg" appendToBody="true">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="username" value="Username:" />
<p:inputText value="#{loginBean.username}"
id="username" required="true" label="username" />
<h:outputLabel for="password" value="Password:" />
<h:inputSecret value="#{loginBean.password}"
id="password" required="true" label="password" />
<f:facet name="footer">
<p:commandButton id="loginButton" value="Login"
/>
</f:facet>
</h:panelGrid>
</p:dialog>
<h:panelGroup>
<h:outputLabel value="#{I18N['Create_Ticket_for_other_users']}" styleClass="atf-header"></h:outputLabel>
</h:panelGroup>
<h:panelGroup id="search_section" layout="block"
styleClass="atf-content-area atf-separarot-botton"> <!-- This is the section where we get the grey background -->
<h:panelGroup id="input_search_section" styleClass="">
<h:outputText id="name" value="Siddharth Mishra"
labelStyleClass="atf-label">
</h:outputText>
</h:panelGroup>
<h:panelGroup styleClass="atf-right atf-inline-block">
<p:commandButton id="btn_search" value="select different user"
styleClass="atf-button-search" onclick="dlg.show();" type="button"
>
</p:commandButton>
</h:panelGroup>
</h:panelGroup>
Now i only have one form you can see the popup dialog section + the button which is calling the popup under h:panelgroup.Atached is the image how the form currently looks Thanks in advance
You need to restructure your code.
*Don't nest forms inside other forms.
*You dont have the end-tag of your p:dialog.
<h:form>
<p:dialog id="dialog" header="Select different user"
widgetVar="dlg" appendToBody="true">
//Content ex inputtext
</p:dialog>
<p:commandButton value="Show Dialog" onclick="dlg.show();" type="button" />
</h:form>
I am using Primefaces3.5 with Jsf2.1.9 i am getting a weired issue with
<p:commandButton>
as well as
<p:commandLink>
I have to press button as well as link more than one then JSF bean method is calling otherwise on Single click nothing Happening as i read some Stackoverflow Question this Issue relates to JSF but even using Primfaces3.5 with their Components i am getting this issue.Any one know How can i resolve this issue.
My JSF page
<ui:composition xmlns:jsp="http://java.sun.com/JSP/Page"
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:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
template="./../portfolio/PortfolioViewTemplate.xhtml">
<ui:define name="moduleContent">
<p:panel id="panel" header="Portfolio Transaction">
<p:messages id="msgs" />
<p:panelGrid id="addMoreTrans" columns="2" style="border-style:none;">
<f:facet name="header">
Add Trades
</f:facet>
<p:dataTable var="moreTrans"
value="#{portfolioTransactionBean.listOfTrasPanel}" style="border:none;" >
<p:column style="border:none;">
<p:panelGrid columns="7" id="transactionPanel1"
style="display: inline-block;border:none;">
<h:outputLabel for="tipsSuggestionBoxId" value="Company Name:*"></h:outputLabel>
<h:outputText value="Date:*" />
<h:outputText value="Type:*" />
<h:outputText value="Quantity:*" />
<h:outputText value="Price:*" />
<h:outputText value="Brokerage:" />
<h:outputText value="Notes:" />
<p:autoComplete id="tipsSuggestionBoxId"
completeMethod="#{applicationScopeBean.autoSelectCompany}"
value="#{portfolioTransactionBean.txnCurrentRecord.companyName}"
minChars="3" nothingLabel="No similar company found"
requestDelay="1" minQueryLength="3" required="true"
requiredMessage="Company Name Cannot be empty">
</p:autoComplete>
<p:calendar id="from_date1" size="10" required="true"
requiredMessage="Date Cannot be empty"
value="#{portfolioTransactionBean.txnCurrentRecord.umptTransDate}"
mode="popup" showOn="both" pattern="dd/MM/yyyy"
popupIconOnly="true" readonly="#{facesContext.renderResponse}"
navigator="true" showButtonPanel="true">
</p:calendar>
<p:selectOneMenu id="tranType"
value="#{portfolioTransactionBean.txnCurrentRecord.umptTransType}">
<f:selectItem itemLabel="Transaction Type" itemValue="" />
<f:selectItem itemLabel="Buy" itemValue="Buy" />
<f:selectItem itemLabel="Sell" itemValue="Sell" />
<f:selectItem itemLabel="Bonus" itemValue="Bonus" />
<f:selectItem itemLabel="Split" itemValue="Split" />
</p:selectOneMenu>
<p:spinner id="transQuntity1" required="true"
value="#{portfolioTransactionBean.txnCurrentRecord.umptQty}"
min="1" label="Quanity" size="5"
validatorMessage="Field Is mandatory" />
<p:spinner id="transPrice1" required="true" size="5"
value="#{portfolioTransactionBean.txnCurrentRecord.umptPrice}"
label="Price" validatorMessage="Field Is mandatory" />
<p:spinner id="brokerage1" size="5"
value="#{portfolioTransactionBean.txnCurrentRecord.umptBrokerage}" />
<h:inputText
value="#{portfolioTransactionBean.txnCurrentRecord.umptNotes}" />
</p:panelGrid>
<p:separator id="customSeparator" style="width:100%;height:10px" />
</p:column>
</p:dataTable>
<f:facet name="footer">
<p:commandButton value="Clear"
actionListener="#{portfolioTransactionBean.clearRecord}"
process="#this"
rendered="#{portfolioTransactionBean.onClickofButton}">
</p:commandButton>
<p:spacer width="100" height="10" />
<p:commandButton value="Save Transaction " icon="ui-icon-check"
action="#{portfolioTransactionBean.savePortfolioTransaction}"
style="float:right;right:20%;"
rendered="#{portfolioTransactionBean.onClickofButton}">
</p:commandButton>
<p:commandButton value="Add More Transaction " icon="ui-icon-check"
action="#{portfolioTransactionBean.addMoreTransPanel}"
process="#this" update="addMoreTrans"
rendered="#{portfolioTransactionBean.onClickofButton}" />
</f:facet>
</p:panelGrid>
<p:panelGrid columns="2" id="allTransactionPanel">
<p:dataTable value="#{portfolioTransactionBean.transactionsList}"
var="tradeRec">
<f:facet name="Header">
<h:outputText>Recently Added Trades</h:outputText>
</f:facet>
<p:column headerText="Company">
<h:outputText value="#{tradeRec.companyName}"></h:outputText>
</p:column>
<p:column headerText="Date">
<h:outputText value="#{tradeRec.umptTransDate}">
<f:convertDateTime dateStyle="medium"></f:convertDateTime>
</h:outputText>
</p:column>
<p:column headerText="Type">
<h:outputText value="#{tradeRec.umptTransType}"></h:outputText>
</p:column>
<p:column headerText="Quantity">
<h:outputText value="#{tradeRec.umptQty}"></h:outputText>
</p:column>
<p:column headerText="Price">
<h:outputText value="#{tradeRec.umptPrice}">
<f:convertNumber maxFractionDigits="2"></f:convertNumber>
</h:outputText>
</p:column>
<p:column headerText="Brokerage">
<h:outputText value="#{tradeRec.umptBrokerage}">
<f:convertNumber maxFractionDigits="2"></f:convertNumber>
</h:outputText>
</p:column>
</p:dataTable>
</p:panelGrid>
</p:panel>
</ui:define>
</ui:composition>
Template File...
<ui:composition xmlns:jsp="http://java.sun.com/JSP/Page"
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:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
template="./../templates/MasterTemplate.xhtml">
<ui:define name="moduleDetails">
<h:form name="portfolioMenuForm">
<f:event type="javax.faces.event.PreRenderViewEvent" listener="#{applicationScopeBean.preRenderView}"/>
<table style="width: 100%; min-height: 500px; border: none">
<tr>
<td>
<p:menu>
<p:submenu label="Portfolio Views">
<p:menuitem value="Add Transactions" action="PortfolioTransactionMgmt" update="#form" immediate="true" />
<p:menuitem value="View Transactions" action="PortFolioTranView" update="#form" immediate="true" />
<p:menuitem value="Latest Holdings" action="PortfolioHolding" update="#form" immediate="true" />
<p:menuitem value="Sector Allocations" action="#{portfolioTransactionBean.showSecAllocation}" update="#form" immediate="true" />
<p:menuitem value="Realized Profit" action="PortfolioRelizeProfit" update="#form" immediate="true" />
<p:menuitem value="Portfolio Performance" action="#{portfolioTransactionBean.showPortPerformance}" update="#form" immediate="true" />
<p:menuitem value="Benchmark Comparison" action="#{portfolioTransactionBean.showBenhComparison}" update="#form" immediate="true" />
</p:submenu>
</p:menu>
</td>
<td>
<ui:insert name="moduleContent"></ui:insert>
</td>
</tr>
</table>
</h:form>
</ui:define>
</ui:composition>
Basically this is not weired issue
On first click jsf first looks whether there is (javax.faces.ViewState) state available or not and if it is not available then it creates one for you & when you click second time because of Jsf state available it makes call to bean method.
Solution : try creating jsf state through the javascript function
Thanks to Çağatay Çivici Project lead of primefaces .He suggest me
Try with 2.1.17 then, showcase is 2.1.17.
So i updated my pom for jsf and it work like a charm.
Thanks
I have the following code that allows a user enter some data:
<h:panelGrid columns="2" cellspacing="5">
<h:outputLabel value="#{msg.FrequencyOfSpending}" />
<h:selectOneMenu id="ruleFrequencyOptions" value="#{Rule.ruleControls.ControlOne.controlParams.Period.valueSelected}" styleClass="commonSelect">
<f:selectItems value="#{Rule.ruleControls.ControlOne.controlParams.Period.validValues}" itemLabelEscaped="true" />
<f:ajax event="valueChange" listener="#{Rule.ruleControls.ControlOne.controlParams.Period.valueSelectedChange}" onerror="handleAjaxError" render="rulesGroup" />
</h:selectOneMenu>
</h:panelGrid>
<h:panelGroup id="rulesGroup">
<a4j:repeat value="#{Rule.ruleParams.Action.properties}" var="RuleParamProperty" id="budgetRuleIterator">
<h:panelGrid columns="4" cellspacing="5" columnClasses="ruleParamCheckbox, ruleParamAction, ruleParamActionFrequency, ruleParamActionInput">
<h:selectBooleanCheckbox value="#{RuleParamProperty.selected}" immediate="true">
<a4j:ajax event="click" listener="#{RuleParamProperty.selectedChange}" onerror="handleAjaxError" />
</h:selectBooleanCheckbox>
<h:outputText value="#{msg[RuleParamProperty.name]}" />
<h:panelGrid columns="3">
<h:outputText value="#{msg.Action_1}" />
<h:outputText value="#{msg[Rule.ruleControls.ControlOne.controlParams.Period.valueSelected]}" class="italic-text" />
<h:outputText value="#{msg.Action_3}" />
</h:panelGrid>
<h:inputText value="#{RuleParamProperty.inputValue}" />
</h:panelGrid>
</a4j:repeat>
</h:panelGroup>
<!--******* Link here to generate row with exact same format as all code above ***-->
<h:panelGrid columns="2">
<img id="AddIcon" src="#{facesContext.externalContext.requestContextPath}/images/icons/add.png" alt="#{msg.addControl}" />
<h:link value="#{msg.addControl}" />
</h:panelGrid>
I need to add a link to allow them add different variations of this data i.e. user clicks add new control link and a new row appears that allows them enter more data (of the exact same format). They should be able to add multiple rows of the same data.
What is the best way to approach this with JSF2 and Richfaces4?
Should I put my panelGroups within a table?
Thanks
I used a datatable as suggested by Luiggi:
<h:dataTable value="#{rule}" var="currentRuleItem">
<h:column>
<h:panelGrid columns="2" cellspacing="5">
<h:outputLabel value="#{msg.FrequencyOfSpending}" />
<h:selectOneMenu id="ruleFrequencyOptions" value="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelected}" styleClass="commonSelect">
<f:selectItems value="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.validValues}" itemLabelEscaped="true" />
<f:ajax event="valueChange" listener="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelectedChange}" onerror="handleAjaxError" render="rulesGroup" />
</h:selectOneMenu>
</h:panelGrid>
<h:panelGroup id="rulesGroup">
<a4j:repeat value="#{currentRuleItem.ruleParams.Action.properties}" var="RuleParamProperty" id="budgetRuleIterator">
<h:panelGrid columns="4" cellspacing="5" columnClasses="ruleParamCheckbox, ruleParamAction, ruleParamActionFrequency, ruleParamActionInput">
<h:selectBooleanCheckbox value="#{RuleParamProperty.selected}" immediate="true">
<a4j:ajax event="click" listener="#{RuleParamProperty.selectedChange}" onerror="handleAjaxError" />
</h:selectBooleanCheckbox>
<h:outputText value="#{msg[RuleParamProperty.name]}" />
<h:panelGrid columns="3">
<h:outputText value="#{msg.Action_1}" />
<h:outputText value="#{msg[currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelected]}" class="italic-text" />
<h:outputText value="#{msg.Action_3}" />
</h:panelGrid>
<h:inputText value="#{RuleParamProperty.inputValue}" />
</h:panelGrid>
</a4j:repeat>
</h:panelGroup>
</h:column>
</h:dataTable>
<!--******* Link here to generate row with exact same format as all code above ***-->
<h:panelGrid columns="2">
<img id="AddIcon" src="#{facesContext.externalContext.requestContextPath}/images/icons/add.png" alt="#{msg.addControl}" />
<h:commandLink value="#{msg.addControl}" action="#{myBean.addRule}" />
</h:panelGrid>
// my bean method
public void addRule()
{
iRuleSet.get("RuleControl1").add(createRule());
}
I have a form with 4 fields (2 dates, 2 selectOne).
My Bean is SessionScoped.
When I change the values in the form once, and click on submit, the applications works fine.
But when I want to change the values in form again, nothing happens. no change nor execution.
My Form looks like:
<ui:define name="sidebar">
<p:panel header="Select Values">
<h:form id="graphform">
<p:growl id="msg" />
<p:panelGrid columns="1">
<h:outputLabel for="servervalue" value="Servervalue: " />
<p:selectOneMenu value="#{graph.servervalue }" id="servervalue">
<f:selectItems value="#{formconst.SERVERVALUES}" />
</p:selectOneMenu>
<h:outputLabel for="startdate" value="Startdate: " />
<p:calendar disabledWeekends="false" size="10" pattern="dd.MM.yyyy"
showOtherMonths="true" maxlength="10"
mindate="#{formconst.MINDATE }" maxdate="#{formconst.MAXDATE}"
showButtonPanel="true" navigator="true" readOnlyInputText="true"
id="startdate" value="#{graph.startdate }" showOn="button"
required="true" />
<h:outputLabel for="enddate" value="Enddate: " />
<p:calendar disabledWeekends="false" size="10" pattern="dd.MM.yyyy"
showOtherMonths="true" maxlength="10"
mindate="#{formconst.MINDATE }" maxdate="#{formconst.MAXDATE }"
showButtonPanel="true" navigator="true" readOnlyInputText="true"
id="enddate" value="#{graph.enddate }" showOn="button"
required="true" />
<h:outputLabel for="filter" value="Filter: " />
<p:selectOneMenu value="#{graph.filter }" id="filter">
<f:selectItems value="#{formconst.GENERALFILTER}" />
</p:selectOneMenu>
</p:panelGrid>
<p:separator />
<p:panelGrid columns="2">
<p:commandButton value="Submit" update="#all"
action="#{ graph.updateChart }" />
<p:commandButton value="Reset" update="#all"
action="#{ graph.resetChart }" />
</p:panelGrid>
</h:form>
</p:panel>
</ui:define>
<ui:define name="content">
<h:form id="linechart">
<p:lineChart value="#{graph.cartesianChartBean.chartmodel }"
enhancedLegend="true" legendPosition="nw" xaxisAngle="50" />
</h:form>
</ui:define>
The same happens when I change SessionScoped to ViewScoped or RequestScoped.
I can change the values only one time.
I use primefaces 3.2 with apache myFaces 2.0.2 on WebSphere Application Server 8.0
Best Regards Veote