【问题标题】:Error message for immediate attribute即时属性的错误消息
【发布时间】:2011-01-22 07:36:47
【问题描述】:

见以下代码:

<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>

当我在不输入值的情况下提交表单时,它应该验证并为第一个文本框抛出错误。但是,它没有发生。可能是什么问题呢?

【问题讨论】:

  • 看起来不错。可以肯定的是,我什至将它复制粘贴到我的 JSF 2.0.3 / Tomcat 7.0.5 环境中。工作正常。您确定您正在运行您认为正在运行的代码吗?您使用的是什么 JSF impl/version?您使用的是什么 servletcontainer impl/version?您没有任何可能会干扰阶段的事件/阶段侦听器吗? (例如,过早致电facesContext.renderResponse()
  • 我正在使用最新的实现。我很困惑为什么它不起作用。

标签: java jsf


【解决方案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>

【讨论】:

  • balusc.blogspot.com/2006/09/…。阅读该部分。
  • 3 Process validations This phase is skipped due to the immediate="true" in the h:commandButton. 是的,就是这样。
  • 完整阅读应用请求值部分。
  • 克里希纳是对的。它应该工作。带有 immediate=true 的按钮只会验证带有 immediate=true 的输入。给定的代码示例应在第一个字段上显示必需的消息,而在第二个字段上显示任何内容,只要字段留空。
  • 我已经添加了我的配置和测试过的代码,你可以用它来验证
【解决方案2】:

因为它的立即数=真。立即字段跳过验证步骤。我认为您必须手动验证此字段。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多