【问题标题】:Making p:commandButton work like h:button使 p:commandButton 像 h:button 一样工作
【发布时间】:2010-07-26 07:00:42
【问题描述】:

我的 webapp 中有这个工作代码:

<h:button value="Edit user..." outcome="/public/user" >
    <f:param name="userName" value="#{authBean.authUser}"/>
</h:button>

它的作用:

  1. 它使按钮发送一个 GET
  2. 它在 URL 中传递指定的参数,使其可收藏。

我需要什么:

  • 它应该像上面的 h:button 一样工作(发送 GET)
  • 按钮应该看起来像其他 Primefaces 按钮(例如,装饰有图像......等)。

这是我能得到的最接近的:

<p:commandButton value="Edit user..." action="/public/user?faces-redirect=true" ajax="false" immediate="true" >
    <f:param name="userName" value="#{authBean.authUser}"/>
</p:commandButton>

它发送一个 POST,该 POST 使用 GET 重定向到新 URL。但是参数在这个过程中丢失了。

另一个想法:

<p:linkButton value="Edit user..." href="http://localhost:8080/contextpath/faces/public/user.xhtml">
    <f:param name="userName" value="#{authBean.authUser}"/>
</p:linkButton>

GET 请求被中止(??? 根据 Firebug),当前页面再次被 POST。

这样做的正确方法是什么?

更新:这有效(在一个空页面上,没有 p:dataTable):

<p:linkButton value="Edit user..." href="http://localhost:8080/contextpath/faces/public/user.xhtml?userName=myusername">

但事实并非如此:

<p:linkButton value="Edit user..." href="http://localhost:8080/contextpath/faces/public/user.xhtml?userName=myusername&secondParam=otherValue">

后者导致:

500:javax.servlet.ServletException: 错误解析 /sample0.xhtml: 错误 Traced[line: 14] 参考 实体“secondParam”必须以 ';'分隔符。


UPDATE2:& 应该被转义:

<p:linkButton value="Edit user..." href="http://localhost:8080/contextpath/faces/public/user.xhtml?userName=myusername&amp;secondParam=otherValue">

它看起来不错......但我仍然得到 GET 中止和 POST 重新发送:

alt text http://img64.imageshack.us/img64/1017/primefaceslinkbutton.jpg

这是我一直在尝试的完整空白页面:

<?xml version='1.0' encoding='UTF-8' ?>
<!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:c="http://java.sun.com/jsp/jstl/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <h:head />
    <h:body>
        <h:form>
            <p:linkButton value="Click me" href="http://stackoverflow.com" />
        </h:form>
    </h:body>
</html>

Primefaces 2.1 版本。

【问题讨论】:

    标签: jsf jsf-2 primefaces


    【解决方案1】:

    在 PrimeFaces 2.2. 中,我们将弃用 linkBut​​ton 并引入 p:button。出票;

    http://code.google.com/p/primefaces/issues/detail?id=1037

    【讨论】:

    • 啊,很好。你能详细解释一下具体问题吗?
    • 好的,所以我现在会坚持使用没有图像的 h:button。感谢 BalusC 的时间和精力,当然还有 Cagatay 的回答 ;-)
    【解决方案2】:

    使用p:linkButton


    更新:根据您对代码示例的更新,URL 应在href 属性中指定,而不是在url 属性中。另请参阅我在上面链接的组件文档。

    这些症状至少听起来像是在触发异步 (Ajax) GET 请求,而不是同步请求。当请求在不同的域上触发时,FireBug 确实会给出这种错误。

    您没有其他一些 Javascript 会干扰/与链接按钮的默认行为发生冲突吗?该按钮通过一个简单的onclick="window.location=newurl;" 导航。


    更新 2:如果您在一个简单的页面中单独测试它是否有效?例如

    <!DOCTYPE html>
    <html xmlns="http://www.w3c.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.prime.com.tr/ui">
        <h:head>
            <title>Test</title>
        </h:head>
        <h:body>
            <p:linkButton value="test" href="http://stackoverflow.com" />
        </h:body>
    </html>
    

    【讨论】:

    • 我确实尝试过 p:linkBut​​ton,但它不起作用。 Firebug 说 GET 请求被中止,并且当前页面被再次 POST。
    • 抱歉,我没有进行完整的复制/粘贴。我正在使用“href”属性,Netbeans 甚至不接受“url”。我现在将检查其他 javascript...
    • 在一个空白页面上 p:linkBut​​ton 可以工作......有点。 f:param 不起作用。如果我直接在 URL 中指定参数(例如:“.../user.xhtml?userName=myusername”),它会起作用。当我再指定一个参数时,Glassfish 显示 500: javax.servlet.ServletException: Error Parsing /sample0.xhtml: Error Traced[line: 14] 对实体“relVerMin”的引用必须以 ';' 结尾分隔符。 (relVerMin 是我的第二个参数,如:“.../delrel.xhtml?relVerMaj=1&relVerMin=0”)
    • 哇哦!我在原始页面上使用 p:dataTable 。我在空白页插入同一张表,问题出现了……没有自定义javascript……所以p:dataTable与p:linkBut​​ton不兼容?
    • 好吧,我觉得我累了。也许我应该逃避“&”!明天试试。
    猜你喜欢
    • 1970-01-01
    • 2021-02-02
    • 2015-12-20
    • 2017-04-11
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多