JSF事件在第一次使用屏幕元素时不会触发,但随后可以正常工作

时间:2015-10-20 03:23:10

标签: jsf primefaces

下面的简单代码,但它没有按预期工作。该按钮仅在我按下它时第二次开始工作(显示预期的错误消息)。它第一次什么也没做。

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>
   <title>Some Title</title>
</h:head>

<h:body>
   <h:form>
      <p:panelGrid id="panel" columns="2" border="0" cellpadding="10" cellspacing="1">      
         <p:outputLabel value="Enter Username" />
         <p:inputText id="username" size="20" label="UserName" required="true">
            <f:validateLength for="username" minimum="5" maximum="20" />            
         </p:inputText>

         <p:outputLabel value="Test" />
         <p:commandButton id="submit" value="Submit" action="result"/>
      </p:panelGrid>
      <p:messages autoUpdate="true" />
   </h:form>
</h:body>
</html>


如果我完全删除验证并尝试调用支持Bean,我会得到相同的结果。加载页面后,第一次按下按钮不起作用。随后按下它可以正常工作。

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>
   <title>Some Title</title>
</h:head>

<h:body>
   <h:form>
      <p:panelGrid id="panel" columns="2" border="0" cellpadding="10" cellspacing="1">      
         <p:outputLabel value="Enter Username" />
         <p:inputText id="username" size="20" label="UserName" />

         <p:outputLabel value="Test" />
         <p:commandButton value="Continue" actionListener="#{signupBean.signup}" icon="ui-icon-check" />
      </p:panelGrid>
      <p:messages autoUpdate="true" />
   </h:form>
</h:body>
</html>


这是另一个测试,有2个DropDown控件。更改第一个上的值应该更新同一个上的可用选项。加载页面后,我第一次在Country上选择一个项目在省上没有任何作用。此后它工作正常。

<p:selectOneMenu id="country" value="#{signupBean.countryId}" style="width:200px">
    <p:ajax update="province" />
    <f:selectItem itemLabel="Select Country" itemValue="0" noSelectionOption="true" />
    <f:selectItems value="#{countriesBean.countries}" var="country" itemValue="#{country.id}" itemLabel="#{country.name}" />
</p:selectOneMenu>

<p:selectOneMenu id="province" value="#{signupBean.provinceId}" style="width:200px">
    <f:selectItem itemLabel="Select State or Province" itemValue="0" noSelectionOption="true" />
    <f:selectItems value="#{signupBean.provinces}" var="province" itemValue="#{province.provinceId}" itemLabel="#{province.name}" />
</p:selectOneMenu>



以下是上述第一次测试的请求/响应周期:

首次加载页面时

Request
GET                 /PictabiteServer/faces/pages/venues/zzbTest.xhtml HTTP/1.1
Host:               localhost:8080
Accept:             text/html,application/xhtml+xml,application/xml;q=0.9,*/ *;q=0.8
Cookie:             SESSIONID=6b770d215805cacdf9f6a1d0d29c; has_js=1
User-Agent:         Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9
Accept-Language:    en-us
Accept-Encoding:    gzip, deflate
Connection:         keep-alive

Response
HTTP/1.1            200 OK
X-Powered-By:       JSF/2.0
Content-Type:       text/html;charset=UTF-8
Content-Length:     2679
Date:               Tue, 20 Oct 2015 19:56:24 GMT



首先点击(没有任何反应时)

Request
POST                /PictabiteServer/faces/pages/venues/zzbTest.xhtml HTTP/1.1
Host:               localhost:8080
Accept:             application/xml, text/xml, */ *; q=0.01
Faces-Request:      partial/ajax
X-Requested-With:   XMLHttpRequest
Accept-Encoding:    gzip, deflate
Accept-Language:    en-us
Content-Type:       application/x-www-form-urlencoded; charset=UTF-8
Origin:             http://localhost:9181
Content-Length:     188
User-Agent:         Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9
Referer:            http://localhost:9181/PictabiteServer/faces/pages/venues/zzbTest.xhtml
Connection:         keep-alive
Cookie:             SESSIONID=6b770d215805cacdf9f6a1d0d29c; has_js=1
Payload:            javax.faces.partial.ajax=true&javax.faces.source=form%3Asubmit&javax.faces.partial.execute=%40all&javax.faces.partial.render=form&form%3Asubmit=form%3Asubmit&form%3Ausername=&form_SUBMIT=1

