我正在尝试创建一个复合组件,以便在我的Seam应用程序中使用,并且我遇到了最简单的"hello,world"组件的问题.
我在{jboss deploy} /application.ear/application.war/resources/greet中放置了一个名为hello.xhtml的文件:
<!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:composite="http://java.sun.com/jsf/composite">
<head>
<title>My First Composite Component</title>
</head>
<body>
<composite:interface>
<composite:attribute name="who"/>
</composite:interface>
<composite:implementation>
<h:outputText value="Hello, #{cc.attrs.who}!"/>
</composite:implementation>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
现在在home.xhtml中,位于我的webapp的根目录({jboss deploy} /application.ear/application.war/home.xhtml):
<!DOCTYPE html 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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:g="http://java.sun.com/jsf/composite/greet"
xmlns:s="http://jboss.com/products/seam/taglib"
template="layout/template.xhtml">
<ui:define name="content">
<div id="content">
<g:hello who="World"/>
<br/>
</div>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
但是我的"你好,世界"没有显示,我也没有收到任何错误消息,即使我打开com.sun和javax.faces类别的调试级别日志记录.
我已经验证资源目录在适当的位置,因为添加目录resources/images/test.jpg,然后将其添加到home.xhtml:
h:graphicImage value="#{resource['images:test.jpg']}"/>
Run Code Online (Sandbox Code Playgroud)
显示图像的结果.我只是不知道为什么JSF没有从greet目录中获取我的xhtml文件.
有任何想法吗?
我有一个复合组件,其接口包含:
<cc:attribute name="model"
shortDescription="Bean that contains Location" >
<cc:attribute name="location" type="pkg.Location"
required="true" />
</cc:attribute>
</cc:interface>
Run Code Online (Sandbox Code Playgroud)
所以我可以使用#{cc.attrs.model.location}访问标记中的Location对象.
我也从复合组件的支持bean访问该对象,如下所示:
FacesContext fc = FacesContext.getCurrentInstance();
Object obj = fc.getApplication().evaluateExpressionGet(fc,
"#{cc.attrs.model.location}", Location.class);
Run Code Online (Sandbox Code Playgroud)
所以现在我的复合组件已经完成了它的工作 - 如何从支持bean调用模型上的setter方法?(即model.setLocation(someValue)?
我正在学习使用JSF 2.0的复合组件,我希望我的组件能够从支持bean中触发方法,所以我创建了一个简单的例子,但是出了点问题.
这是我创建的组件:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface>
<composite:attribute name="attribute1"/>
<composite:attribute name="attribute2"/>
<composite:attribute name="actionBtnText"/>
<composite:attribute name="actionMethod" method-signature="java.lang.String action()"/>
</composite:interface>
<composite:implementation>
<h:form>
<h:inputText value="#{cc.attrs.attribute1}"/>
<br/>
<h:inputText value="#{cc.attrs.attribute2}"/>
<br/>
<h:commandButton action="#{cc.attrs.actionMethod}" value="#{cc.attrs.actionBtnText}"/>
</h:form>
</composite:implementation>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我在JSF页面中使用它的方式
<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:custom="http://java.sun.com/jsf/composite/custom">
...
<h:body>
<custom:demoCustomComponent attribute1="#{demoBB.value1 }" attribute2="#{demoBB.value2 }" actionBtnText="Button text!" actionBtn="#{demoBB.act}"/>
</h:body>
Run Code Online (Sandbox Code Playgroud)
这是支持组件所在页面的支持bean
@Named("demoBB")
@RequestScoped
public class DemoBB {
private String value1;
private String value2;
public String getValue1() {
return value1;
}
public String act() {
System.out.println("Input 1: " + value1 …Run Code Online (Sandbox Code Playgroud) 我创建一个这样的复合组件:
<cc:interface>
<cc:attribute name="value"
required="true" />
<cc:attribute name="rendered"
displayName="True to render"
default="true" />
</cc:interface>
Run Code Online (Sandbox Code Playgroud)
当我调用这个组件时,我得到一个IllegalArgumentException.我可以将呈现的名称更改为其他名称(如doIt)然后它可以工作.
渲染的属性是以某种方式保留的吗?我希望我的复合组件看起来像"常规"JSF组件.
这是Mojarra.
我正在开发一个JSF2/Primefaces应用程序,我在访问此组件的支持bean中复合组件的接口中定义的属性时遇到问题.
我的组件定义如下:
<!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:composite="http://java.sun.com/jsf/composite">
<composite:interface componentType="testComponent">
<composite:attribute name="text" required="true" type="java.lang.String" />
</composite:interface>
<composite:implementation>
<h:outputText value="Passed text is: #{cc.attrs.text}" />
</composite:implementation>
</html>
Run Code Online (Sandbox Code Playgroud)
它存储在名为text.xhtml:application/src/main/webapp/resources/my_componentdirectory 的文件中.
我在另一个页面(这是一个Facelets组合元素)上使用此组件,如下所示:
<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:myc="http://java.sun.com/jsf/composite/my_component"
template="./resources/templates/template.xhtml">
<ui:define name="content">
<h:form id="products">
<myc:test id="test" text="A text" />
</h:form>
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
支持组件类定义如下:
package my.application.component;
import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@FacesComponent ( value="testComponent" )
public class TestComponent extends UINamingContainer {
Logger log = …Run Code Online (Sandbox Code Playgroud) 我面临一个问题"在JSF中不允许使用空id属性",而下面提到的复合组件用于一组按钮(按钮的计数可以是1到3)(我在Tomcat-7上使用Mojarra 2-0-8) .
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface>
<composite:attribute name="buttonCount" />
<composite:attribute name="button1Id" />
<composite:attribute name="button1Style" />
<composite:attribute name="button1Action" />
<composite:attribute name="button2Id" />
<composite:attribute name="button2Style" />
<composite:attribute name="button2Action" />
<composite:attribute name="button3Id" />
<composite:attribute name="button3Style" />
<composite:attribute name="button3Action" />
</composite:interface>
<composite:implementation>
<h:commandButton rendered = "#{cc.attrs.buttonCount ge '1'}" id="#{cc.attrs.button1Id}" styleClass="#{cc.attrs.button1Style}">
<f:ajax listener="#{cc.attrs.button1Action}" immediate="true"/>
</h:commandButton>
<h:panelGroup rendered = "#{cc.attrs.buttonCount ge '2'}">
<h:commandButton id="#{cc.attrs.button2Id}" styleClass="#{cc.attrs.button2Style}">
<f:ajax listener="#{cc.attrs.button2Action}" immediate="true"/>
</h:commandButton>
</h:panelGroup>
<h:panelGroup …Run Code Online (Sandbox Code Playgroud) 我必须将复合组件迁移到自定义组件.此示例相当简化,但演示了问题:我的component(my:test)的子节点需要在另一个组件中呈现.my:testC作为一个我不想使用的例子,复合材料看起来像这样
<composite:implementation>
<p:panel>
<composite:insertChildren/>
</p:panel>
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)
显然,(我希望至少我这个假设是正确的),我不能简单地呈现p:panel在encodeBegin.
@FacesComponent("test")
public class Test extends UIPanel
{
@Override
public void encodeBegin(FacesContext context) throws IOException
{
// ??
}
@Override
public void encodeEnd(FacesContext context) throws IOException
{
// ??
}
}
Run Code Online (Sandbox Code Playgroud)
我希望以my:test这样的方式使用:
<my:test>
<h:outputText value="some Text"/>
</my:test>
Run Code Online (Sandbox Code Playgroud)
输出应该与使用相同my:testC:在PrimeFaces面板中呈现的一些文本.如何编码p:panelJava类的用法?
我正在阅读http://docs.oracle.com/javaee/7/tutorial/doc/jsf-facelets005.htm#GIQZR上的Java EE 7教程
在我的IDE中的8.5复合组件一章中键入示例代码并在GlassFish4.0上运行示例后,我收到错误
java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.putIfAbsent(ConcurrentHashMap.java:1078)
at com.sun.faces.util.Cache.get(Cache.java:116)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.getComponentMetadata(FaceletViewHandlingStrategy.java:237)
at com.sun.faces.application.ApplicationImpl.createComponent(ApplicationImpl.java:951)
at javax.faces.application.ApplicationWrapper.createComponent(ApplicationWrapper.java:648)
Run Code Online (Sandbox Code Playgroud)
然后我查看本教程的旧版本,我发现了一个区别.
在Java EE 7版本中,email.xhtml代码如下:
<!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:composite="http://xmlns.jcp.org/jsf/composite"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>This content will not be displayed</title>
</h:head>
<h:body>
<composite:interface>
<composite:attribute name="value" required="false"/>
</composite:interface>
<composite:implementation>
<h:outputLabel value="Email id: "></h:outputLabel>
<h:inputText value="#{cc.attrs.value}"></h:inputText>
</composite:implementation>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
但是在Java EE 6版本中
<!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:composite="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>This content will not be displayed</title>
</h:head> …Run Code Online (Sandbox Code Playgroud) 我有一个带有MethodExpression属性的UI组件changeListener:
<composite:interface>
<composite:attribute name="changeListener" required="false" method-signature="void actionListener(javax.faces.event.ActionEvent)" />
..
</composite:interface>
<composite:implementation>
<p:remoteCommand name="ajaxOnChange"
update="#{cc.attrs.onChangeUpdate}"
oncomplete="#{cc.attrs.onchange}"
actionListener="#{cc.attrs.changeListener}" />
..
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)
此changeListener属性是一个可选的方法表达式,在其中用作actionListenerremoteCommand,我想在<p:remoteCommand>仅changeListener设置属性的情况下呈现该属性.
我已经尝试了几种方法来检查属性是否设置,尤其是:
<c:if test="#{! empty cc.attrs.changeListener}">
Run Code Online (Sandbox Code Playgroud)
和
<p:remoteCommand rendered="#{cc.attrs.changeListener != null}" />
Run Code Online (Sandbox Code Playgroud)
但我得到一个javax.el.PropertyNotFoundException,因为它试图将属性评估为属性.
如何评估是否设置了可选方法属性?
谢谢
我试图通过嵌套一个孩子来使两个复合组件很好地协同工作.该设置包含一个灯箱和一个带有名为"Value"的属性的输入.这工作正常,直到我引入动态数量的输入,因此必须使用ui:repeat.
bugTest.xhtml
<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:pw="http://java.sun.com/jsf/composite/components">
<h:head></h:head>
<h:body>
<pw:lightBox value="Header">
<h:form>
<ui:repeat var="input" value="#{BugTestBean.inputs}">
<pw:bugTestInput value="#{input}" />
</ui:repeat>
</h:form>
</pw:lightBox>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
ui:repeat似乎混淆了两个组件的value属性,并发生以下异常.
Caused by: javax.el.PropertyNotFoundException: /resources/components/bugTestInput.xhtml @15,62 value="#{cc.attrs.value.text}": The class 'java.lang.String' does not have the property 'text'.
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIOutput.getValue(UIOutput.java:170)
at javax.faces.component.UIInput.getValue(UIInput.java:284)
at com.sun.faces.facelets.component.UIRepeat$SavedState.populate(UIRepeat.java:879)
at com.sun.faces.facelets.component.UIRepeat.saveChildState(UIRepeat.java:396)
at com.sun.faces.facelets.component.UIRepeat.saveChildState(UIRepeat.java:402)
at com.sun.faces.facelets.component.UIRepeat.saveChildState(UIRepeat.java:402)
at com.sun.faces.facelets.component.UIRepeat.saveChildState(UIRepeat.java:356)
at com.sun.faces.facelets.component.UIRepeat.setIndex(UIRepeat.java:470)
at com.sun.faces.facelets.component.UIRepeat.process(UIRepeat.java:586)
at com.sun.faces.facelets.component.UIRepeat.encodeChildren(UIRepeat.java:1042)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1819)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:847)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:847)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
at …Run Code Online (Sandbox Code Playgroud)