2011-01-22 44 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。立即字段跳过验证步骤。我认为你必须手动验证这个字段。