JSF: Update and add new request params - jsf-2

I have a page with the following params:
<f:metadata>
<f:event type="preRenderView" listener="#{busquedaBean.init}" />
<f:viewParam name="k" value="#{busquedaBean.keyword}" />
<f:viewParam name="categoryId" value="#{busquedaBean.categoryId}" />
<f:viewParam name="minprice" value="#{busquedaBean.minPrice}" />
<f:viewParam name="maxprice" value="#{busquedaBean.maxPrice}" />
</f:metadata>
All of them are optional. I need to do 2 things, which I wasn't able to do so far:
1) Add new params (keeping the previous params). This is what I tried for adding price params:
<h:outputText value="Precio diario" />
<o:form includeRequestParams="true">
<h:outputText value="min: " />
<p:inputText id="min" value="#{busquedaBean.minPrice}" />
<h:outputText value="max: " />
<p:inputText id="max" value="#{busquedaBean.maxPrice}" />
<p:commandButton icon="ui-icon-search" ajax="false">
<f:param name="minprice" value="#{busquedaBean.minPrice}" />
<f:param name="maxprice" value="#{busquedaBean.maxPrice}" />
</p:commandButton>
</o:form>
This basically works but the problem is that the URL is not updated. if the params in the URL were ?minprice=10&maxprice=200 and I update those values, the url remains the same. if these params were not included in the url, they won't be added and the filter won't work.
2) Updating the value of a param:
When categoryId is not empty, I want to show a button that will clear this param. This is what I tried:
<p:button outcome="/busqueda" includeViewParams="true" >
<f:param name="categoryId=" value="" />
</p:button>
But this button makes a GET to this URL: /busqueda/?categoryId=%3D&categoryId=1
The categoryId param appears twice. How can I just update the existing param?
I'm using MyFaces 2.1.14, Tomcat 7, Primefaces 4.0, Omnifaces
Thanks!

I was looking at this in the wrong way. includeViewParams=true will make the request include all the params declared in f:metadata with the current values. if such value is null, then the parameter is not included.
So.. to add price params:
<h:form >
<h:outputText value="$ " />
<p:inputText id="min" value="#{busquedaBean.minPrice}"/>
<h:outputText value=" a $ " />
<p:inputText id="max" value="#{busquedaBean.maxPrice}"/>
<p:commandButton icon="ui-icon-search" ajax="false" action="/busqueda?faces-redirect=true&includeViewParams=true" />
</h:form>
And to remove a param:
<p:commandButton action="#{busquedaBean.removeCategory}" ajax="false" icon="ui-icon-close" />
In the backing bean:
public String removeCategory() {
categoryId = null;
return "/busqueda?faces-redirect=true&includeViewParams=true";
}

Related

pass method through ui:include to custom component

I have 3 files:
main.xhtml
<ui:include src="included.xhtml">
<ui:param name="bean" value="#{invoices}" />
<ui:param name="method" value="initExchange" />
</ui:include>
included.xhtml
<s:compositeComponent bean="#{bean}" beanMethod="#{method}"
buttonActionListener="#{bean[method]()}" />
compositeComponent.xhtml
<cc:interface>
<cc:attribute name="bean" />
<cc:attribute name="beanMethod" />
<cc:attribute name="buttonActionListener" method-signature="void f()" />
</cc:interface>
<cc:implementation>
<h:outputText value="#{cc.attrs.bean}" />
<h:outputText value="#{cc.attrs.beanMethod}" />
<p:commandButton actionListener="#{cc.attrs.buttonActionListener}" />
h:outputTexts are OK. But clicking on button gives exception:
Target Unreachable, identifier 'bean' resolved to null
How to pass the method to button's actionListener?
It works when actionListener in composite component is written as bean[method] like in included.xhtml:
<p:commandButton actionListener="#{cc.attrs.bean[cc.attrs.beanMethod]}" />
So generally passing method attributes through multiple stages should always be done by separate bean and method rather then one attribute with 'method-signature'.

Pass dataTable value of the row to new JSF using managed bean or?

