updating component primefaces datepicker with pattern “MMMM yyyy” via ajax call leads to “uncaught name at position […]” exception - jsf

I have a primeface datepicker inside a modal dialog. Setting the initial date with the pattern "MMMM yyyy" (german) works fine. When i am trying to update the componenent via an ajax call im getting the javascript exception "uncaught name at positon [...]".
I am using Primefaces 7.0 and wrote a Year Month Converter by myself.
PrimeFaces locale:
PrimeFaces.locales['de'] = {
closeText: 'Schließen',
prevText: 'Zurück',
nextText: 'Weiter',
monthNames: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
monthNamesShort: ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'],
dayNames: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
dayNamesShort: ['Son', 'Mon', 'Die', 'Mit', 'Don', 'Fre', 'Sam'],
dayNamesMin: ['S', 'M', 'D', 'M ', 'D', 'F ', 'S'],
weekHeader: 'Woche',
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: '',
timeOnlyTitle: 'Nur Zeit',
timeText: 'Zeit',
hourText: 'Stunde',
minuteText: 'Minute',
secondText: 'Sekunde',
currentText: 'Aktuelles Datum',
ampm: false,
month: 'Monat',
week: 'Woche',
day: 'Tag',
allDayText: 'Ganzer Tag',
};
Converter:
/**
* The Class YearMonthConverter.
*/
#Slf4j
#SuppressWarnings("common-java:DuplicatedBlocks")
#FacesConverter(value = "yearMonthConverter")
public class YearMonthConverter implements Converter {
private static final String PATTERN = "MMMM yyyy";
#Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
String componentPattern = extractPattern(uiComponent);
String pattern = componentPattern.equals("") ? PATTERN : componentPattern;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern, Locale.GERMANY);
try {
return YearMonth.parse(s, formatter);
} catch (DateTimeParseException e) {
log.warn("conversion of date '{}' expected pattern '{}' failed with {}", s, pattern, e);
return YearMonth.now();
}
}
#Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
if (o instanceof YearMonth) {
String componentPattern = extractPattern(uiComponent);
String pattern = componentPattern.equals("") ? PATTERN : componentPattern;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern, Locale.GERMANY);
return formatter.format((YearMonth) o);
} else {
return null;
}
}
private String extractPattern(UIComponent component) {
// try to get the pattern from component
if (component instanceof Calendar) {
Calendar calendarComponent = (Calendar) component;
return calendarComponent.getPattern();
} else if (component instanceof DatePicker) {
DatePicker datepickerComponent = (DatePicker) component;
return datepickerComponent.getPattern();
}
return "";
}
}
Example xhtml-file:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<composite:interface>
</composite:interface>
<composite:implementation>
<style type="text/css">
.ui-panel .ui-panel-content {
padding: 0;
}
</style>
<!-- modaler someDialog -->
<p:dialog id="someDialog"
header="some header"
widgetVar="someDlg"
modal="true"
width="500"
height="280"
resizable="false"
closeOnEscape="true">
<p:panel id="pnlContent" styleClass="ui-noborder">
<p:panelGrid id="inputArea" columns="2">
<p:outputLabel value="Label:" style="width: 12em; display: block;"/>
<p:outputLabel value="example"
style="display: block;"/>
<p:outputLabel value="Monat:" style="width: 12em; display: block;"/>
<h:panelGroup>
<!-- Monat zurück -->
<p:commandButton
id="btnPrevMonth"
action="#{someDialogVC.previousMonth}"
style="padding: 2px"
icon="fa fa-angle-double-left"/>
<!-- Monatsanzeige -->
<p:datePicker id="dpMonat" view="month"
value="#{someDialogVC.state.selektierterMonat}"
converter="yearMonthConverter" pattern="MMMM yyyy" yearNavigator="true"
yearRange="2000:2050" inputStyle="width: 17em;" readonlyInput="true">
<p:ajax event="dateSelect" listener="#{someDialogVC.monthChanged}"/>
</p:datePicker>
<!-- Monat vor -->
<p:commandButton
id="btnNextMonth"
action="#{someDialogVC.nextMonth}"
style="padding: 2px"
icon="fa fa-angle-double-right"/>
</h:panelGroup>
</p:panelGrid>
<p:separator style="border-color: #aaaaaa; margin-bottom: 10px;"/>
<div style="display: flex; justify-content: flex-end">
<p:commandButton id="btnCreate"
value="Anlegen"
action="#{someDialogVC.createAndExit()}"
disabled="#{someDialogVC.doGetAnlegenDisabled()}"/>
</div>
</p:panel>
<p:blockUI block="pnlContent" trigger="btnCreate">
<p:graphicImage name="images/loader.gif"/>
</p:blockUI>
</p:dialog>
</composite:implementation>
</html>
setting initial date in bean:
#PostConstruct
public void init() {
data = new someDialogVO();
state = new someDialogVS();
state.setSelektierterMonat(YearMonth.now());
}
code snippets bean:
public void previousMonth() {
state.setSelektierterMonat(state.getSelektierterMonat().minusMonths(1L));
updateMonat();
}
public void nextMonth() {
state.setSelektierterMonat(state.getSelektierterMonat().plusMonths(1L));
updateMonat();
}
private void updateMonat() {
PrimeFaces.current().ajax().update("contentForm:someDialog:dpMonat");
}
After the ajax update call i am able to see, that the new Month is selected visually but after some milliseconds i am getting the described javascript exception. When using the pattern "MM.yyyy" everything works fine.

