jsf-验证器,带有输入中的参数 [英] jsf- validator with parameters from input

查看:63
本文介绍了jsf-验证器,带有输入中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

here a part of my .xhtml page:

<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>

这里是我的validate-method():

here my validate-method():

@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"),我总是为空....

for my validator i need a second value. Here i need the mitarbeiter.id! for component.getAttributes().get("foo") i get always null....

推荐答案

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

I don't think that the OP is interested in the problem any more, but if someone else finds this question:

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

当您得到一个字符串时将不起作用.您写的总是有点空,这有点奇怪.您应该得到一个:

Will not work as you get a String. A little strange that you write that you always get null. You should get a:

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

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

If this is the case, try:

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

这篇关于jsf-验证器,带有输入中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