SeamELResolver vs javax.el.ELResolver
bernix.bernix.ning.gmail.com Sep 18, 2008 2:54 PMWe have a collection of objects(entities) which has their own validation pattern, so we have to develop a custom JSF validator for them.
The problem is, when we trying to set the validator's property with a EL expression, this expression can not be resolvered exactly, and not getting the correct value in the custom validator.
I doubt it's because the ELContext can't find the Seam context, so that the seam component can not be resolvered.
Here's my code:
MyEntity.java
@Entity
public class MyEntity implements Serializable {
// other fields
private String valuePattern;
private String value;
// getters and setters
}RegexValidator.java
@Name("company.regexValidator")
@BypassInterceptors
@Validator
public class RegexValidator implements javax.faces.validator.Validator, Serializable {
@Logger Log log;
private String expression;
private String errorMessage;
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
log.info(">>>> validating regex [#0]", expression);
// compile the regex expression and matches the value
}
// setters for the "expression" and "errorMessage"
}company.taglib.xml
<?xml version="1.0"?> <!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"> <facelet-taglib> <namespace>http://www.company.com/taglib</namespace> <tag> <tag-name>validateRegex</tag-name> <validator> <validator-id>company.regexValidator</validator-id> </validator> </tag> </facelet-taglib>
MyBean.java
@Name("myBean")
@Scope(PAGE)
public class MyBean {
// other business logic
public List<MyEntity> getMyEntities() {
// generate the entities
}
}myBeanEdit.xhtml
...
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:my="http://www.company.com/taglib"
...
<ui:repeat value="#{myBean.myEntities}" var="myEntity">
<h:inputText value="#{myEntity.value}">
<my:validateRegex expression="#{myEntity.valuePattern}">
</h:inputText>
</ui:repeat>web.xml
... <context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/WEB-INF/company.taglib.xml</param-value> </context-param> ...
The log from the validator is >>>> validating regex [], I guess it's because the expression EL can not be resolvered correctly,
and I change the code to be:
... <my:validateRegex expression="[a-zA-Z]*"/> ...
it works fine.
then I try:
@Name("myBean")
@Scope(PAGE)
public class MyBean {
// other business logic
public String getRegex() {
return "[0-9]*";
}
}
<my:validateRegex expression="#{myBean.regex}">works fine too....
Then I try to change the faces-config.xml:
... <application> <el-resolver>org.jboss.seam.el.SeamELResolver</el-resolver> </application> ...
Just doesn't work... the expression still can not be resolvered...
My project was generated by seam-gen, Seam 2.0.2SP1, JBoss 4.2.2GA.
how can I resolver this valuePattern exactly in the validator? or may be I should write a tag class? like:
public class RegexValidatorTag extends ValidatorELTag {
private ValueExpression expression;
public Validator createValidator() throws JspException {
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
// how to use SeamELResolver to resolver the expression here?
RegexValidator validator = new RegexValidator();
validator.setExpression((String)expression.getValue(elContext));
}
}Any comments will be greatful,
Regards.