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