Response
HTTP/1.1            200 OK
X-Powered-By:       JSF/2.0
Cache-Control:      no-cache
Content-Type:       text/xml;charset=UTF-8
Content-Length:     2534
Date:               Tue, 20 Oct 2015 19:59:33 GMT
Payload:            
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html>
<html>
<head>
    <![CDATA[
    <form action="/PictabiteServer/faces/pages/venues/zzbTest.xhtml" enctype=
    "application/x-www-form-urlencoded" id="form" method="post" name="form">
        <table class="ui-panelgrid ui-widget" id="form:panel" role="grid">
            <tbody>
                <tr class="ui-widget-content" role="row">
                    <td class="ui-panelgrid-cell" role="gridcell"><label class=
                    "ui-outputlabel ui-widget" id="form:j_idt6">Enter
                    Username</label></td>
                    <td class="ui-panelgrid-cell" role="gridcell">
                        <input aria-required="true" class=
                        "ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all"
                        id="form:username" name="form:username" size="20" type=
                        "text">
                        <script id="form:username_s" type="text/javascript">
                        <!--
                        PrimeFaces.cw("InputText","widget_form_username",{id:"form:username",widgetVar:"widget_form_username"});
                        //-->
                        </script>
                    </td>
                </tr>
                <tr class="ui-widget-content" role="row">
                    <td class="ui-panelgrid-cell" role="gridcell"><label class=
                    "ui-outputlabel ui-widget" id=
                    "form:j_idt7">Test</label></td>
                    <td class="ui-panelgrid-cell" role="gridcell">
                        <button class=
                        "ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                        id="form:submit" name="form:submit" onclick=
                        "PrimeFaces.ab({s:&quot;form:submit&quot;,u:&quot;form&quot;});return false;"
                        type="submit"><span class=
                        "ui-button-text ui-c">Submit</span></button>
                        <script id="form:submit_s" type="text/javascript">
                        <!--
                        PrimeFaces.cw("CommandButton","widget_form_submit",{id:"form:submit",widgetVar:"widget_form_submit"});
                        //-->
                        </script>
                    </td>
                </tr>
            </tbody>
        </table><!-- <p:messages autoUpdate="true" /> -->
        <div aria-live="polite" class="ui-message" id="form:j_idt9">
        </div><input name="form_SUBMIT" type="hidden" value="1">
    </form>
</body>
</html>]]>
    <![CDATA[xx0mj9nLEgqpT1Qzy+C+EOPBDpgzlWfrR47DK7Jrk7FQOmB3AoP9QejbqT9qxvCnkXXkd9atuFv4K78lufbk+qH+bNArqpnjf2p48aHz8pe5gGbbmv2RB2LF1jauia2rP9UbsDKgFRmy6CrlGy4H70h40u4Ys9Jvhc83utraiA8kELR5Hc+7lnaF3ABTt0Xnhj2jAT4QzPyCHCEKyZpU3OeN9eW5JhDYK18U0uiND4OYcbHZsfUsphazJZ7q9/dv83QA3X6Wx2CGo9XlenaiFdzRi6btnXaost+LfHr9Yd8VCTQrImmR21i9nQLNiujQAiEPEbcz3cbWOTUGbrUfMFIYm3GaFPyXZAZmqF3OPyFpX1sd0IJm31IYm3GaFPyXi1fDUjlkMX7da9AALtf+yr7cGAfLWthbyDplp3xbCvAz/olFB/apmqkmXyE6TJMSP+taaAhNDdsDz6QF27UCq1IEVl62P6zkVdR1WkREh/hQluZ6xcOFj93EqzKxJglgJqEsXAutHGNe+tgcBumIonsWVDKDN31s9HwjLytEUcsF21bCgL+z9zBxDWrwLp2CG6jM+1PmBH35HiPVcEjla3iU2eV2fgNhn8FvRsM3TkydodlIc1eGpCSb2eGxH8HL/87IeMdlt0bEULSyjE+YAjDPhT3+KVpjqwaz1b7z9H2Nh5Hllsp+A5qJGzg=]]>
    <title></title>
