复合组件不会在样式属性

时间:2016-06-23 17:54:55

标签: jsf jsf-2 attributes facelets composite-component

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

<?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.代码已修改

... 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>

。根据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:"

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

由于

1 个答案:

答案 0 :(得分:0)

这不是应该如何声明复合组件。只需查看SO composite-component 代码信息

即可

这就是说(我已根据你的情况对其进行了简化):

首先在公共webcontent中创建一个目录resourcesWEB-INF目录和所有常规Facelets文件也在那里。)

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

在资源目录中,专门为复合组件创建一个目录。目录名最终作为复合组件名称空间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之前,应该使用命名空间http://java.sun.com/jsf/composite

相依

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