JSF學習五Ajax

來源:互聯網
上載者:User

標籤:jsf2   jsf   ajax   

驗證使用者名稱(不能有底線)和密碼(不能小於六位)1.UserBean.java
package ajax;import java.io.Serializable;import javax.faces.application.FacesMessage;import javax.faces.bean.ManagedBean;import javax.faces.bean.SessionScoped;import javax.faces.component.UIComponent;import javax.faces.context.FacesContext;import javax.faces.validator.ValidatorException;@ManagedBean(name="ajax")@SessionScopedpublic class UserBean implements Serializable {private String name="";private String password;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public void validateName(FacesContext fc,UIComponent c,Object value){if(((String)value).contains("_")){throw new ValidatorException(new  FacesMessage("Name cannot contain underscores!"));}}public void validatePassword(FacesContext fc,UIComponent c,Object value){if(((String)value).length()<6){throw new ValidatorException(new  FacesMessage("Password at least six words!"));}}}


2.hello.xhtml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"xmlns:h="http://java.sun.com/jsf/html"xmlns:f="http://java.sun.com/jsf/core"><h:head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title></h:head><h:body><h:form><p><h:outputText value="Name"/><h:inputText id="name" value="#{ajax.name}" validator="#{ajax.validateName}"><f:ajax event="blur" execute="@this" render="nameError"/></h:inputText><h:message for="name" id="nameError" style="color:red" /></p><h:outputText value="Password"/><h:inputSecret id="password" value="#{user.password}" validator="#{ajax.validatePassword}"><f:ajax event="blur" render="passwordError"/></h:inputSecret><h:message for="password" id="passwordError" style="color:red" /></h:form></h:body></html>


3.運行結果如下

JSF學習五Ajax

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.