Ben*_*Ben 2 jsf myfaces composite-component jsf-2
嗨有这个奇怪的问题,我使用的Composite Component是我写的,我从之前使用的CC(componentTypebean)的支持bean获取值
我不知道如何更好地描述这个,而不仅仅是显示代码.我将尝试简要介绍并删除多余的部分:这是Composite Component定义:
<cc:interface componentType="dynamicFieldGroupList">
<cc:attribute name="coupletClass" />
<cc:attribute name="form" default="@form"/>
<cc:attribute name="list" type="java.util.List" required="true"/>
<cc:attribute name="fieldNames" type="java.util.List" required="true" />
</cc:interface>
<cc:implementation>
<h:dataTable value="#{cc.model}" var="currLine">
<h:column>
<h:outputText id="inner_control_component" value="Inner Look at currLine:#{currLine}"/>
</h:column>
</h:dataTable>
</cc:implementation>
Run Code Online (Sandbox Code Playgroud)
CC bean定义:
@FacesComponent(value = "dynamicFieldGroupList")
// To be specified in componentType attribute.
@SuppressWarnings({ "rawtypes", "unchecked" })
// We don't care about the actual model item type anyway.
public class DynamicFieldGroupList extends UIComponentBase implements
NamingContainer
{
private transient DataModel model;
@Override
public String getFamily()
{
return "javax.faces.NamingContainer"; // Important! Required for
// composite components.
}
public DataModel getModel()
{
if (model == null)
{
model = new ListDataModel(getList());
}
return model;
}
private List<Map<String, String>> getList()
{ // Don't make this method public! Ends otherwise in an infinite loop
// calling itself everytime.
return (List) getAttributes().get("list");
}
}
Run Code Online (Sandbox Code Playgroud)
并使用代码:
<ui:repeat var="group" value="#{currentContact.detailGroups}">
<h:panelGroup rendered="#{not empty group.values}">
<h:outputText id="controlMsg" value=" list:#{group.values}" /><br/><br/>
<utils:fieldTypeGroupList list="#{group.values}"
fieldNames="#{group.fields}" coupletClass="utils" />
</h:panelGroup>
</ui:repeat>
Run Code Online (Sandbox Code Playgroud)
id的文本controlMsg显示正确的值,#{group.values}而id组件内的控件输出inner_control_component显示上次使用的值.
这些值第一次是正确的......
我想这是一个使用CC bean的一个基本错误,否则它可能是一个错误MyFaces 2.1(我正在使用)
这种行为的解释很简单:视图中只有一个组件被定义.因此,只有一个支持组件与一个模型.由于模型在第一次获取时被延迟加载,因此在父迭代组件的每次迭代中都重用相同的模型.
将<ui:repeat>在视图生成时不运行(如JSTL一样),但鉴于在渲染时间.因此,视图中的组件在物理上没有被迭代的项目那么多<ui:repeat>.如果您正在使用<c:forEach>(或在视图构建期间运行的任何其他迭代标记),那么复合组件的行为将如您所期望的那样.
您想要更改数据模型在后备组件中的保存方式.您希望为父迭代组件的每次迭代保留单独的数据模型.其中一种方法是更换model属性,如下所示:
private Map<String, DataModel> models = new HashMap<String, DataModel>();
public DataModel getModel() {
DataModel model = models.get(getClientId());
if (model == null) {
model = models.put(getClientId(), new ListDataModel(getList()));
}
return model;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3829 次 |
| 最近记录: |