栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

不对空值调用JSF Custom Converter

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

不对空值调用JSF Custom Converter

您发布的链接说,转换器应使用null,但不会说在每种情况下使用null值都会调用转换器。

具体来说,它并不表示当转换器 位于a内

h:outputText
且值为null 时将调用转换器。

如果您挖掘Mojarra的资源,您将看到:

//Line 355 -- com.sun.faces.renderkit.html_basic.HtmlBasicRenderer//method getCurrentValue    Object currentObj = getValue(component);    if (currentObj != null) {        currentValue = getFormattedValue(context, component, currentObj);    }

显然,空值将永远不会被转换!而且我找不到解决方法。

然后,如果您 真的需要 将值设置为null(可以返回0或类似的值),我认为唯一的机会就是制作自定义渲染器。这很简单:

您编写的渲染器将覆盖重要的方法:

package my;import javax.faces.component.UIComponent;import javax.faces.component.UIInput;import javax.faces.context.FacesContext;import com.sun.faces.renderkit.html_basic.TextRenderer;public class HtmlCustomRenderer extends TextRenderer {    @Override    public String getCurrentValue(FacesContext context, UIComponent component) {        if (component instanceof UIInput) { Object submittedValue = ((UIInput) component).getSubmittedValue(); if (submittedValue != null) {     // value may not be a String...     return submittedValue.toString(); }        }        String currentValue = null;        Object currentObj = getValue(component);        //Remove the 'if' to call getFormattedValue even if null        currentValue = getFormattedValue(context, component, currentObj);        return currentValue;    }}

然后我们在faces-config.xml中声明渲染器:

<render-kit>    <renderer>        <component-family>javax.faces.Output</component-family>        <renderer-type>javax.faces.Text</renderer-type>        <renderer-class>my.HtmlCustomRenderer</renderer-class>    </renderer></render-kit>

现在,您的转换器将使用空值调用!

希望对您有所帮助!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464010.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号