JSF 驗(yàn)證要求示例

2018-02-22 10:54 更新

JSF教程 - JSF驗(yàn)證要求示例


以下代碼顯示如何根據(jù)需要標(biāo)記字段。

例子

下面的代碼來自UserBean.java。

package cn.w3cschool.common;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{
  
  String password;
  String confPassword;
  
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
  public String getConfPassword() {
    return confPassword;
  }
  public void setConfPassword(String confPassword) {
    this.confPassword = confPassword;
  }

}

以下代碼來自result.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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      >
    <h:body>
    <h:panelGrid columns="2">
    Password :  <h:outputText value="#{user.password}" />
    Confirm Password :  <h:outputText value="#{user.confPassword}" />
    </h:panelGrid>
      
    </h:body>
</html>

以下代碼來自demo.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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
    <h:form>
      <h:panelGrid columns="3">
        Enter your password : 
        <h:inputSecret id="password" value="#{user.password}" 
          size="20" required="true"
          label="Password" />
        <h:message for="password" style="color:red" />
        Enter your password again : 
        <h:inputSecret id="confPassword" value="#{user.confPassword}" 
          size="20" required="true"
          label="Confirm Password">
          <f:validateRequired />  
        </h:inputSecret>
        <h:message for="confPassword" style="color:red" />
      </h:panelGrid>
      <h:commandButton value="Submit" action="result" />
    </h:form>
    </h:body>
</html>
下載 Validation_Required.zip

運(yùn)行

將生成的WAR文件從目標(biāo)文件夾復(fù)制到Tomcat部署文件夾,并運(yùn)行Tomcat-Install-folder/bin/startup.bat。

Tomcat完成啟動后,在瀏覽器地址欄中鍵入以下URL。

http://localhost:8080/simple-webapp/demo.xhtml
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號