复合组件不在样式属性中打印#{cc.attrs.xxx}

Composite component does not print #{cc.attrs.xxx} in style attribute

我正在观察这个组件片段:

<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
  xmlns:cc="http://java.sun.com/jsf/composite"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core">
  <cc:interface>
    <cc:attribute name="width" default="300"/>
    <cc:attribute name="height" default="400"/>
  </cc:interface>
  <cc:implementation>
    <h:outputStylesheet library="css" name="styles.css"/>
    <h:form prependId="false">
      <div id="links" style="width: #{cc.attrs.width}px; height: #{cc.attrs.height}px">              
        </ul>
      </div>          
    </h:form>
  </cc:implementation>
</html>

p.s.the代码修改

...which shows 如何动态设置 css 值;我尝试了 css 的方式:

style="width: #{cc.attrs.width}px; height: #{cc.attrs.height}px"

编辑

我尝试将该组件(在同一个项目中)用作一个简单的标签,将其放置到 "WEB-INF/mytestcomponent" :

<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
   <namespace>http://testcomponent.com/jsf/facelets</namespace>
   <tag>
    <tag-name>testComponent</tag-name>
      <source>testComponent.xhtml</source>      
   </tag>
</facelet-taglib>

p.s.according 至 this 示例

然后我将组件称为:

<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"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:test="http://testcomponent.com/jsf/facelets"
      >

<body>
...
<test:testComponent></test:testComponent>
...
</body>
</html>

但似乎不起作用:(生成的代码给出空值

"width: ; height:"

所以我的问题是导致问题的原因以及如何解决?

谢谢

这不是声明复合组件的方式。看看 SO composite-component 标签信息

这就是所说的(我已针对您的情况进行了简化):

首先在 public 网页内容中创建一个目录 resourcesWEB-INF 目录和所有常规 Facelets 文件都在该目录中)。

WebContent
 |-- WEB-INF
 |    `-- lib
 |-- resources   <---
 `-- test.xhtml

在resources目录下,专门为复合组件创建一个目录。目录名称最终作为复合组件命名空间 URI 中的额外路径。您可以自由选择名称。我们以mycomponents为例。

WebContent
 |-- WEB-INF
 |    `-- lib
 |-- resources
 |    `-- mycomponents   <---
 `-- test.xhtml

这样,此目录中的复合组件可通过以下命名空间在所有模板中使用:

<html xmlns:my="http://xmlns.jcp.org/jsf/composite/mycomponents">

前缀my可自由选择。 http://xmlns.jcp.org/jsf/composite/ 部分是强制性的。 mycomponents 部分应该与目录名称相同。

现在您需要创建一个新的 XHTML 文件并将您的 片段代码 粘贴到其中。文件名成为复合组件标签名称。您可以自由选择名称。我们称它为 mytestcomponent.xhtml.

WebContent
 |-- WEB-INF
 |    `-- lib
 |-- resources
 |    `-- mycomponents
 |         `-- mytestcomponent.xhtml   <---
 `-- test.xhtml

这样复合组件在所有模板中都可用,如下所示:

<my:mytestcomponent />

在没有 widthheight 属性的情况下调用它会使其使用默认值。您仍然可以在像这样调用 CC 时设置这些值

<my:mytestcomponent width="500" height="140"/>

备注

  • 复合组件 XML 声明标签在命名空间 http://xmlns.jcp.org/jsf/composite 下可用。在 JSF 2.2 之前,应该使用命名空间 <a href="http://java.sun.com/jsf/composite" rel="nofollow noreferrer">http://java.sun.com/jsf/composite</a>

无关

  • 您的代码片段中有一个错误,它认为您应该在那里删除一个额外的 </ul>
  • <h:form> 标签不应嵌入到您的 cc 实现代码中。您应该将 cc 视为表单自定义元素