I am goin to explain a problem that I have when I use one composite component inside a template.
Imagine one view like this, that work with a generic managed bean with view scope. I pass it to the template as a parameter.
<?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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:trkal="http://java.sun.com/jsf/composite/trkalcomponents">
<ui:composition template="/template.xhtml">
<ui:param name="maisuBean" value="#{genericBean}" />
</ui:composition>
</html>
The template is like this. Beside other components, it also use one composite component.
<?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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:trkal="http://java.sun.com/jsf/composite/trkalcomponents">
<h:head>
<title>Titulo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</h:head>
<h:body>
<h:form enctype="multipart/form-data">
...
<trkal:toolbarbuttonwidget id="buttonToolbar" label="Action" iconName="toolbar.png"
buttonAction="#{maisuBean.myActionListener}"
>
</trkal:toolbarbuttonwidget>
...
<h:commandLink id="otherButton" actionListener="#{maisuBean.myActionListener}">
<h:graphicImage library="images" name="toolbar.png" />
<h:outputText value="Other Action" />
</h:commandLink>
...
</h:form>
</h:body>
</html>
As you can see, this template use one composite components that allow specify the action listener that hear this event.
<composite:interface>
<composite:attribute name="id" />
<composite:attribute name="buttonAction" method-signature="void myAction(javax.faces.event.ActionEvent)" targetAttributeName="actionListener"/>
<composite:attribute name="iconName" />
<composite:attribute name="label"/>
<composite:attribute name="title"/>
<composite:attribute name="styleClass"/>
</composite:interface>
<composite:implementation>
<h:outputStylesheet target="head" library="trkalcomponents" name="toolbarbuttonwidget.css" />
<h:commandLink id="buttonAction">
<h:graphicImage library="images" name="#{cc.attrs.iconName}" />
<h:outputText value="#{cc.attrs.label}" />
</h:commandLink>
</composite:implementation>
If I click in otherButton, it work fine, but if I click in buttonToolbar it don't work.
09-nov-2012 19:16:28 javax.faces.event.MethodExpressionActionListener processAction
GRAVE: Se ha recibido 'javax.el.PropertyNotFoundException' al invocar la escucha de acción '#{maisuBean.myActionListener}' para el componente 'buttonAction'
09-nov-2012 19:16:28 javax.faces.event.MethodExpressionActionListener processAction
GRAVE: javax.el.PropertyNotFoundException: /template.xhtml #20,6 buttonAction="#{maisuBean.myActionListener}": Propiedad 'myActionListener' no hallada en el tipo com.joxeja.test.ToolBarBean
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111)
It seem that it can't resolve the EL expression inside the composite component.
What is wrong? How can I use one composite componente inside one template?
I am using Mojarra 2.1.7
Thanks.
Sorry I missed this on the first read:
Your implementation isn't targeting the attributes properly. I made this mistake (as I'm pretty sure all of us do). You need to reference the attributes as -> #{cc.attrs.someAttribute} not by their name. You do this for most of the elements, but not the actionListener. If that is your code it should fix it. Your signatures are correct. You're trying to use the targetAttributeName which I'm not familiar with. My guess is you need to set the id of the component to that name (so your button would be myAction not actionListener (if I'm referencing the same example you are).
That aside, how I would do it is:
<composite:interface>
<composite:attribute name="id" />
<!--
<composite:attribute name="buttonAction" method-signature="void myAction(javax.faces.event.ActionEvent)" targetAttributeName="actionListener"/>
-->
<composite:attribute name="buttonAction" method-signature="void action(javax.faces.event.ActionEvent)"/>
<composite:attribute name="iconName" />
<composite:attribute name="label"/>
<composite:attribute name="title"/>
<composite:attribute name="styleClass"/>
</composite:interface>
<composite:implementation>
<h:outputStylesheet target="head" library="trkalcomponents" name="toolbarbuttonwidget.css" />
<!-- fix below -->
<h:commandLink id="buttonAction" actionListener=#{cc.attrs.buttonAction}>
<h:graphicImage library="images" name="#{cc.attrs.iconName}" />
<h:outputText value="#{cc.attrs.label}" />
</h:commandLink>
</composite:implementation>
I like this method because it is similar to the rest of the way pages are marked up and it seems simple. Give it a shot.
Related
I'm developing composite component in JSF. In that I can pass method names as arguments in following way.
<?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:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:p="http://primefaces.org/ui"
xmlns:composite="http://java.sun.com/jsf/composite">
<!-- INTERFACE -->
<composite:interface>
<composite:attribute name="noOfButtons" type="java.lang.Integer" required="true"/>
<composite:attribute name="actionOnly" type="java.lang.Boolean" required="true"/>
<composite:attribute name="myBean" required="true" type="com.jsf.CRUDBean"/>
<composite:attribute name="myIcons" type="java.lang.String" required="true"/>
<composite:attribute name="myAction" type="java.lang.String" required="true"/>
<composite:attribute name="myDesc" type="java.lang.String" required="true"/>
</composite:interface>
<!-- IMPLEMENTATION -->
<composite:implementation>
<c:forEach begin="0" end="#{cc.attrs.noOfmys - 1}" var="counter"
varStatus="status">
<h:commandButton id="myButton_${counter}"
action="#{cc.attrs.myBean[fn:split(cc.attrs.myAction, ',')[status.index]]}"
image="#{request.contextPath}/resources/images/#{fn:split(cc.attrs.myIcons, ',')[status.index]}"
title="#{fn:split(cc.attrs.myDesc, ',')[status.index]}"
style="padding:2px">
<p:spacer width="5px;" />
</c:forEach>
</composite:implementation>
</html>
This is working perfect.But in my case, I need to pass action in following way.
<c:forEach begin="0" end="#{cc.attrs.noOfmys - 1}" var="counter"
varStatus="status">
<h:commandButton id="myButton_${counter}"
image="#{request.contextPath}/resources/images/#{fn:split(cc.attrs.myIcons, ',')[status.index]}"
title="#{fn:split(cc.attrs.myDesc, ',')[status.index]}"
style="padding:2px">
<f:attribute name="action" value="#{cc.attrs.myBean[fn:split(cc.attrs.myAction, ',')[status.index]]}" />
</h:commandButton>
<p:spacer width="5px;" />
</c:forEach>
But this is not working in my application. Whenever I click commandButton from the page nothing happening at user level. Any help would be thankful.
I have a composite component with a dataScroller inside (tomahawk). In my xhtml I tried to use this component but I receive an error:
java.lang.IllegalArgumentException: could not find UIData referenced by attribute dataScroller#for = 'myTable'
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:ui="http://java.sun.com/jsf/facelets"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:t="http://myfaces.apache.org/tomahawk">
<composite:interface>
<composite:attribute name="for" targets="scroll"></composite:attribute>
<composite:attribute name="paginatorMaxPages" />
</composite:interface>
<h:head>
</h:head>
<h:body>
<composite:implementation>
<div class="paginacao">
<t:dataScroller id="scroll" for="#{cc.attrs.for}"
styleClass="paginador" renderFacetsIfSinglePage="false"
pageIndexVar="numeroPagina" pageCountVar="quantidadePaginas"
rowsCountVar="quantidadeRegistros" paginator="true"
paginatorMaxPages="#{cc.attrs.paginatorMaxPages}"
paginatorTableClass="paginasPaginador"
paginatorActiveColumnClass="negrito">
<f:facet name="first">#{labels['paginacao.primeira']}</f:facet>
<f:facet name="previous">#{labels['paginacao.anterior']}</f:facet>
<f:facet name="next">#{labels['paginacao.proxima']}</f:facet>
<f:facet name="last">#{labels['paginacao.ultima']}</f:facet>
</t:dataScroller>
</div>
</composite:implementation>
</h:body>
</html>
My datatable:
<h:form>
...
<t:dataTable id="myTable" var="item" value="#{mBean.lista}">
...
</t:dataTable>
...
</h:form>
<param:parametrosScroll id="dataScroller" for="myTable" paginatorMaxPages="5" />
To ensure reusability of multiple instances in the same view, composite components are inherently naming containers, like <h:form>, <h:dataTable>, etc. In your specific case, the relative client ID in for="myTable" will be searched in the context of <cc:implementation>, but there is no such component. Instead, it's outside the composite, in another naming container represented by <h:form>.
You've 2 options:
Pass an absolute client ID after having given the <h:form> a fixed ID.
for=":myForm:myTable"
Use a tagfile instead of a composite.
In the given case, I want to use a facelet with different ManagedBeans, so the regarding action-bean is given as an parameter:
<?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" >
<h:body>
<ui:include src="ratings.xhtml" >
<ui:param name="createAction" value="#{myOneCreateAction}" />
<ui:param name="ratings" value="#{context.ratings}" />
</ui:include>
</h:body>
</html>
I'm giving the create action as parameter value="#{myOneCreateAction}".
Within that facelet is a component also being used several times on other pages - so I try to refactor it in a composite component.
<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:io="http://java.sun.com/jsf/composite/inoutComponents">
<ui:composition>
<rich:dataTable id="ratingTblId"
value="#{ratings}"
var="rating">
<rich:column>
<io:removeButton
id="removeButton"
actionMethod="#{createAction.removeRating}"
immediate="true"
render=":#{rich:clientId('ratingTblId')}" />
<h:commandButton
id="removeButton2"
actionListener="#{createAction.removeRating}"
immediate="true" >
<f:ajax render="ratingTblId" />
</h:commandButton>
</rich:column>
</rich:dataTable>
</ui:composition>
</html>
See, how the method is given as actionMethod="#{createAction.removeRating}" to the component. This component itself looks like following:
<ui:composition 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:cc="http://java.sun.com/jsf/composite">
<!-- INTERFACE -->
<cc:interface>
<cc:attribute
name="actionMethod"
targets="remove"
method-signature="void f(javax.faces.event.ActionEvent)"/>
<cc:attribute name="render" required="false" />
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<h:commandButton
id="remove"
actionListener="#{cc.attrs.actionMethod}"
onclick="if (!confirm('Do you really?')) { return false; }">
<f:ajax execute="#this" render="#{cc.attrs.render}" />
</h:commandButton>
</cc:implementation>
</ui:composition>
and last but not least, the managed bean
Name("myOneCreateAction")
#Scope(ScopeType.CONVERSATION)
public class MyOneCreateAction {
...
public void removeRating(ActionEvent ev) {
// do something
}
...
}
Surprisingly, while the removeButton2 correctly jumps into the right function, the composite components version returns a
javax.faces.event.AbortProcessingException: Target Unreachable,
identifier 'createAction' resolved to null
instead. Am using Mojarra JSF 2.1.26 with Seam 2.3.1.CR1. There are no nested composite components. When replacing the composite component parameter to #{myOneCreateAction.removeRating}, it works like expected.
Has anybody seen this before? Am I blind? Any work-arounds known... ? Thanks in advance!
As a work-around I rewrote the component to give action bean and action method as two separate parameters resolving them as following
xhtml:
<io:removeButton
id="removeButton"
actionBean="#{createAction}"
actionMethod="removeRating"
immediate="true"
render=":#{rich:clientId('ratingTblId')}" />
composite component:
<ui:composition 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:cc="http://java.sun.com/jsf/composite">
<!-- INTERFACE -->
<cc:interface>
<cc:attribute name="actionBean" />
<cc:attribute name="actionMethod" />
<cc:attribute name="render" required="false" />
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<h:commandButton
id="remove"
action="#{cc.attrs.actionBean[cc.attrs.actionMethod]}"
onclick="if (!confirm('Do you really?')) { return false; }">
<f:ajax execute="#this" render="#{cc.attrs.render}" />
</h:commandButton>
</cc:implementation>
</ui:composition>
also changing the action methods signature to return String instead of void. That does not look that super sexy anymore, but works. :-/
I was having the same issue, found out that it has been fixed on version 2.2.15:
https://github.com/javaserverfaces/mojarra/issues/4271
I have this composite component that based on what is selected in a drop down(STREET, PO BOX) renders either another composite component streetAddressUpdate or postalBoxAddressUpdate. Here is the code
<?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: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"
xmlns:tad="http://java.sun.com/jsf/composite/tmr/ad"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:tmi="http://java.sun.com/jsf/composite/tmr/mi">
<!-- INTERFACE -->
<cc:interface>
<cc:attribute
name="title"
default="Postal address" />
<cc:attribute
name="postalAddress"
type="qdtmr.comp.cbui.address.fieldmodel.PostalAddress"
default="#{addressUpdate.postalAddress}"
required="true" />
</cc:interface>
<cc:implementation>
<ol class="questions">
<!-- Postal address type -->
<li><h:outputLabel for="postalAddressType">
<span class="label">#{tcum.postalAddressType}</span>
</h:outputLabel> <p:selectOneMenu
id="postalAddressType"
value="#{cc.attrs.postalAddress.postalAddressType}"
valueChangeListener="#{addressUpdate.changePostalAddress}">
<f:selectItems value="#{addressController.postalAddressTypesList}" />
<p:ajax
update=":#{cc.clientId}:postalAddressPanel, :#{cc.clientId}:poBoxPanel" />
</p:selectOneMenu></li>
<p:outputPanel id="postalAddressPanel">
<ui:fragment rendered="#{cc.attrs.postalAddress.postalAddressType == 'STREET'}">
<tad:streetAddressUpdate
streetAddress="#{cc.attrs.postalAddress.postalStreetAddress}"
title="" />
</ui:fragment>
<p:outputPanel id="poBoxPanel">
<ui:fragment rendered="#{cc.attrs.postalAddress.postalAddressType == 'POBOX'}">
<tad:postalBoxAddressUpdate
postalBoxAddress="#{cc.attrs.postalAddress.postalBoxAddress}" />
</ui:fragment>
</p:outputPanel>
</ol>
</cc:implementation>
</html>
The thing that is confusing me is that if I take out the ui:fragment tag, my values on the page make their way back to the postalStreetAddress/postalBoxAddress model beans, but with the ui:fragment in their, my values do not end up making to the postalStreetAddress/postalBoxAddress.
The reason I have the ui:fragment is because depending on the postalAdressType either the postalBoxAddressUpdate composite component will be used or the postalStreetAddressUpdate component will be used.
Any help will be greatly appreciated. I suspect its the ui:fragment
EDIT: I checked up taking out the ui:fragment and it does seem to be the culprit. I would really like to understand whats happening
How can I use id of DataTable Component (Primefaces 2.2.1) inside Composite Component in Java Server Faces 2.1 ?
Now, I have a view:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:haiq="http://java.sun.com/jsf/composite/haiqcomponents"
template="/WEB-INF/templates/login/main.xhtml">
<ui:define name="content">
<h:form prependId="false"> <!-- prependId="true" ??? -->
<p:dataTable id="leakTable" var="leak" value="#{dataExplorer.data}">
<p:column filterBy="#{leak.source}" headerText="source" footerText="source" filterMatchMode="contains" >
<f:facet name="header">
<h:outputText value="source" />
</f:facet>
<h:outputText value="#{leak.source}" />
</p:column>
<!-- Few more columns here -->
</p:dataTable>
<!-- Add : prefix before ID? -->
<haiq:exporter target=":leakTable" fileName="#{msgs.fileName}" imageLibrary="images" pageOnly="false" />
</h:form>
</ui:define>
</ui:composition>
My composite component:
<?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:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">
<!-- INTERFACE -->
<cc:interface>
<cc:attribute name="fileName" default="data" />
<cc:attribute name="target" required="true" type="java.lang.String" />
<cc:attribute name="pageOnly" default="true" type="java.lang.Boolean" />
<cc:attribute name="imageLibrary" default="images" />
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<h:commandLink>
<h:graphicImage library="#{cc.attrs.imageLibrary}" name="excel.png" />
<p:dataExporter type="xls"
target="#{cc.attrs.target}"
fileName="#{cc.attrs.filename}"
pageOnly="#{cc.attrs.pageOnly}" />
</h:commandLink>
</cc:implementation>
</html>
After view rendering, following error occurred:
javax.faces.FacesException: Cannot find component ":leakTable" in view.
at org.primefaces.component.export.DataExporter.processAction(DataExporter.java:89)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
Removing : before leakTable (in target attribute) or changing preperndId (in form) to true does not solve the problem.
How can I use datatable inside cc?
Similar problem is described here
Apparently you've another NamingContainer parent in the view. To be sure, open page in browser, rightclick and View Source and determine the generated ID of <p:dataTable id="leakTable">. Then, you should grab exactly that ID and prefix with :.
Alternatively, you can also bind the table component to the view and use UIComponent#getClientId() instead to dynamically refer the client ID.
<p:dataTable binding="#{leakTable}" ...>
with
<haiq:exporter target=":#{leakTable.clientId}" ...>
Unrelated to the concrete problem, I suggest to replace <!DOCTYPE><html> of your composite by <ui:component>, which is more natural and it also saves JSF from doing it impliticly everytime.
<ui:component
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:p="http://primefaces.prime.com.tr/ui">
See also https://stackoverflow.com/tags/composite-component/info.