</head>
<body>
</body>
</html>




第二次点击(工作时)

Request
POST                /PictabiteServer/faces/pages/venues/zzbTest.xhtml HTTP/1.1
Host:               localhost:8080
Accept:             application/xml, text/xml, */ *; q=0.01
Faces-Request:      partial/ajax
X-Requested-With:   XMLHttpRequest
Accept-Encoding:    gzip, deflate
Accept-Language:    en-us
Content-Type:       application/x-www-form-urlencoded; charset=UTF-8
Origin:             http://localhost:9181
Content-Length:     951
User-Agent:         Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9
Referer:             http://localhost:9181/PictabiteServer/faces/pages/venues/zzbTest.xhtml
Connection:         keep-alive
Cookie:             SESSIONID=6b770d215805cacdf9f6a1d0d29c; has_js=1
Payload:            javax.faces.partial.ajax=true&javax.faces.source=form%3Asubmit&javax.faces.partial.execute=%40all&javax.faces.partial.render=form&form%3Asubmit=form%3Asubmit&form%3Ausername=&form_SUBMIT=1&javax.faces.ViewState=xx0mj9nLEgqpT1Qzy%2BC%2BEOPBDpgzlWfrR47DK7Jrk7FQOmB3AoP9QejbqT9qxvCnkXXkd9atuFv4K78lufbk%2BqH%2BbNArqpnjf2p48aHz8pe5gGbbmv2RB2LF1jauia2rP9UbsDKgFRmy6CrlGy4H70h40u4Ys9Jvhc83utraiA8kELR5Hc%2B7lnaF3ABTt0Xnhj2jAT4QzPyCHCEKyZpU3OeN9eW5JhDYK18U0uiND4OYcbHZsfUsphazJZ7q9%2Fdv83QA3X6Wx2CGo9XlenaiFdzRi6btnXaost%2BLfHr9Yd8VCTQrImmR21i9nQLNiujQAiEPEbcz3cbWOTUGbrUfMFIYm3GaFPyXZAZmqF3OPyFpX1sd0IJm31IYm3GaFPyXi1fDUjlkMX7da9AALtf%2Byr7cGAfLWthbyDplp3xbCvAz%2FolFB%2FapmqkmXyE6TJMSP%2BtaaAhNDdsDz6QF27UCq1IEVl62P6zkVdR1WkREh%2FhQluZ6xcOFj93EqzKxJglgJqEsXAutHGNe%2BtgcBumIonsWVDKDN31s9HwjLytEUcsF21bCgL%2Bz9zBxDWrwLp2CG6jM%2B1PmBH35HiPVcEjla3iU2eV2fgNhn8FvRsM3TkydodlIc1eGpCSb2eGxH8HL%2F87IeMdlt0bEULSyjE%2BYAjDPhT3%2BKVpjqwaz1b7z9H2Nh5Hllsp%2BA5qJGzg%3D

