composite attributes returns null in jsf custom components

a 夏天 提交于 2019-12-25 09:49:24

问题


I am implementing my custom component like below. Placed this file web-> resource folder

<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:composite="http://java.sun.com/jsf/composite"
      >
    <h:body>
        <composite:interface>
            <composite:attribute name="width" default="300"/>
            <composite:attribute name="height" default="400"/>
        </composite:interface>
        <composite:implementation>
            <h:inputText style="height: #{composite.attrs.height}px"></h:inputText>
            <span> #{composite.attrs.height}</span>
        </composite:implementation>
    </h:body>
</html>

but attrs.height return nothing.

Custom component is used like below

<my:mycustom  height="40"></my:mycustom>

What i have done mistakes here. Anyone please help me to do this.


回答1:


I have found the issue, used the namespaces as composite to get the attribute(#{composite.attrs.height}) But this is not correct it seems and used cc instead of composite and its returns correctly.

{cc.attrs.height}




回答2:


Little bit late I know but, I wanted to answer your question, because there's no valid answers here yet..

Even, you've found the null value reason, take this as answer an advice please..

What you've found: {cc.attrs.height} is right, you must reach the attributes via "cc" it does not change regarding the namespace, take it as a quick access keyword like "session", "request", etc.

Let me move on to my suggestion..

Your component definition has html and body tags. Which are generally not for component definitions. Because a component is a part of a page, it can be defined and used as below..

NOTE: You've put your file at the right place but I suggest making a folder "components" let's say in the resources folder. Then, it'll be available at any page via below namespace definition:

xmlns:components="http://java.sun.com/jsf/composite/components"

myFirstComponent.xhtml

<ui:component xmlns=""
              xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:composite="http://java.sun.com/jsf/composite" 
              xmlns:p="http://primefaces.org/ui"
              xmlns:f="http://java.sun.com/jsf/core">

    <composite:interface>
        <!-- Your attributes goes here -->
        <composite:attribute name="sampleAttributeName" default="@form"/>
    </composite:interface>

    <composite:implementation>
        <!-- Your implementation goes here -->
    </composite:implementation>
</ui:component>


来源:https://stackoverflow.com/questions/44454804/composite-attributes-returns-null-in-jsf-custom-components

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