2

Yesterday I posted a question about having to press a button twice to get it to work. I received good help, which is the hallmark of stackoverflow, but the problem still exists. I cut down my code to the bare minimum and the problem still exists. I read closely a BalusC suggestion, hoping that I would find a form inside a form. There is certainly nothing I can see so I will post my code in the hopes that additional pairs of eyes will see something.

I have a template which I call from welcome (the login part). This goes to userInfo which has a command button. This is the command button which I mysteriously have to press twice. On the second push the command button will take me to userPhoto. Everything is trimmed down to the minimum so that I can post it.

master.xthml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Master template</title>
</h:head>
<h:body>
<p:layout fullPage="true" >
    <p:layoutUnit position="north" size="254">
        Top
    </p:layoutUnit>

    <p:layoutUnit position="east" size="50" resizable="true">
        Hello
    </p:layoutUnit>

    <p:layoutUnit position="south" size="30">
        south
    </p:layoutUnit>

    <p:layoutUnit position="center">
        <ui:insert name="AreaOne">Default text</ui:insert>
    </p:layoutUnit>
</p:layout>

</h:body>
</html>

welcome1.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<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:p="http://primefaces.org/ui">
<ui:composition template="master.xhtml">

    <ui:define name="AreaOne">
        <h:form id="form1">
            <p:commandButton type="submit" value="Login" action="userInfo" />
        </h:form>
        <p:messages />
    </ui:define>
</ui:composition>
</html>

And last but not least userInfo.xhtml with the button which needs to be pressed twice:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<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:p="http://primefaces.org/ui">
<ui:composition template="master.xhtml">

    <ui:define name="AreaOne">
        <h:form id="formP">
            <p:commandButton type="submit" value="photos"  action="userPhoto" />
        </h:form>
        <p:messages />
    </ui:define>
</ui:composition>
</html>

I don't see any form nested inside any other form, but SOMETHING is wrong and I can't figure out what. Maybe BalusC is correct that it has something to do with ajax, but I don't see that either.

Thanks for all the help. Ilan

I added a button on the userPhoto page which goes back to the userInfo page. The Login button is the only one which works one the first press. When I use the command buttons to switch back and forth between userInfo and userPhoto, it always takes 2 pushes. I will show the center of userPhoto

<ui:composition template="master.xhtml">
    <ui:define name="AreaOne">
        <h:form id="form3">
            <p:commandButton type="submit" value="home page" action="userInfo" />
        </h:form>
        <p:messages />
    </ui:define>
</ui:composition>
Community
  • 1
  • 1
Ilan Tal
  • 457
  • 2
  • 8
  • 21
  • say, what will happen if you replace the `Default text` with `` (just for a test) haven't seen in the showcase that they used `` – Daniel May 31 '12 at 09:53
  • Hi Daniel. That would make it call itself recursively. The Default text is just so that SOMETHING will appear if I don't – Ilan Tal May 31 '12 at 10:13
  • I meant that you leave only the form in the master.xhtml – Daniel May 31 '12 at 10:45
  • That seems to me to be much too limiting. I want to be able to do anything inside area one. The template fills all the common stuff and areaOne changes from one xhtml to another. – Ilan Tal May 31 '12 at 10:53
  • ok, try replacing ` ` WITH THIS : `` – Daniel May 31 '12 at 11:04
  • Thanks for the suggestion. I had never seen it written that way, so it was worth a try. Unfortunately it doesn't help. Also I get a new problem: Warning: This page calls for XML namespace declared with prefix html but no taglibrary exists for that namespace. – Ilan Tal May 31 '12 at 11:25

1 Answers1

7

You've there a very specific problem. You're fully navigating by ajax instead of by a normal synchronous request and the whole view is replaced with the new view. The forms in the new view does not have the view state anymore which is indeed related to JSF issue 790. It's also not possible to reference the forms in the update of the <p:commandButton> as the forms does not exist in the same view.

After all, it's not recommendable to fully navigate by ajax. It makes your page non-bookmarkable and non-searchbotindexable. I suggest to replace all forms of

<p:commandButton ... action="otherViewId" />

by

<p:button ... outcome="otherViewId" />

This will navigate by normal synchronous requests and will create new views wherein all forms will have their view state. Note that the <p:button> doesn't require a <h:form>, you can omit it if necessary.


Unrelated to the concrete problem, I also suggest to put master.xhtml in the /WEB-INF folder so that the endusers can never request it by entering/guessing its URL in browser address bar. See also Which XHTML files do I need to put in /WEB-INF and which not?

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Thank you. I didn't know that commandButton navigated by ajax. I'll have to search for some information on when it is advisable to navigate via ajax and when not. I did notice that it wasn't changing the page value in the status bar which seems somewhat odd, but I didn't know what to do about it. Button is the answer. It is also nice if I'm not compelled to have a form tag. – Ilan Tal May 31 '12 at 13:37
  • @Balusc same issue here. What if I need action (process stuff in a backing bean too) rather than straight outcome. I get the two click issue in this case. I solved it by programmatically doing a redirect from the action method instead of returning a string (doesn't that mimic outcome= ?). Is there a simpler way to do this? I have a login page/form and also a conditionally rendered logout form in a site menu bar (in a ui:include). Before fixing: Login from the login page redirect to home page. User needs to click twice to log out. This isn't necessary if they navigate to some other page first. – Bill Rosmus Oct 08 '12 at 05:09