如何实现jsf验证器? [英] How to implement jsf validator?

查看:85
本文介绍了如何实现jsf验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI

我想知道如何在JSF中实现Validator.声明验证者ID的好处是什么.在生命周期中什么时候会被调用?我已经实现了以下代码.请找出代码中的错误.我在生命周期的任何地方都没有看到它.

I want to know how to implement Validator in JSF. What is the advantages of declaring the validator-id. When it will be called in the life cycle?. I have implemented the following code. Please find out what is wrong in the code. I am not seeing it called anywhere in the life cycle.

   <?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
 <lifecycle>
  <phase-listener>javabeat.net.jsf.JsfPhaseListener</phase-listener>
 </lifecycle>
 <validator>
  <validator-id>JsfValidator</validator-id>
  <validator-class>javabeat.net.jsf.JsfValidator</validator-class>
 </validator>

 <managed-bean>
  <managed-bean-name>jsfBean</managed-bean-name>
  <managed-bean-class>javabeat.net.beans.ManagedBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <navigation-case>
   <from-outcome>success</from-outcome>
   <to-view-id>success.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>
</faces-config>


public class JsfValidator implements Validator {

 public JsfValidator()
 {
  System.out.println("Inside JsfValidator Constructor");
 }
 @Override
 public void validate(FacesContext facesContext, 
       UIComponent uiComponent, 
       Object object)
   throws ValidatorException {
  System.out.println("Inside Validator");
 }
}

推荐答案

您必须将此自定义验证器绑定到jsf页面上的某些jsf组件,这就是使用您定义的vaidator-id的地方.例如,您的jsf页面可能看起来像这样

You have to tie this custom validator to some jsf component on the jsf page, and that is where the vaidator-id you have defined is used. for e.g your jsf page might look something like this

<h:inputText id="myInput" value="#{mybean.property}">
     <f:validator validatorId="JsfValidator"/>
</h:inputText>

如果您正在学习jsf验证和转换,那么我强烈建议您阅读本文并通过示例清楚地说明了在jsf生命周期阶段进行验证等的示例.

If you are learning jsf validation and conversion then I strongly recommend you to go through this article which clearly explains with examples on in which jsf life cycle phase the validation happens etc.

这篇关于如何实现jsf验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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