用于输入控制的JSF验证模式 [英] JSF validation pattern for input control

查看:49
本文介绍了用于输入控制的JSF验证模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSF页面,并且在此页面中有一个inputtext.我从此输入文本中获取用户关键字.我的问题是我想控制用户输入的关键字数量(用-"分隔),并检查它们的数量不超过特定数量.是否有任何验证模式或更好的方法来检查它们?

I have a JSF page and I have a inputtext in this page. I take the user keywords form this inputtext. My problem is that I want to control the number of keywords that the user enter(they separated by "-" ) and check them to not to be more than a specific number. is there any validation pattern or any better way to check them?

推荐答案

您可以使用

You can do this validation by using a Custom Validator, by defining a java class implementing Validator interface, which should performs the specific desired validation. Here's a small example that shows when the user types keywords (separated by "-") more than a predetermined max limit (e.g 5), a validation message pops up when passing to the next field :

查看: index.xhtml

View : 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:f="http://java.sun.com/jsf/core"      
  xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>Custom Validator Example</title>
</h:head>
<h:body>

    <h:form>
        <h:panelGrid columns="3">
        Give strings separated by "-" : 
        <h:inputText id="input" size="30" required="true"  requiredMessage="Field required" value="#{myBean.myExpression}">
                <f:ajax event="blur" render="inputMessage" />
                <f:validator binding="#{myValidator}" />
        </h:inputText>
        <h:message for="input" id="inputMessage" style="color:red" />

        Enter your name:
        <h:inputText id="input1" required="true" requiredMessage="missed name" value="#{myBean.name}"  />
        </h:panelGrid>
    </h:form>
</h:body>

一个简单的托管bean: MyBean.java

A simple managed-bean : MyBean.java

 // imports
    @ManagedBean
    @RequestScoped
    public class MyBean implements Serializable {

        private String myExpression ;
        private String name ;

        // getters/setters

}

自定义验证器: MyValidator.java

Custom Validator : MyValidator.java

// imports
@ManagedBean
@ViewScoped
public class MyValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

        int max_limit = 5; // for example

        String expression = (String) value;
        String[] result = expression.split("-");
        int count=0;

        for(int i=0;i<result.length;i++){
            if (result[i].equals("")) continue;  // Empty keywords (e.g successive "-" caracters) are ignored. Else, just omit this line
            count++;
        }

        if (count > max_limit) {
            FacesMessage facesmsg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"Exeeded allowed limit !",null);
            throw new ValidatorException(facesmsg);
        }
    }
}

这篇关于用于输入控制的JSF验证模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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