How do I pass the value in my dataTable to another jsf page? I managed to pass the value by using f:param but I have no clue how to get the data in dataTable and pass the value.
<p:outputPanel id="panelSysMenuList">
<p:dataTable id="dtSysMenu" paginator="true" paginatorPosition="bottom" paginatorAlwaysVisible="true"
var="menu" styleClass="panel-list" filteredValue="#{menuController.filteredList}"
value="#{menuController.resultList}" emptyMessage="#{msg.noRecordsFound}"
rows="#{menuController.datatableMaxResult}" selection="#{menuController.selectMenu}"
widgetVar="widVarTable" rowKey="#{menu}" selectionMode="single">
<p:column sortBy="#{menu.admenuPK.parentNode}" >
#{menu.admenuPK.parentNode}
</p:column>
<p:column sortBy="#{menu.admenuPK.node}" >
#{menu.admenuPK.node}
</p:column>
<p:column sortBy="#{menu.nodeDesc}" >
#{menu.nodeDesc}
</p:column>
<p:column sortBy="#{menu.className}" >
#{menu.className}
</p:column>
<p:column sortBy="#{menu.nodeRemark}" >
#{menu.nodeRemark}
</p:column>
</p:dataTable>
</p:outputPanel>
<p:button id="btnAdd2" value="#{msg.sysMenuAdd}" icon="ui-icon-plus" styleClass="action-buttons" outcome="MenuMaint">
<f:param name="parentNode" value="#{menuController.parentNode}" />
<f:param name="node" value="#{menuController.selectMenu}" />
</p:button>
My MenuMaint
<f:metadata>
<f:viewParam name="parentNode" value="#{menuMaintController.parentNode}" />
<f:viewParam name="node" value="#{menuMaintController.node}" />
</f:metadata>
Or is that anyway that I can do?
BTW, I am using ViewScoped
Thanks in advance!
I have solved my question by adding the param at the end of my URL
MenuMaint?parentNode=parentNode&node=selected
at the managed bean of MenuMaint, I will get the param using
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("parentNode")
this enable for me to get the value in another way than f:param

Show dialog when the input data is validated <p:confirmDialog> [duplicate]

This question already has an answer here:
How to display dialog only on complete of a successful form submit
(1 answer)
Closed 6 years ago.
For my school project I have to realize a mini website where I use primefaces framework. In a form I want after pressing the Save button two things:
1 - Validate the data entered. that's why I put
<p:message for="date" /> and <p:message for="zone" />
As the values ​​are not correct, the dialog box should not be displayed.
2 - When all data is correct, and I click Save, I want to display my dialog box.
Now I can not. Can you help me? I use version 4 primefaces.
<h:form>
<p:panel id="panel" header="Create" style="margin-bottom:10px;border-color:blueviolet" >
<p:messages id="messages" />
<h:panelGrid columns="3">
<h:outputLabel for="date" value="Date : *" />
<p:calendar locale="fr" id="date" value="#{newBusinessCtrl.exercice.debut}" required="true" label="date" showButtonPanel="true"/>
<p:message for="date" />
<h:outputLabel for="zone" value="Zone Monétaire: *" />
<p:selectOneMenu id="zone" value="#{newBusinessCtrl.exercice.zoneChoice}" >
<f:selectItem itemLabel="Choice " itemValue="" />
<f:selectItems value="#{newBusinessCtrl.exercice.zones}" var="azone"
itemLabel="#{azone}" itemValue="#{azone}" >
</f:selectItems>
<p:message for="zone" />
</p:selectOneMenu>
<p:message for="zone" />
</h:panelGrid>
</p:panel>
<p:commandButton update="panel" value="Save" icon="ui-icon-check" style="color:blueviolet" onclick="choice.show()"/>
<p:confirmDialog message="Would you like to create accounts automatically ?"
header="Account creation" severity="alert"
widgetVar="choice" appendTo="#(body)">
<p:button outcome="personalizeAccount" value="Personalize" icon="ui-icon-star" />
<p:button outcome="autoAccount" value="Continue" icon="ui-icon-star" />
</p:confirmDialog>
Just show the dialog in Backing Bean like this:
Page:
<p:commandButton update="panel" value="Save"
icon="ui-icon-check"
style="color:blueviolet"
action="#{newBusinessCtrl.showDlg('choice')}"/>
Backing Bean:
public void showDlg(String dlgName){
RequestContext.getCurrentInstance().execute(dlgName+".show()");
}
Once the validation failed, the action won't execute and thus the dialog will not show.
If validation hasn't failed. PrimeFaces puts a global args object in the JavaScript scope which in turn has a boolean validationFailed property. You can check it before showing the dialog So you can use it this way:
<p:commandButton value="save" oncomplete="if (args && !args.validationFailed) saveDialog.show()"/>