Related

How do I get “p:focus” to work properly in this simple app (its currently not “focusing” at all)

PROBLEM:
The PrimeFaces "p:focus" tag is not focusing.
-I.e., when the xhtml page is presented to the user, no input fields are in focus.
QUESTION:
Why not? How can I get "p:focus" to work properly in this simple app?
(NOTE: using Java 6, Mojarra v2.1.11, PrimeFaces v3.4.2)
index.jsp
<?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://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">
<f:view contentType="text/html">
<h:head>
<title>testcal2 - index.xhtml</title>
<meta charset="utf-8" />
</h:head>
<h:body>
<h:form id="queryForm">
<f:event type="postValidate" listener="#{testBean.validate}" />
<h:panelGroup id="msgsx">
<p:messages showSummary="true"/>
</h:panelGroup>
<p:panel id="queryPanel" header="Test p:focus..." style="width:100%;">
<p:focus context="queryPanel"/>
<h:panelGroup id="msgs" style="height:1.5em; text-align: center;display:block">
<p:message id="lastName_msg" for="lastName" style="display:none;" showSummary="false"/>
<p:message id="birthDate_msg" for="birthDate" style="display:none;" showSummary="false"/>
<p:message id="startDate_msg" for="startDate" style="display:none;" showSummary="false"/>
</h:panelGroup>
<h:panelGroup id="querypanelgroup" style="display:inline-block;">
<h:panelGroup>
<h:panelGroup style="text-align:right;vertical-align:middle;display:inline-block;width:150px">
<p:outputLabel style="margin-right: 5px;" value="Last Name:" for="lastName"/>
</h:panelGroup>
<h:panelGroup style="margin-left: 4px; vertical-align:middle;display:inline-block;width:250px;">
<p:inputText
id="lastName"
value="#{testBean.parmMap['lastName']}"
requiredMessage="last name required"
size="95"
maxlength="95"
onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(0).show();return false;" >
</p:inputText>
</h:panelGroup>
</h:panelGroup>
<br/>
<br/>
<h:panelGroup>
<h:panelGroup style="text-align:right;vertical-align:middle;display:inline-block;width:150px">
<p:outputLabel style="margin-right: 5px;" value="Birth Date:" for="birthDate"/>
</h:panelGroup>
<h:panelGroup style="margin-left: 4px; vertical-align:middle;display:inline-block;width:250px;">
<p:inputText
id="birthDate"
value="#{testBean.parmMap['birthDate']}"
requiredMessage="birth date required"
converter="dpConverter"
styleClass="datePicker"
onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(1).show();$(this).mask('99/99/9999');return false;">
<p:ajax event="change" process="#this" update="#this"/>
</p:inputText>
</h:panelGroup>
</h:panelGroup>
<br/>
<br/>
<h:panelGroup>
<h:panelGroup style="text-align:right;vertical-align:middle;display:inline-block;width:150px">
<p:outputLabel style="margin-right: 5px;" value="Start Date:" for="startDate"/>
</h:panelGroup>
<h:panelGroup style="margin-left: 4px; vertical-align:middle;display:inline-block;width:250px;">
<p:inputText
id="startDate"
value="#{testBean.parmMap['startDate']}"
requiredMessage="start date required"
converter="dpConverter"
styleClass="datePicker"
onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(2).show();$(this).mask('99/99/9999');return false;">
<!-- optional to populate another field with same value...
onchange="$('...hashSymbolHere...queryForm\\:endDate').val($(this).val());">
-->
<p:ajax event="change" process="#this" update="#this"/>
</p:inputText>
</h:panelGroup>
</h:panelGroup>
<br/>
<br/>
<p:commandButton
id="submit"
value="Submit"
oncomplete="applyDatePicker();"
type="submit"
update="#form"
process="#form"
action="#{testBean.submitQuery}"
style="width:150px;"
styleClass="button"/>
<p:commandButton
value="Reset"
update="#form"
onclick="location.reload();return true;"
process="#this"
actionListener="#{testBean.reset}"
immediate="true"
ajax="false"/>
</h:panelGroup>
</p:panel>
</h:form>
<h:outputStylesheet library="styles" name="query.css" />
<h:outputScript library="primefaces" name="/jquery/jquery.js" />
<h:outputScript library="primefaces" name="/jquery/plugins/ui/jquery-ui.custom.js" />
<h:outputScript library="primefaces" name="/jquery/plugins/inputmask/maskedinput.js" />
<h:outputScript library="js" name="index.js" target="head" />
</h:body>
</f:view>
</html>
index.js
$(document).ready(function()
{
applyDatePicker();
});
function applyDatePicker(){
$('.datePicker').datepicker(
{
showOn: 'button',
buttonText: "Choose",
showButtonPanel: true,
showOptions: {direction: 'up'},
changeYear: true,
changeMonth: true,
yearRange: "c-120:c+0"
});
}
TestBean.java
package aaa.bbb.ccc.war;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIForm;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ComponentSystemEvent;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
#Component("testBean")
#Scope("request")
public class TestBean implements Serializable
{
public TestBean()
{
parmMap = this.getParmMap();
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("parmMap", parmMap);
}
public void reset(ActionEvent event)
{
LinkedHashMap<String, Object> m = new LinkedHashMap<String, Object>();
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("parmMap");
setParmMap(m);
}
public String submitQuery()
{
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("hitlistData");
if (this.getParmMap().isEmpty())
{
return "";
}
return "/page2.xhtml?faces-redirect=true";
}
private static LinkedHashMap<String, Object> parmMap;
public LinkedHashMap<String, Object> getParmMap()
{
LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("parmMap");
if (null == map)
{
map = new LinkedHashMap<String, Object>();
}
return map;
}
public void setParmMap(LinkedHashMap<String, Object> map)
{
parmMap = map;
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("parmMap", parmMap);
}
private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
public void validate(ComponentSystemEvent e) throws ParseException
{
LinkedHashMap parmMap = this.getParmMap();
UIForm queryForm = (UIForm) e.getComponent();
FacesContext fc = FacesContext.getCurrentInstance();
UIInput lastName_c = (UIInput) queryForm.findComponent("lastName");
String lastName = (String) (lastName_c.getValue());
UIInput birthDate_c = (UIInput) queryForm.findComponent("birthDate");
String birthDate = (String) birthDate_c.getValue();
UIInput startDate_c = (UIInput) queryForm.findComponent("startDate");
String startDate = (String) startDate_c.getValue();
try
{
if (null != lastName && lastName.trim().length() > 0)
{
if (null == birthDate || birthDate.length() < 1)
{
birthDate_c.setValid(false);
fc.addMessage(birthDate_c.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, birthDate_c.getRequiredMessage(), birthDate_c.getRequiredMessage()));
}
else
{
birthDate_c.setValid(true);
}
}
else
{
birthDate_c.setValid(true);
}
if (null == startDate || startDate.trim().length() < 1)
{
startDate_c.setValid(false);
fc.addMessage(startDate_c.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, startDate_c.getRequiredMessage(), startDate_c.getRequiredMessage()));
}
else
{
startDate_c.setValid(true);
}
if (fc.getMessageList().size() > 0)
{
fc.renderResponse();
}
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
It's because you're returning false on focus here:
<p:inputText
...
onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(0).show();return false;" >
</p:inputText>
Returning false causes the event's default behaviour to be blocked.
To fix your problem, simply remove return false; from onfocus attribute of the input component.
<p:inputText
...
onfocus="$('#queryForm\\:msgs > div').hide();$('#queryForm\\:msgs > div').eq(0).show();" >
</p:inputText>

Why do I can alway get label in getAsObject() in Primefaces?

I do not why although I verify everything which is right. Anyone know?
I tried to debug:
- getAsString() return ID (that's right)
- when I submitted - getAsObject() always throw exception because the label values was always passed instead of ID values.
My code as below:
my xhtml file:
<div class="ui-grid-row" style="margin-top: 5px; margin-bottom: 5px;">
<div class="ui-grid-col-2 pdt4">
<p:outputLabel for="txtApprovalScheduler"
value="#{lang['workforce.category.parttimeManagement.approved.scheduler']}"/>
</div>
<div class="ui-grid-col-2">
<p:selectOneMenu value="#{parttimeController.selectedAgentDTO}"
id="txtApprovalScheduler"
filterMatchMode="contains" editable="true"
style="width: 86%;"
required="true"
requiredMessage="#{lang['workforce.category.parttimeManagement.approved.scheduler.missing']}">
<f:converter converterId="agentConverter"/>
<f:selectItem itemValue="" itemLabel="#{lang['wf.common.choose']}"/>
<f:selectItems value="#{parttimeController.agentDTOs}" var="agentItem1"
itemLabel="#{agentItem1.userName}"
itemValue="#{agentItem1}"/>
</p:selectOneMenu>
</div>
<div class="ui-grid-col-2 pdt4">
<p:outputLabel for="txtApprovalRegister"
value="#{lang['workforce.category.parttimeManagement.approved.register']}"/>
</div>
<div class="ui-grid-col-2">
<p:selectOneMenu value="#{parttimeController.selectedAgentDTOForRegister}"
id="txtApprovalRegister" editable="true"
filterMatchMode="contains" style="width: 86%; font-size: 12px !important;"
required="true"
requiredMessage="#{lang['workforce.category.parttimeManagement.approved.register.missing']}">
<f:converter converterId="agentConverter"/>
<f:selectItem itemValue="" itemLabel="#{lang['wf.common.choose']}"/>
<f:selectItems value="#{parttimeController.agentDTOs}" var="item1"
itemLabel="#{item1.userName}"
itemValue="#{item1}"/>
</p:selectOneMenu>
</div>
</div>
My Converter:
#FacesConverter(forClass = AgentDTO.class,value = "agentConverter")
public class AgentConverter implements Converter {
public static List<AgentDTO> listAgentDTOs;
#Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String agentId) {
if (agentId.trim().equals("")) {
return null;
} else {
try {
Long number = Long.parseLong(agentId);
for (AgentDTO a : listAgentDTOs) {
if (a.getAgentId() == number) {
return a;
}
}
} catch (NumberFormatException exception) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid data"));
}
}
return null;
}
#Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
if (value == null || value.equals("") || "-1".equals(value)) {
return "";
} else {
String result = String.valueOf(((AgentDTO) value).getAgentId());
return result;
}
}
}
My bean:
#Component
#Scope("view")
#ManagedBean(name = "parttimeController")
public class ParttimeController extends BaseController implements Serializable {
private AgentDTO selectedAgentDTO;
private AgentDTO selectedAgentDTOForRegister;
private List<AgentDTO> agentDTOs;
.... getter/setter and initiallize AgentCenvert.listAgentDTOs
My class:
public class AgentDTO{
private Long agentId;
private String userName;
....getter/setter
}
I found out the reason - I only remove editable attribute in selectOneMenu. So getAsObject() will pass a value instead of a label

Primefaces datatable filter

I've tried a few things but I could not do any work on my filter dataTable. Already follow the example of the primefaces showcase and nothing.
I have the following codes:
xhtml:
<p:dataTable id="dataTable" var="valor" value="#{beanMensagemXContato.listaContatoEmail}"
widgetVar="carsTable" emptyMessage="No cars found with given criteria" filteredValue="#{tableBean.filteredCars}">
<f:facet name="header">
</f:facet>
<p:column
style="max-width: 50px; min-width: 50px; overflow: hidden">
<f:facet name="header">
<h:outputText value="Contato" />
</f:facet>
<h:outputText value="#{valor.nomGrupoEmail}" />
</p:column>
<p:column
style="max-width: 50px; min-width: 50px; overflow: hidden">
<f:facet name="header">
<h:outputText value="Email" />
</f:facet>
<h:outputText value="#{valor.endEmail}" />
</p:column>
<p:column
style="max-width: 50px; min-width: 50px; overflow: hidden">
<f:facet name="header">
<h:outputText value="Telefone" />
</f:facet>
<h:outputText value="#{valor.numTelefone}" />
</p:column>
<p:column
style="max-width: 50px; min-width: 50px; overflow: hidden">
<f:facet name="header">
<h:outputText value="Ações" />
</f:facet>
</p:column>
</p:dataTable>
Bean:
public List<ContatoEmail> getListaContatoEmail() {
listaContatoEmail = new ArrayList<ContatoEmail>();
listaContatoEmail = consultaContatoEmail.listarContatoEmail();
return listaContatoEmail;
}
I want something that when you type a word in dataTable select the row.
Can someone pass me a simple example.
Since I already appreciate.
Debora
Ok, here is an example:
I'll take the popular example of cars.
Use Case: Dynamically update a data-table upon each keystrokes in auto-complete
My Facelet:
<!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: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://primefaces.org/ui">
<f:view>
<h:head />
<h:body>
<h:form>
<p:autoComplete var="carac" converter="carconvertor"
value="#{testBean.selectedCar}" itemLabel="#{carac.carmodel}"
itemValue="#{carac}"
completeMethod="#{testBean.complete}" process="#this"
onSelectUpdate="idGrid">
<p:ajax event="keyup" listener="#{testBean.onValueChange}"
update="idGrid"></p:ajax>
</p:autoComplete>
<p:dataTable value="#{testBean.matchingCarModels}" var="carmatch"
id="idGrid" converter="carconvertor">
<p:column headerText="Car Model">
<h:outputText value="#{carmatch.carmodel}" />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</f:view>
</html>
A Car POJO
public class Car
{
private String carmodel;
public Car(String carmodel) {
super();
this.carmodel = carmodel;
}
// Add setters and getters
}
A Car Faces Convertor
#FacesConverter(forClass=Car.class, value="carconvertor")
public class CarConverter
implements Converter {
#Override
public Object getAsObject(FacesContext arg0, UIComponent component, String stringvalue) {
Car car = new Car(stringvalue);
return car;
}
#Override
public String getAsString(FacesContext arg0, UIComponent component, Object objectvalue) {
Car car = (Car) objectvalue;
if(car == null) {
return StringUtils.EMPTY;
}
return car.getCarmodel();
}
}
And finally the backing bean
#ManagedBean(name="testBean")
#SessionScoped
public class TestBackingBean
{
/**
* The input filter
*/
private String filter = StringUtils.EMPTY;
/**
* Some test data
*/
private final List<Car> carModels = new ArrayList<Car>() {
{
add(new Car("toyota"));
add(new Car("honda"));
add(new Car("suzuki"));
add(new Car("ford"));
add(new Car("renault"));
add(new Car("subaru"));
}
};
/**
* For updating the grid.
*/
public void onValueChange(AjaxBehaviorEvent event)
{
AutoComplete ac = (AutoComplete) event.getSource();
Car input = (Car) ac.getValue();
filter = (input == null) ? StringUtils.EMPTY : input.getCarmodel();
}
/**
* For the auto complete drop down
*/
public List<Car> complete(String input)
{
filter = input;
return getMatchingCarModels();
}
/**
* get the match
*/
public List<Car> getMatchingCarModels()
{
if(StringUtils.isEmpty(filter))
return carModels;
List<Car> matches = new ArrayList<Car>();
for(Car car : carModels)
{
if(car.getCarmodel().startsWith(filter))
{
matches.add(car);
}
}
return matches;
}
/**
* The selected car
*/
private Car selectedCar;
//Add setters and getters for above member
}
HTH
You could see the solution to the same problem in stackoverflow here
As an alternative approach (using auto complete) for the search and capture the keyup event to update the data table. An example tallying to your context:
<p:autoComplete var="address"
value="#{addressBean.address}" itemLabel="#{address.personName}"
itemValue="#{address}" completeMethod="#{addressBean.complete}"
process="#this" converter="personconvertor"
onSelectUpdate="dataTable">
<p:ajax event="keyup" listener="#{addressBean.onValueChange}"
update="dataTable"></p:ajax>
</p:autoComplete>

f:setPropertyActionListener not calling method

I need some help. I'm having trouble with using f:setPropertyActionListener on JSF. The problem is that the JSF is not setting any value on the target method.
Part of the xhtml code is the following:
<h:form>
<fieldset style="width:100; margin-left: 350px; margin-top: 250px; position:absolute; border-style: none">
<p:dataGrid var="prod" value="#{productoController.arrProductosRelevantes}"
columns="5" rows="10" paginator="true">
<p:panel style="text-align:center">
<h:panelGrid columns="1" style="width:100%" columnClasses="colStyle1" rowClasses="rowStyle1" >
<p:graphicImage value="#{prod.imagenUrl}" style="width:100px; height:100px"/>
<h:outputText value="#{prod.marca}" style="width:40px"/>
<p:commandLink value="Ver" action="#{productoController.visualizarProducto()}">
<f:setPropertyActionListener value="#{prod}"
target="#{productoController.productoSeleccionado()}" />
</p:commandLink>
</h:panelGrid>
</p:panel>
</p:dataGrid>
</fieldset>
</h:form>
The backing bean is the following (the most relevant):
public Producto getProductoSeleccionado() {
return productoSeleccionado;
}
public void setProductoSeleccionado(Producto productoSeleccionado) {
if (productoSeleccionado != null) {
this.productoSeleccionado = productoSeleccionado;
arrComentarios = null;
arrProductosComplementarios = null;
arrProductosSuplementarios = null;
}
}
public String visualizarProducto() {
if (arrComentarios == null) {
obtenerComentarios();
}
if (arrProductosComplementarios == null) {
obtenerArrProductosComplementarios();
}
if (arrProductosSuplementarios == null) {
obtenerArrProductosSuplementarios();
}
return "visualizarProducto.xhtml?faces-redirect=true";
}
public ArrayList<Producto> getArrProductosRelevantes() {
return arrProductosRelevantes;
}
public void obtenerArrProductosRelevantes() {
clienteDAO = new ClienteDAO();
productoDAO = new ProductoDAO();
cliente = clienteDAO.getClientePorId(Sesion.IDCLIENTE);
arrProductosRelevantes = productoDAO.obtenerProductoRelevantesPorCliente(cliente);
}
By the way, I'm using #SessionScoped.
Any help will be appreciated, thanks.
The target attribute of f:setPropertyActionListener is supposed to be a property, not a method. That means, if you set target="#{productoController.productoSeleccionado}" it will call the setProductoSeleccionado(Producto prod) method in the productoController bean, passing as argument the value you set for the value attribute.
You are referring to a method productoSeleccionado() that may well not even exist in your code. So, just lose the parenthesis and you should be fine:
<f:setPropertyActionListener value="#{prod}"
target="#{productoController.productoSeleccionado}" />

How to send selected date of rich:calendar to my bean?

I am using richfaces calendar in my JSF 1.1 project. If I select a date, I want to send the selected date to my bean. How can I do it? I write simple code for this but it comes always null.
Here is my JSF page:
<rich:panel style="width: 15%;">
<rich:calendar cellWidth="24px" cellHeight="22px" value="#{functions.selectedDate}"
datePattern="yyyy-MM-dd" style="width:200px;">
</rich:calendar>
</rich:panel>
Here is my bean:
import java.util.Date;
public class functions {
private Date selectedDate;
public Date getSelectedDate() {
return selectedDate;
}
public void setSelectedDate(Date selectedDate) {
this.selectedDate = selectedDate;
}
}
Put it in a <h:form> and submit it by a command button/link inside the same form.
E.g.
<rich:panel style="width: 15%;">
<h:form>
<rich:calendar cellWidth="24px" cellHeight="22px" value="#{functions.selectedDate}"
datePattern="yyyy-MM-dd" style="width:200px;">
</rich:calendar>
<h:commandButton value="submit" action="#{functions.submit}" />
</h:form>
</rich:panel>
with
public void submit() {
System.out.println("Selected date is: " + selectedDate);
}

Resources