Jsf 自定义UIComponent的验证程序
我正在编写自己的自定义组件扩展Jsf 自定义UIComponent的验证程序,jsf,jsf-2,Jsf,Jsf 2,我正在编写自己的自定义组件扩展UIInput以及自定义渲染器。我希望我的组件在validator属性中应用方法表达式。但验证器属性不会呈现到html标记中。因此,writeAttribute(“validator”,validator,null)在这种情况下无效。我怎么能做到 我为UIInput编写了渲染器 @FacesRenderer(componentFamily = "javax.faces.Input", rendererType = "text") public class TextF
UIInput
以及自定义渲染器。我希望我的组件在validator
属性中应用方法表达式。但验证器属性不会呈现到html标记中。因此,writeAttribute(“validator”,validator,null)
在这种情况下无效。我怎么能做到
我为UIInput
编写了渲染器
@FacesRenderer(componentFamily = "javax.faces.Input", rendererType = "text")
public class TextFieldRenderer extends Renderer{
@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException{
String ClientId= component.getClientId(context);
String hint= (String) component.getAttributes().get("placeholder");
String styleClass= (String) component.getAttributes().get("styleClass");
String value =(String) component.getAttributes().get("value");
ResponseWriter writer= context.getResponseWriter();
writer.startElement("input", component);
writer.writeAttribute("type", "text", null);
writer.writeAttribute("name", ClientId, null);
writer.writeAttribute("placeholder", hint, "hint");
writer.endElement("input");
}
}
和text.taglib.xml
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib>
<namespace>http://text.com</namespace>
<tag>
<tag-name>hintText</tag-name>
<component>
<component-type>javax.faces.Input</component-type>
<renderer-type>text</renderer-type>
</component>
</tag>
</facelet-taglib>
现在我编写index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:lpform="http://xmlns.jcp.org/jsf/composite/lpform"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:text="http://text.com">
<h:head></h:head>
<h:body>
<h:form>
<text:hintText id="ht" placeholder="hint" validator="#{LBean.validateLoginField}">
<f:validateLength minimum="4"/>
</text:hintText>
<h:commandButton value="sub"/>
<h:message for="ht"/>
</h:form>
</h:body>
</html>
我想用
validator
属性验证hintText
的值,就像它在
上执行一样。但是无论是
还是验证程序=“#{LBean.validateLoginField}”
都不能验证我的hintText
您能提供完整的代码吗?似乎无法通过提供的信息查看问题。@Xtreme Biker已更新。
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:lpform="http://xmlns.jcp.org/jsf/composite/lpform"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:text="http://text.com">
<h:head></h:head>
<h:body>
<h:form>
<text:hintText id="ht" placeholder="hint" validator="#{LBean.validateLoginField}">
<f:validateLength minimum="4"/>
</text:hintText>
<h:commandButton value="sub"/>
<h:message for="ht"/>
</h:form>
</h:body>
</html>