将转换器理解为托管bean

时间:2015-10-11 08:29:59

标签: jsf converter managed-bean mojarra

Mojarra 2.2.12

在facelets中,我有以下SelectOneMenu,包含在自定义标记中:

<ui:composition>
    <ui:param name="value" value="#{value}" />
    <ui:param name="collection" value="#{collection}" />
    <h:selectOneMenu value="#{value}">
        <f:selectItems value="#{collection}" />
        <f:converter binding="#{myConverter}"/> 
    </h:selectOneMenu>
</ui:composition>

这是实际的标签定义:

<tag>
    <tag-name>selector</tag-name>
    <attribute>
        <name>value</name>
        <type>ucom.adg.utils.ui.jsf.CollectionHolder</type>
        <required>true</required>
        <description>A value expression the selected item is going to be bound to</description>
    </attribute>
    <attribute>
        <name>collection</name>
        <type>java.lang.List</type>
        <required>true</required>>
        <description>The list all items to be displayed in the drop-down-list.
        </description>
    </attribute>
    <source>../resources/tags/selector.xhtml</source>
</tag>

其中转换器的实现方式如下:

@ManagedBean
@SessionScoped
public class MyConverter implements Converter{

    @ManagedProperty(value="#{myDao}")
    private MyDao myDao;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        //do some
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        //do some
    }
    // GET, SET
}

因此,我遇到的问题是h:selectOneMenu正在恢复,MyConverter myDao属性设置为null导致NullPointException成为context.getExternalContext().getRequest()).getSession().getAttribute("platformSelectorConverter")抛出。 myDao反过来将设置Dao的召集人返回到实际的RestoreViewPhase

所以我开始调试HtmlSelectOneMenu,并发现恢复组件的状态正由UIComponentBase# processRestoreState(FacesContext, Object)执行。

如果是public Object restore(FacesContext context) throws IllegalStateException { Object result = null; Class toRestoreClass; //some code ommited try { toRestoreClass = loadClass(className, this); } //catches if (null != toRestoreClass) { try { result = toRestoreClass.newInstance(); //Creating a new instance, // that's why the Dao is null } //ommmited return result; } ,则会以StateHolderSaver#restore结尾。并调用以下代码来恢复转换器:

Long myNum = Long.parseInt(RndNbGenNbs1.getText().toString());

我的问题是为什么Mojarra不会尝试从会话中获取转换器,而是创建一个新实例以及如何修复它?

0 个答案:

没有答案