How to reference value declared in cc:attribute in cc:implementation

蓝咒 提交于 2019-12-31 04:30:09

问题


I have a simple JSF 2.0 composite component example.

<!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:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<head>
    <title>
        A panel box component
    </title>
</head>
<body>
    <cc:interface>
        <cc:attribute name="model" required="true" type="at.test.Person"/>
    </cc:interface>
    <cc:implementation>
        <h:inputText value="#{model.vorname}">

        </h:inputText>
    </cc:implementation>
</body>

And here is my JSF test page:

 <!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:mc="http://java.sun.com/jsf/composite/mygourmet" >

<h:body>
<h:form>
    <mc:inputTest model="#{person}">

    </mc:inputTest>
    <h:commandButton value=""/>
    <h:outputText value="#{person.vorname}"/>
</h:form>
 </h:body>

</html>

I want that my composite component saves a string value in a JSF session bean with <h:inputText>. But the problem is, when I submit the form with the <h:commandButton> I see the following error:

Caused by: javax.el.PropertyNotFoundException: /resources/mygourmet/inputTest.xhtml      at line 18 and column 42 value="#{model.vorname}": Target Unreachable, identifier 'model' resolved to null
    at org.apache.myfaces.view.facelets.el.TagValueExpression.getType(TagValueExpression.java:73)
    at org.apache.myfaces.shared_impl.renderkit._SharedRendererUtils.findUIOutputConverter(_SharedRendererUtils.java:77)

回答1:


You need to reference composite component attribute values by #{cc.attrs.<name>} where <name> is the attribute name. So, this should do:

<h:inputText value="#{cc.attrs.model.vorname}">

See also:

  • Java EE 6 tutorial - Facelets - Composite Components
  • Java EE 6 tutorial - Advanced Composite Components
  • <composite:xxx> tag documentation

Unrelated to the concrete problem, all that <html><head><body> in the composite is unnecessary. I suggest to use <ui:component> since that's more clear. See also our composite component wiki page for examples.



来源:https://stackoverflow.com/questions/7986868/how-to-reference-value-declared-in-ccattribute-in-ccimplementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!