jsf- validator with parameters from input

狂风中的少年 提交于 2019-12-11 10:23:37

问题


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>

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!"));
        }
    }

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


回答1:


In the attribute you need to set the value of the expression

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

it should be

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



回答2:


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"));


来源:https://stackoverflow.com/questions/13934503/jsf-validator-with-parameters-from-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!