I am getting a strange error.
I created a simple JSF composite component, that is:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<cc:interface>
<cc:attribute name="fieldId" required="true" />
<cc:attribute name="targetValue" type="java.sql.Date" required="true" />
<cc:attribute name="required" default="false" />
<cc:attribute name="disabled" default="false" />
<cc:attribute name="styleClass" />
</cc:interface>
<cc:implementation>
<p:calendar id="#{cc.attrs.fieldId}" disabled="#{cc.attrs.disabled}" required="#{cc.attrs.required}" locale="pt" navigator="true" pattern="dd/MM/yyyy" showOn="button" value="#{cc.attrs.targetValue}" styleClass="#{cc.attrs.styleClass}">
<f:convertDateTime type="date" dateStyle="short" pattern="dd/MM/yyyy"/>
<f:ajax event="blur" execute="#this" render="#this" />
</p:calendar>
</cc:implementation>
</html>
And I´m using this on my form, normally:
<p:outputLabel for="inputInicial:dataInicial" value="#{msg['entity.dataInicial']}" />
<po:inputData id="inputInicial" targetValue="#{acaoController.entity.dataInicial}" fieldId="dataInicial"/>
<p:outputLabel for="inputFinal:dataFinal" value="#{msg['entity.dataFinal']}" />
<po:inputData id="inputFinal" targetValue="#{acaoController.entity.dataFinal}" fieldId="dataFinal" />
And, it works fine when I click to add a new entity... I click on save, and it saves as expected.
When I click to edit an existing entity, and click on save button... This errors occurs:
SEVERE: Servlet.service() for servlet [facesServlet] in context with path [/ProjetoOlimpio] threw exception [javax.el.ELException: /resources/olimpio/inputData.xhtml #18,238 value="#{cc.attrs.targetValue}": Cannot convert 30/12/12 21:00 of type class java.util.Date to class java.sql.Date] with root cause
javax.el.ELException: /resources/olimpio/inputData.xhtml #18,238 value="#{cc.attrs.targetValue}": Cannot convert 30/12/12 21:00 of type class java.util.Date to class java.sql.Date
at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:139)
at javax.faces.component.UIInput.updateModel(UIInput.java:818)
at javax.faces.component.UIInput.processUpdates(UIInput.java:735)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIForm.processUpdates(UIForm.java:281)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at org.primefaces.component.layout.Layout.processUpdates(Layout.java:252)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1231)
at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
My model uses java.util.Date class of course.
import java.util.Date;
[...]
#Column(name = "data_inicial")
#Temporal(TemporalType.DATE)
private Date dataInicial;
#Column(name = "data_final")
#Temporal(TemporalType.DATE)
private Date dataFinal;
I am using Spring Data to persist!
It is hard to understand...
Why does it work on save (not on update) ?
Why does it show the selected Date correctly on the view after I click to edit the entity?
Here some info about my enviroment:
INFO: Starting Servlet Engine: Apache Tomcat/7.0.12
INFO: Initializing Mojarra 2.1.10
INFO: Running on PrimeFaces 3.4.2
INFO: Running on PrimeFaces Extensions 0.6.1
**EDIT
1 - As I said on the comments: the problem is with the composite component, if I put the p:calendar directly on the form it works. So I´ll edit the code snippet of the CC to the whole thing! :)
Notice that I have a type="java.sql.Date" That was added because I was testing. Originally it had no type attribute. I also tested with type="java.util.Date.
There is workaround without changing hibernate model.
I prefer this way because all changes are in jsf layer.
You can use binding in composite component.
Next code is example with rich:calendar (which uses java.util.Date)
...
<cc:interface componentType="CalendarComponent">
...
</cc:interface>
<cc:implementation>
...
<rich:calendar value="#{cc.attrs.value}" binding="#{cc.attrs.calendar}" />
...
</cc:implementation>
...
and in CalendarComponent:
import java.util.Date;
import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import org.richfaces.component.UICalendar;
#FacesComponent(value = "CalendarComponent")
public class CalendarComponent extends UINamingContainer {
#Override
public void processUpdates(FacesContext context) {
Object o = calendar.getValue();
if (o instanceof Date) {
Date d = (Date) o;
//this ensures type changing
calendar.setValue(new java.sql.Date(d.getTime()));
}
super.processUpdates(context);
}
private UICalendar calendar;
public UICalendar getCalendar() {
return calendar;
}
public void setCalendar(UICalendar calendar) {
this.calendar = calendar;
}
}
Related
I have a Facelet tagfile and need to render different components depending on whether the attribute is specified or not. I tried it as below,
<ui:composition 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"
xmlns:pe="http://primefaces.org/ui/extensions"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:panelGrid columns="1">
<p:outputLabel value="test1" rendered="#{empty myParam}" />
<p:outputLabel value="test2" rendered="#{not empty myParam}" />
</h:panelGrid>
</ui:composition>
Which is used as below,
<mt:myTag myParam="#{myManagedBean.someProperty}" />
However, it didn't work. It takes the evaluated value of #{myManagedBean.someProperty}. If it's empty, then it still shows test1. How can I check if the myParam attribute is actually being set or not?
Create another custom tag with a taghandler class which checks the presence of a certain attribute in the variable mapper of the current Facelet context, and sets a boolean in the Facelet scope indicating the presence of the desired attribute. Finally make use of it in your tagfile.
E.g.
<my:checkAttributePresent name="myParam" var="myParamPresent" />
<h:panelGrid columns="1">
<p:outputLabel value="test1" rendered="#{not myParamPresent}" />
<p:outputLabel value="test2" rendered="#{myParamPresent}" />
</h:panelGrid>
With this tag handler:
public class CheckAttributePresentHandler extends TagHandler {
private String name;
private String var;
public CheckAttributePresentHandler(TagConfig config) {
super(config);
name = getRequiredAttribute("name").getValue();
var = getRequiredAttribute("var").getValue();
}
#Override
public void apply(FaceletContext context, UIComponent parent) throws IOException {
context.setAttribute(var, context.getVariableMapper().resolveVariable(name) != null);
}
}
Which is registered as below in your .taglib.xml:
<tag>
<tag-name>checkAttributePresent</tag-name>
<handler-class>com.example.CheckAttributePresentHandler</handler-class>
<attribute>
<name>name</name>
<required>true</required>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<type>java.lang.String</type>
</attribute>
</tag>
I have a very simple JSF 2/Facelets page that looks like this:
<ui:repeat value="#{myBean.names}" var="_name">
<h:commandLink value="#{_name}" action="#{myBean.sayHello(_name)}">
<f:ajax execute="#this"/>
</h:commandLink>
<br/>
</ui:repeat>
The backing bean provides a java.util.List<String> with names and the action-method just prints a "hello <name>" message to standard output.
This works fine. I get a list of names in the browser and a click fires the action-method that says hello to the specified name.
The Problem arises, when I want to put this code in a composite component that does the iteration and renders the actual link via a facet:
<ui:component xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:cc="http://xmlns.jcp.org/jsf/composite">
<cc:interface>
<cc:attribute name="value" type="java.util.List" required="true" />
<cc:facet name="content" />
</cc:interface>
<cc:implementation>
<ui:repeat value="#{cc.attrs.value}" var="_name">
<cc:renderFacet name="content"/>
</ui:repeat>
</cc:implementation>
</ui:component>
I use the composite component like this:
<my:myComp value="#{bean.names}">
<f:facet name="content">
<h:commandLink value="#{_name}" action="#{bean.sayHello(_name)}">
<f:ajax execute="#this"/>
</h:commandLink>
<br/>
</f:facet>
</my:myComp>
In the browser I get a list of names that looks exactly like before. But clicking a link now renders a "hello null" message. So _name is resolved correctly in the value attribute of <h:commandLink> but not in the action attribute.
I also tried using actionListener instead of action or the listener attribute from the <f:ajax> tag with no difference.
Could anybody shade some light on this issue?
My environment:
WildFly 8.1 with
JSF 2.2.6 (Mojarra)
The issue has to do with the scope of the variable in this case _name which is evaluated once when the <ui:repeat/> is being processed. In my case, I ran your code and it produced Hello John even though their were other names in my list. To get around this, I introduced a <f:param/> that would contain the value of the _name, and modified your code as follows:
<h:form>
<my:myComp value="#{bean.names}">
<f:facet name="content">
<h:commandLink value="#{_name}" action="#{bean.sayHello()}">
<f:param name="name_" value="#{_name}"/>
<f:ajax execute="#this"/>
</h:commandLink>
<br/>
</f:facet>
</my:myComp>
</h:form>
I also modified the sayHello() method as follows for a #RequestScoped bean:
#ManagedProperty(value = "#{facesContext}")
private FacesContext facesContext;
public void setFacesContext(FacesContext facesContext) {
this.facesContext = facesContext;
}
public void sayHello() {
Map<String, String> params = facesContext.getExternalContext()
.getRequestParameterMap();
String name = params.get("name_");
System.out.println("Hello " + name);
}
You could change this to something shorter in a #ViewScoped bean to:
public void sayHello() {
Map<String, String> params = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
String name = params.get("name_");
System.out.println("Hello " + name);
}
The final result is that it prints out the names correctly.
I am facing a problem when trying to export a DataTable with some columns, both the DataTable and the Column components are composite elements, like this:
Custom dataTable XHTML (v:dataTable):
<cc:interface>
<cc:attribute name="value" required="true" />
<cc:attribute name="selectionMode" default="multiple" />
<cc:attribute name="selectionBean" />
<cc:attribute name="selectionProperty" />
<cc:facet name="header" />
</cc:interface>
<cc:implementation>
<p:dataTable id="teste" var="tableItem" value="#{cc.attrs.value}"
selection="#{cc.attrs.selectionBean[cc.attrs.selectionProperty]}"
rowKey="#{tableItem.id}" rowsPerPageTemplate="15, 30, 45"
paginator="true" rows="15"
emptyMessage="#{messages['dataTable.emptyMessage']}">
<cc:insertChildren />
<f:facet name="footer">
<p:commandButton value="#{messages['dataTable.exportExcel']}"
ajax="false">
<p:dataExporter type="xls" target="teste" fileName="export" />
</p:commandButton>`enter code here`
</f:facet>
</p:dataTable>
</cc:implementation>
Custom column XHTML (v:dataColumn):
<cc:interface
componentType="com.example.VDataColumn">
<cc:attribute name="value" />
</cc:interface>
<cc:implementation>
<c:choose>
<c:when test="#{cc.childCount gt 0}">
<cc:insertChildren />
</c:when>
<c:otherwise>
<h:outputText value="#{cc.attrs.value}" />
</c:otherwise>
</c:choose>
</cc:implementation>
The Column component is an extension of the org.primefaces.component.column.Column class:
package com.example.component;
import javax.faces.component.FacesComponent;
import javax.faces.component.NamingContainer;
import javax.faces.component.UINamingContainer;
import org.primefaces.component.column.Column;
#FacesComponent("com.example.VDataColumn")
public class VDataColumn extends Column implements NamingContainer {
#Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
}
The DataTable and Column are used as it follows:
<v:dataTable
value="#{testController.resultList}"
selectionBean="#{testController}"
selectionProperty="selectedList" selectionMode="multiple">
<p:column value="#{tableItem.id}" headerText="ID" />
<v:dataColumn value="#{tableItem.code}" headerText="Code" />
<v:dataColumn value="#{tableItem.nome}" headerText="Name" />
<v:dataColumn value="#{tableItem.desc}" headerText="Desc" />
</v:dataTable>
When I try to export the dataTable with the dataExporter that is inside the component, I get just one column in the XLS file, and it is only the p:column.
Debugging the primefaces DataExporter class, i noticed that the DataTable object contains 4 objects in the getChildren() method, one Column and 3 VDataColumn's, and only the Column object contains children itself.
Did anybody have the same issue? I am using Primefaces 4.0
I had same problem with a custom component. We resolve by extending the exporter and using custom validation for the component.
if (component instanceof UINamingContainer) {
UINamingContainer uiNamingContainer = (UINamingContainer) component;
UIComponent panel = uiNamingContainer.getFacet(UIComponent.COMPOSITE_FACET_NAME);
Collection<UIComponent> children = panel.getChildren();
for (UIComponent uiComponent : children) {
if (uiComponent instanceof javax.faces.component.html.HtmlOutputText) {
try {
uiNamingContainer.encodeBegin(context);
return super.exportValue(context, uiComponent);
} catch (IOException e) {
LOGGER.error(e);
} finally {
try {
uiNamingContainer.encodeEnd(context);
} catch (IOException e) {
LOGGER.error(e);
}
}
}
}
}
I am having this problem in this sample jsf project I created. Managed beans do not get instantiated.
Bean class:
#ManagedBean(name="loginMB")
#RequestScoped
public class LoginMB extends AbstractMB {
private static final long serialVersionUID = -8523135776442886000L;
#ManagedProperty("#{userMB}")
private UserMB userMB;
//getters and setters
public String login() {
UserSupport userSupport = new UserSupportImpl();
User user = userSupport.isValidLogin(email, password);
if (user != null) {
getUserMB().setUser(user);
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context
.getExternalContext().getRequest();
request.getSession().setAttribute("user", user);
return "loggedIn";
//return "/pages/protected/index.xhtml";
}
displayErrorMessageToUser("Check your email/password");
return null;
}
}
ManagedBean annotation and RequestScope annotation have been imported from
import javax.faces.bean.*;
this is how i ve used above bean,
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<p>#{bundle.loginHello}</p>
<h:form>
<p:growl showDetail="false" life="3000" />
<h:panelGrid columns="2">
<h:outputLabel value="#{bundle.loginUserName}" />
<h:inputText value="#{loginMB.email}" label="Email" id="email" required="true">
<f:validateLength minimum="6" />
</h:inputText>
<h:outputLabel value="#{bundle.loginPassword}" />
<h:inputSecret value="#{loginMB.password}" label="Password" id="senha" required="true" autocomplete="off" >
<f:validateLength minimum="6" />
</h:inputSecret>
</h:panelGrid>
<p:commandButton action="#{loginMB.login}" value="Log in" ajax="false" />
</h:form>
</h:body>
</html>
Other managed bean
#SessionScoped
#ManagedBean(name = "userMB")
public class UserMB implements Serializable {
public static final String INJECTION_NAME = "#{userMB}";
private static final long serialVersionUID = 1L;
private User user;
.......
}
Exception:
javax.el.PropertyNotFoundException: /login.xhtml #14,83 value="#{loginMB.email}": Target Unreachable, identifier 'loginMB' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
faces-config.xml
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
<application>
<resource-bundle>
<base-name>messages</base-name>
<var>bundle</var>
</resource-bundle>
<message-bundle>messages</message-bundle>
</application>
<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-action>#{loginMB.login}</from-action>
<from-outcome>loggedIn</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<!-- <managed-bean>
<managed-bean-name>loginMB</managed-bean-name>
<managed-bean-class> com.sample.jsfjpa.beans.LoginMB</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>userMB</managed-bean-name>
<managed-bean-class> com.sample.jsfjpa.beans.UserMB</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean> -->
</faces-config>
Managed beans are not created unless you specifically invoke one of their properties or methods. That happens for all the scopes, except the #ApplicationScoped ones having #ManagedBean(eager=true) which are specifically created when JSF context loads.
Here you're referencing #{loginMB} from the view, but not #{userMB} so there's no chance to have it created.
In order to instance your #SessionScoped managed bean you can add following code in your login page:
<f:metadata>
<f:event type="preRenderView" listener="#{userMB.initialize}" />
</f:metadata>
Which sets a listener that is executed before page rendering. You can invoke here just an empty method (to execute it JSF will construct your bean if not already created).
Starting from JSF 2.2 you can replace the preRenderView method for the new f:viewAction, which has some benefits (it's parameterless and it doesn't get invoked on postbacks ):
<f:metadata>
<f:viewAction action="#{userMB.initialize}" />
</f:metadata>
I'm trying to capture old/new value for some business validation. For that, the ValueChangeListener seemed like a good choice. It worked great on h:selectOneMenu, however it does not get called when used with a home grown Composite Component with a Backing Component. Any idea what I'm doing wrong?
One thing to add is, when removing the componentType attribute from state.xhtml, the valueChangeListener works as expected...
The component:
<!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:composite="http://java.sun.com/jsf/composite">
<composite:interface displayName="state" componentType="com.company.dept.system.ui.address.State" shortDescription="State Information Display/Input Component">
<composite:attribute name="value" type="java.lang.String" required="true" shortDescription="The value of the component" />
<composite:editableValueHolder name="state" />
</composite:interface>
<composite:implementation>
<div id="#{cc.clientId}">
<h:selectOneMenu id="state" value="#{cc.attrs.value}">
<f:selectItem itemLabel="(select)" noSelectionOption="true"/>
<f:selectItems var="item" itemLabel="#{item.displayValue}" value="#{cc.states}" />
</h:selectOneMenu>
</div>
</composite:implementation>
</html>
The backing component
#FacesComponent("com.company.dept.system.ui.address.State")
public class State extends UIInput implements NamingContainer {
private List<com.company.dept.policy.enums.State> states;
#Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
/**
* Prepare the list of states to display
*/
public List<com.company.dept.policy.enums.State> getStates(){
if (states != null) {
return states;
}
states = new ArrayList<com.company.dept.policy.enums.State>();
for (com.company.dept.policy.enums.State st : com.company.dept.policy.enums.State.values()) {
if(!st.equals(com.company.dept.policy.enums.State.NWNORWAY) && !st.equals(com.company.dept.policy.enums.State.UNKNOWN) && !st.equals(com.company.dept.policy.enums.State.TTTRUST_TERRITORY_AND_GUAM)) {
states.add(st);
}
}
Collections.sort(states,new StateNameComparator());
return states;
}
}
The value change listener
public class ClientValueChangeListener implements ValueChangeListener {
#Override
public void processValueChange(ValueChangeEvent event)
throws AbortProcessingException {
System.out.println("*****************************");
System.out.println("VALUE CHANGE LISTENER. OLD=" + event.getOldValue() + " - NEW=" + event.getNewValue());
System.out.println("*****************************");
}
}
consuming page:
<h:form>
<address:state value="#{testPage.state}">
<f:valueChangeListener type="com.company.dept.system.ui.clientinformation.ClientValueChangeListener" for="state"/>
</address:state>
<h:commandButton id="submitButton" value="Test" action="#{testPage.act}"/>
</h:form>
It's because your backing component extends from UIInput. The value change listener is applied to the backing component itself instead of to a child of the composite implementation.
Your concrete functional requirement isn't exactly clear, but based on the information provided so far, you can safely replace extends UIInput implements NamingContainer by extends UINamingContainer (and get rid of getFamily() override).
If you really intend to keep your backing component to extend from UIInput, then you should be delegating the submitted value, local value and value of the backing component all to the child dropdown component, but this makes design technically little to no sense.