2011-01-22 142 views
1

錯誤信息請參閱下面的代碼:立即屬性

<h:form> 
    <table> 
    <tr>     
    <td> 
     <h:inputText id="name" value="#{jsfBean.name }" required="true" immediate="true"/> 
     <h:message for="name"/> 
    </td> 
</tr> 
    <tr>     
     <td> 
     <h:inputText id="number" value="#{jsfBean.number }" required="true" /> 
     <h:message for="number"/> 
    </td> 
</tr>   
<tr> 
    <td> 
     <h:commandButton id="submit" value="Submit" action="#{jsfBean.submit }" immediate="true"/> 
</td> 
</tr> 
    </table> 
    </h:form> 

當我提交表單,而無需輸入值,應該驗證,並拋出錯誤第一個文本框。但是,它沒有發生。可能是什麼問題呢?

+0

看起來很好。可以肯定的是,我甚至將它拷貝到我的JSF 2.0.3/Tomcat 7.0.5環境中。工作正常。你確定你正在運行你認爲你正在運行的代碼嗎?您使用的是JSF impl/version?你在使用什麼servletcontainer impl/version?你有沒有任何事件/階段性聽衆可能會擾亂這個階段? (例如,太快調用`facesContext.renderResponse()`) – BalusC 2011-01-22 13:06:11

+0

我正在使用最新的實現。我迷惑它爲什麼不起作用。 – Krishna 2011-01-22 13:59:17

回答

1

它的工作對我來說

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>faces/index.xhtml</welcome-file> 
    </welcome-file-list> 
</web-app> 

faces-config.xml中

<?xml version='1.0' encoding='UTF-8'?> 

<!-- =========== FULL CONFIGURATION FILE ================================== --> 

<faces-config version="2.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> 


</faces-config> 

的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:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <h:head> 
     <title>Facelet Title</title> 
    </h:head> 
    <h:body> 
     <h:form> 

      <h:inputText value="#{myBean.val}" required="true" immediate="true"/> 
      <h:commandButton action="#{myBean.submit}" immediate="true"/> 

     </h:form> 
    </h:body> 
</html> 

<dependencies> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-web-api</artifactId> 
      <version>6.0</version> 
      <scope>provided</scope> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.2</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>2.0.2-b10</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-impl</artifactId> 
      <version>2.0.2-b10</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>taglibs</groupId> 
      <artifactId>standard</artifactId> 
      <version>1.1.2</version> 
     </dependency> 
    </dependencies> 
+0

我不確定它是否完全正確。 – Krishna 2011-01-22 07:55:41

0

因爲它的immediate = true。立即字段跳過驗證步驟。我認爲你必須手動驗證這個字段。