Password fields are cleared when popup is closed

I have few password fields and a dropdown box. Whenever drop down value is changed (value change event), I am displaying a popup to agree to Terms&Conditions. As soon as Terms&Conditions are accepted, password fields are cleared.
XHTML file
Enter Password: <h:inputSecret value="#{myBean.password}" />
Confirm Password: <h:inputSecret value="#{myBean.confirmPassword}" />
Select an Amount: <h:selectOneMenu value="#{myBean.amount}">
<f:selectItems value="#{myBean.amountValues}" />
<f:ajax event="valueChange" render="#this">
<rich:componentControl target="pnl" operation="show" />
</f:ajax>
</h:selectOneMenu>
<rich:popupPanel id="pnl" domElementAttachment="form" height="150" width="400" moveable="false" resizeable="false">
<f:facet name="header"><h:outputText value="Do you accept our Terms&Conditions"/></f:facet>
<h:commandButton value="Yes" immediate="true" action="#{myBean.agreed}"/>
<h:commandButton value="No" immediate="true" action="#{myBean.disagreed}" />
</rich:popupPanle>
MyBean
public void agreed() {
//set some variables
}
public void disagreed() {
//set some variables
}
For some reason whole page is getting rendered, since the form is not submitted yet, the password values are not set in the bean. How can I prevent it?

How to fetch validation error messages using FacesContext?

My xhtml contains input fields, where user can input numeric or alphabet values. Using an ajax call I am validating the input value and throwing a validation error on the . But even after entering invalid characters and error message, user may still hit the 'Save' button. Once the user hits the Save button it triggers a method in the Controller. Is there a way I can check for validation errors on p:messages inside the controller using the FacesContext.getCurrentInstance().
xhtml code
<!-- Messages -->
<p:messages id="errorMessages" globalOnly="false" showDetail="true" showSummary="false" closable="true" />
<!-- Column input field-->
<p:column>
<f:facet name="header">
<h:outputText value="Input Amount" escape="false" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.amount}" escape="false">
<f:convertNumber maxFractionDigits="3" minFractionDigits="3"
maxIntegerDigits="5" />
</h:outputText>
</f:facet>
<f:facet name="input">
<p:inputText id="input" value="#{row.amount}" maxlength="10"
size="10">
<f:convertNumber maxFractionDigits="3" minFractionDigits="3"
maxIntegerDigits="5" />
<f:validator validatorId="hourlyValueValidator" for="input" />
<p:ajax event="change" partialSubmit="true" process="input"
update=":#{p:component('errorMessages')}" />
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<!-- Save Button -->
<p:commandButton id="btnSubmit" value="Save"
update=":#{p:component('tblScrollPnl')} :#{p:component('errorMessages')}"
action="#{controller.saveValues()}" ajax="true" />
Controller in ViewScope
public void saveValues() throws Exception {
FacesContext context = FacesContext.getCurrentInstance();
List<FacesMessage> errorMsgList = context.getMessageList("globalMessages");
......
......
......
}
You don't need to get messages from the bean side for validation, as you experienced, view side validation occur but doesn't block your action.
There is couple of thing wrongs in your view, to fix your problem you need to change the process attribute of your p:commandButton, ortherwise only the button is processed (by default process="#this", so the validation is skipped.
<p:commandButton id="btnSubmit" value="Save" process="#form" update=":tblScrollPnl :errorMessages" action="#{controller.saveValues()}" />
Also note that I've removed ajax="true" since it is by default and fixed major problems in update="".
You should also rearrange your validators :
<p:inputText id="input" value="#{row.amount}" maxlength="10" size="10" validator="hourlyValueValidator">
<f:convertNumber maxFractionDigits="3" minFractionDigits="3" maxIntegerDigits="5" />
</p:inputText>

Resources