继续按两次按钮

时间:2012-05-31 08:09:06

标签: ajax jsf primefaces

昨天我发布了一个关于必须按两次按钮才能使其工作的问题。我收到了很好的帮助,这是stackoverflow的标志,但问题仍然存在。我将我的代码减少到最低限度,问题仍然存在。我仔细阅读了一个BalusC建议,希望我能在表格中找到一个表格。我肯定没有什么可以看到的,所以我会发布我的代码,希望额外的双眼会看到一些东西。

我有一个模板,我从welcome(登录部分)调用。这将转到userInfo,它有一个命令按钮。这是我神秘地要按两次的命令按钮。在第二次按下命令按钮将我带到userPhoto。一切都被削减到最低限度,以便我可以发布它。

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>

最后但并非最不重要的是userInfo.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="formP">
            <p:commandButton type="submit" value="photos"  action="userPhoto" />
        </h:form>
        <p:messages />
    </ui:define>
</ui:composition>
</html>

我没有看到嵌套在任何其他形式内的任何形式,但是SOMETHING错了,我无法弄清楚是什么。也许BalusC是正确的,它与ajax有关,但我也没有看到。

感谢所有帮助。 伊兰

我在userPhoto页面上添加了一个按钮,该按钮返回到userInfo页面。 “登录”按钮是唯一一个在第一次按下时工作的按钮。当我使用命令按钮在userInfo和userPhoto之间来回切换时,它总是需要2次推送。我将展示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>

1 个答案:

答案 0 :(得分:7)

你有一个非常具体的问题。您完全通过ajax导航而不是通过正常的同步请求进行导航,并且整个视图将替换为新视图。新视图中的表单不再具有与JSF issue 790确实相关的视图状态。由于表格在同一视图中不存在,因此也无法在update的{​​{1}}中引用表单。

毕竟,不建议用ajax完全导航。它使您的页面不可收藏且非searchbotindexable。我建议替换所有形式的

<p:commandButton>

通过

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

这将按正常的同步请求进行导航,并创建新视图,其中所有表单都将具有其视图状态。请注意,<p:button ... outcome="otherViewId" /> 不需要<p:button>,如有必要,您可以省略它。


对于具体问题

无关,我还建议将<h:form>放在master.xhtml文件夹中,以便最终用户永远不会通过在浏览器中输入/猜测其网址来请求它地址栏。另请参阅Which XHTML files do I need to put in /WEB-INF and which not?