2013-04-17 26 views
-1

JSF驗證不起作用當我提交時,我在myfaces(2.1.10)和mojarra(2.1.21)下嘗試過,因爲相同的結果。這裏是我的代碼。理論上不執行onSubmit ()並在m_name.But中顯示錯誤消息,但實際上是執行onSubmit()並且不顯示錯誤消息。JSF驗證沒有模板中的鍋

的template.xhtml

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:rich="http://richfaces.org/rich"> 
<h:head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <meta http-equiv="Cache-Control" content="no-cache" /> 
    <title>Title</title> 
</h:head> 
<h:body> 
     <ui:insert name="body"></ui:insert> 
</h:body> 
</html> 

模板客戶端文件templatevalidation.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    template="/template.xhtml"> 
    <ui:define name="body"> 
     <h:form> 
      <div> 
       <h:inputText id="name" value="#{templateBean.name}"> 
        <f:validateRequired /> 
        <f:validateLength minimum="5" /> 
       </h:inputText> 
       <h:message id="m_name" for="name" /> 
      </div> 
      <div> 
       <h:commandButton action="#{templateBean.onSubmit}" value="submit"> 
        <f:ajax execute="name" render="m_name" /> 
       </h:commandButton> 
      </div> 
     </h:form> 
    </ui:define> 
</ui:composition> 

這裏是我的豆TemplateBean.class

import java.io.Serializable; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 

@ManagedBean 
@ViewScoped 
public class TemplateBean implements Serializable{ 

    private static final long serialVersionUID = 9009393522101806766L; 

    private String name; 

    public void onSubmit(){ 
     System.out.println("Name: " + name); 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 


} 

回答

-1

必須使用Hibernate的驗證

<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-validator</artifactId> 
    <version>5.0.0.Final</version> 
</dependency> 
0

您需要後更新您的<h:message>組件AJAX調用使用render顯示錯誤消息屬性,在<f:ajax>標籤:

<h:message id="nameMsg" for="name" /> 

<h:commandButton action="#{templateBean.onSubmit}" value="submit"> 
    <f:ajax execute="name" render="nameMsg" /> 
</h:commandButton> 
+0

仍不能正常工作。 –