【问题标题】:continuation of pressing button twice连续按两次按钮
【发布时间】:2012-05-31 08:09:05
【问题描述】:

昨天我发布了一个关于必须按两次按钮才能使其工作的问题。我得到了很好的帮助,这是stackoverflow的标志,但问题仍然存在。我将代码减少到最低限度,但问题仍然存在。我仔细阅读了BalusC 的建议,希望能在表单中找到表单。当然我什么都看不到,所以我将发布我的代码,希望更多的眼睛能看到一些东西。

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

master.xml:

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

我没有看到嵌套在任何其他表单中的任何表单,但有些事情是错误的,我不知道是什么。也许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>

【问题讨论】:

  • 说,如果你用&lt;ui:include 替换&lt;ui:insert name="AreaOne"&gt;Default text&lt;/ui:insert&gt; 会发生什么&lt;ui:include src="master.xhtml" /&gt;(只是为了测试)在展示中没有看到他们使用了``
  • 你好丹尼尔。这将使它递归地调用自己。默认文本只是为了在我不 时会出现 SOMETHING
  • 我的意思是你只在master.xhtml中留下表格
  • 在我看来这限制太多了。我希望能够在区域一内做任何事情。该模板填充了所有常见的东西,并且 areaOne 从一个 xhtml 更改为另一个。
  • 好的,尝试用这个替换&lt;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"&gt; &lt;ui:composition template="master.xhtml"&gt; &lt;ui:composition 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" template="master.xhtml"&gt;

标签: ajax jsf primefaces


【解决方案1】:

您遇到了一个非常具体的问题。您完全通过 ajax 而不是正常的同步请求进行导航,并且整个视图被新视图替换。新视图中的表单不再具有确实与JSF issue 790 相关的视图状态。也不能引用&lt;p:commandButton&gt;update 中的表单,因为表单不存在于同一个视图中。

毕竟,不建议完全使用 ajax 导航。它使您的页面不可添加书签和不可搜索机器人索引。我建议替换所有形式的

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

通过

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

这将通过正常的同步请求进行导航,并将创建新视图,其中所有表单都将具有它们的视图状态。请注意,&lt;p:button&gt; 不需要&lt;h:form&gt;,如果需要,您可以省略它。


与具体问题无关,我还建议将master.xhtml 放在/WEB-INF 文件夹中,以便最终用户永远无法通过在浏览器地址栏中输入/猜测其URL 来请求它。另见Which XHTML files do I need to put in /WEB-INF and which not?

【讨论】:

  • 谢谢。我不知道由ajax 导航的commandButton。我将不得不搜索一些关于何时建议通过 ajax 导航以及何时不建议的信息。我确实注意到它没有改变状态栏中的页面值,这看起来有点奇怪,但我不知道该怎么做。按钮就是答案。如果我不强制使用表单标签也很好。
  • @Balusc 也有同样的问题。如果我需要采取行动(也在支持 bean 中处理东西)而不是直接的结果怎么办。在这种情况下,我得到了两次点击问题。我通过以编程方式从操作方法执行重定向而不是返回字符串来解决它(这不是模仿结果 = 吗?)。有没有更简单的方法来做到这一点?我在站点菜单栏中(在 ui:include 中)有一个登录页面/表单和一个有条件呈现的注销表单。修复前:从登录页面登录重定向到主页。用户需要点击两次才能退出。如果他们先导航到其他页面,则不需要这样做。
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 1970-01-01
  • 2014-07-11
  • 2019-10-28
相关资源
最近更新 更多