【问题标题】:jsf- validator with parameters from inputjsf- 带有输入参数的验证器
【发布时间】:2012-12-18 13:53:17
【问题描述】:

这里是我的 .xhtml 页面的一部分:

<h:inputText id="kartNumIn"  value="#{controller.mitarbeiter.kartenNummer}">
                <f:attribute name="foo" value="controller.mitarbeiter.id" />
                <f:validator validatorId="kartVal" binding="#{kartVal}" disabled="#{!controller.noUpdate}"/>
            </h:inputText>

这里是我的验证方法():

@Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

        int id=(Integer) component.getAttributes().get("foo"); //always 0
        int temp = (Integer) value;

        if (!(value instanceof Integer)) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Eingabefehler", "FEHLER:Bitte geben Sie eine Zahl ein!"));
        }

       System.out.print("Input"+value+"Aktuelle"+component.getAttributes().get("foo").toString());

        if (getAlleKartennummern().contains(temp) && temp!=id) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Kartennummerfehler", "FEHLER:Kartennumer bereits vergeben!"));
        }
    }

对于我的验证器,我需要第二个值。在这里我需要mitarbeiter.id!对于component.getAttributes().get("foo"),我总是为空......

【问题讨论】:

  • 请发布你在一个完全空白的游乐场项目上真正测试过的真实代码,所有设置都为默认设置(这样人们就可以通过复制'n'paste'n'运行代码而不复制你的确切问题做任何不明显的更改/存根)。到目前为止发布的代码似乎粗心过度简化并且与问题症状不匹配。我希望德国开发商能提供更多“Deutsche Gründlichkeit”;)

标签: java jsf jsf-2


【解决方案1】:

我认为OP不再对这个问题感兴趣,但是如果其他人发现这个问题:

int id=(Integer) component.getAttributes().get("foo");

当你得到一个字符串时不会工作。有点奇怪,你写的是你总是为空。你应该得到一个:

java.lang.String cannot be cast to java.lang.Integer

如果是这种情况,请尝试:

int id = Integer.parseInt((String) component.getAttributes().get("foo"));

【讨论】:

    【解决方案2】:

    在属性中需要设置表达式的值

    <f:attribute name="foo" value="controller.mitarbeiter.id" />
    

    应该是

    <f:attribute name="foo" value="#{controller.mitarbeiter.id}" />
    

    【讨论】:

    • 我相信这是对问题的粗心准备。 OP 提到他得到了null,这对于静态字符串是不可能的。原因更有可能是属性值依赖于仅在视图渲染期间才知道的变量,例如 &lt;h:dataTable&gt;var
    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多