Response
HTTP/1.1            200 OK
X-Powered-By:       JSF/2.0
Cache-Control:      no-cache
Content-Type:       text/xml;charset=UTF-8
Content-Length:     3013
Date:               Tue, 20 Oct 2015 20:05:12 GMT
Payload:            
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html>
<html>
<head>
    <![CDATA[    <form action="/PictabiteServer/faces/pages/venues/zzbTest.xhtml" enctype=
    "application/x-www-form-urlencoded" id="form" method="post" name="form">
        <table class="ui-panelgrid ui-widget" id="form:panel" role="grid">
            <tbody>
                <tr class="ui-widget-content" role="row">
                    <td class="ui-panelgrid-cell" role="gridcell"><label class=
                    "ui-outputlabel ui-widget" id="form:j_idt6">Enter
                    Username</label></td>
                    <td class="ui-panelgrid-cell" role="gridcell">
                        <input aria-required="true" class=
                        "ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all ui-state-error"
                        id="form:username" name="form:username" size="20" type=
                        "text" value="">
                        <script id="form:username_s" type="text/javascript">
                        <!--
                        PrimeFaces.cw("InputText","widget_form_username",{id:"form:username",widgetVar:"widget_form_username"});
                        //-->
                        </script>
                    </td>
                </tr>
                <tr class="ui-widget-content" role="row">
                    <td class="ui-panelgrid-cell" role="gridcell"><label class=
                    "ui-outputlabel ui-widget" id=
                    "form:j_idt7">Test</label></td>
                    <td class="ui-panelgrid-cell" role="gridcell">
                        <button class=
                        "ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                        id="form:submit" name="form:submit" onclick=
                        "PrimeFaces.ab({s:&quot;form:submit&quot;,u:&quot;form&quot;});return false;"
                        type="submit"><span class=
                        "ui-button-text ui-c">Submit</span></button>
                        <script id="form:submit_s" type="text/javascript">
                        <!--
                        PrimeFaces.cw("CommandButton","widget_form_submit",{id:"form:submit",widgetVar:"widget_form_submit"});
                        //-->
                        </script>
                    </td>
                </tr>
            </tbody>
        </table><!-- <p:messages autoUpdate="true" /> -->
        <div aria-live="polite" class=
        "ui-message ui-message-error ui-widget ui-corner-all" id="form:j_idt9">
            <span class="ui-message-error-icon"></span><span class=
            "ui-message-error-detail">UserName: Validation Error: Value is
            required.</span>
        </div><input name="form_SUBMIT" type="hidden" value="1">
    </form>]]>
    <![CDATA[xx0mj9nLEgqpT1Qzy+C+EOPBDpgzlWfrR47DK7Jrk7FQOmB3AoP9QejbqT9qxvCnkXXkd9atuFv4K78lufbk+qH+bNArqpnjf2p48aHz8pe5gGbbmv2RB2LF1jauia2rP9UbsDKgFRmy6CrlGy4H70h40u4Ys9Jvhc83utraiA8kELR5Hc+7lnaF3ABTt0Xnhj2jAT4QzPyCHCEKyZpU3OeN9eW5JhDYK18U0uiND4OYcbHZsfUsphazJZ7q9/dv83QA3X6Wx2CGo9XlenaiFdzRi6btnXaost+LfHr9Yd8VCTQrImmR21i9nQLNiujQM5JcCsg7JifdpiiuudmDswM/MBwgukOv29N5DulgW+Z8ZyzewXf59dBIejAk2ZA+yb4xK9rOeXQ+SmuXGQbEfmbL/M1mMQu9GLpWVz6T4vZQugK/IIzu/Xy9ZjaAGADKs6ikCgHJhHUuqMaYc3MCAE9DFXz/6oBbsTNk6gtMP222ZCEkYXzli1762BwG6Yii0JIWxUiD7I/37LHdN9NM2Ub4coNHfQyFw8jVus8EplV0URb5c7a0dst0JW0r2vh9sSqzrFMWqUHrz1MXfpWQcN3AlYb+If9sZgYS18sybSXN9KbwxaTpn2/FhykQZprHRb/ml3JS0kZAtQQVEQx/t7tEHhQT34DPREmQu0CWZ+PpBho06WB+WB0+O+5CyHzCEVP8E7PJ9WLnxxE4aVLxmClp1ON28Jf3soqhmkQVOibZPAypHAaNzdg/H5kzgWHzzwlZxgjpi7kwd2HFRT3UX8d0432oRunudvnluvpwXjon8JQ4K5Ts007SHgkdqhpz6GUGEAXqUzHtP5AAFrLC0eAQhPCX1OtL/9+LytYXyzl9caQyPBcrT9Lpbl3AeHL1rlV15A==]]>
    <title></title>
</head>
<body>
    {"validationFailed":true}
</body>
</html>

我正在使用Glassfish 3.1.2,Mojarra 2.1.9,PrimeFaces 5.2

谢谢!

1 个答案:

答案 0 :(得分:0)

似乎问题是Glassfish自己的Mojarra库正在弄乱我在项目中使用的MyFaces库,实际上导致了一个脏的类路径。

以下解决了问题:

  • 从项目的类路径中删除MyFaces库
  • 替换MyFaces' 在web.xml中监听Mojarra的监听器,如:


<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class> <!-- for Mojarra -->
    <!-- <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>  --> <!-- for MyFaces -->
</listener>


非常感谢@BalusC指出我的环境可能有问题