0

Simple piece of code below, yet it doesn't work as expected. The button only starts working the 2nd time I press it (displays the expected error message). The first time it does nothing.

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


If I remove the validation altogether and just try to make a call to a backing Bean, I get the same result. After loading the page, pressing the button the 1st time does nothing. Pressing it subsequent times works fine.

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


Here is another test, with 2 DropDown controls. Changing the value on the first one is supposed to update the available options on the same one. After loading the page, the first time I select an item on Country does nothing on Province. Thereafter it works fine.

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



Here's the request/response cycles for the 1st Test illustrated above:

When page first loads

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



First click (when nothing happens)

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>




Second click (when it works)

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>

I'm on Glassfish 3.1.2, Mojarra 2.1.9, PrimeFaces 5.2

Thanks!

DTs
  • 1,196
  • 1
  • 11
  • 28
  • What's the HTTP request/response during the failing action? (press F12 and open Network tab). Nonetheless, is this the same problem? http://stackoverflow.com/q/11408130 – BalusC Oct 20 '15 at 08:06
  • @BalusC I updated my question to show the HTTP request/response cycles when the page first loads, when the first click occurs, and when the 2nd click occurs. It doesn't look like this is a duplicate to the issue you linked to, as I'm only using 1 form, and also my form already has an id. – DTs Oct 20 '15 at 20:38
  • @BalusC Looking at this in a diff viewer, it seems that the only difference between the request of the working Vs. non-working click, is that the former has an extra POST parameter appended: `javax.faces.ViewState=...` (and different `Content-Length` of course). – DTs Oct 20 '15 at 20:59
  • The absence of `javax.faces.ViewState` parameter causes non-working forms (because there's no JSF view state at all, so basically a brand new view would be created). The aforelinked question shows how to fix it. However, this problem is not expected to occur on the given code snippet. So either you oversimplified too much, or there's more at matter at your environment. Only you can tell why `javax.faces.ViewState` isn't being sent in first place. – BalusC Oct 20 '15 at 21:14
  • The only thing that I can think of is MyFaces and Mojarra libs messing up with each other. Glassfish seems to have it's own Mojarra implementation. I tried replacing `javax.faces.jar` from `glass fish/modules/` with the latest MyFaces binaries for JSF2.1 (`myfaces-bundle-2.1.17.jar`). However the server wouldn't start, so I abandoned that idea and put back the Mojarra lib, but upgraded to 2.1.9 - I thought that was ok since I have in my Eclipse project settings JSF 2.1 selected. However looking at the HTTP responses above, I see header `X-Powered-By` returning `JSF/2.0`. – DTs Oct 20 '15 at 22:22
  • Further, If I don't include in my project settings the MyFaces libs, glass fish cannot start the application. If include the Mojarra libs, I get exceptions. If I don't include any libs, I get exceptions. I have tried adding `` in `glass-fish-web.xml`, but no love. Thoughts? – DTs Oct 20 '15 at 22:26

1 Answers1

0

It seems that the problem was that Glassfish's own Mojarra libs were messing with the MyFaces libs that I had in my project, effectively resulting in a dirty classpath.

The following fixed the problem:

  • Remove MyFaces libs from my project's classpath
  • Replace MyFaces' listener to Mojarra's listener in web.xml like so:


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


Many thanks to @BalusC for pointing out that there is probably something wrong with my environment

DTs
  • 1,196
  • 1
  • 11
  • 28