问题在于,我始终将null
作为listofthings
的值,但我将5
作为test
的值,这是正确的。
另外,我尝试初始化Bean
构造函数中的事物列表而不是init()
方法。仍然没有任何变化。
以下是我的复合材料组件的代码:
mycomponent.xhtml
<ui:component
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:p="http://primefaces.org/ui"
xmlns:composite="http://java.sun.com/jsf/composite"
>
<composite:interface componentType="myNewComponent">
<composite:attribute name="listofthings" required="true" type="java.util.List"/>
<composite:attribute name="test" type="java.lang.Integer"/>
</composite:interface>
<composite:implementation>
<h:outputStylesheet library="custom" name="mycomponent/resources/mycomp.css" />
<ui:include src="mycomponent/componentImpl.xhtml" />
<h:outputScript library="custom" name="mycomponent/resources/mycomp.js" />
</composite:implementation>
</ui:component>
MyNewComponent.java
package com.abc.components;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
@FacesComponent("myNewComponent")
public class MyNewComponent extends UINamingContainer {
private List<Things> listOfThings;
@SuppressWarnings("unchecked")
@Override
public void encodeBegin(FacesContext context) throws IOException {
Map<String, Object> attrs = getAttributes();
Object obj = attrs.get("listofthings");
//I get obj as null and attrs.get("test") correctly gives 5
if (attrs.get("listofthings") instanceof List<?>) {
listOfThings = (List<Things>) attrs.get("listofthings");
} else {
listOfThings = new ArrayList<>();
}
super.encodeBegin(context);
}
}
用法xhtml:
<my:mycomponent listofthings="#{bean.things}" test="5"></my:mycomponent>
用法bean:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class Bean implements Serializable {
private static final long serialVersionUID = 1L;
private List<Thing> things;
@PostConstruct
public void init() {
things = new ArrayList<>();
things.add(new Thing("Name", "type"));
things.add(new Thing("Name", "type"));
things.add(new Thing("Name", "type"));
}
}
不确定我在这里缺少什么,非常感谢任何帮助。