使 p:commandButton 像 h:button 一样工作

5

我在我的Web应用程序中有这个可行的代码:

<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请求,然后通过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 出错:错误 已跟踪到[line: 14] 实体引用“secondParam”必须以分号“;”结束。


更新2:& 应该被转义:

<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版本发布。

2个回答

3

啊,好的。你能详细解释一下这个特定的问题吗? - BalusC
好的,那我现在会暂时使用没有图片的h:button。感谢BalusC的时间和努力,当然也感谢Cagatay的回答;-) - egbokul
这是一个新的p:button;http://www.primefaces.org:8080/prime-showcase-labs/ui/button.jsf - Cagatay Civici

1
使用{{link1: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>

抱歉,我没有完全复制/粘贴。我正在使用“href”属性,“url”甚至不被Netbeans接受。我现在会检查其他的JavaScript... - egbokul
在空白页面上,p:linkButton 能够工作... 但是 f:param 不起作用。如果我直接在 URL 中指定参数(例如:".../user.xhtml?userName=myusername"),它就能正常工作。当我指定一个以上的参数时,Glassfish 报错 500:javax.servlet.ServletException: Error Parsing /sample0.xhtml: Error Traced[line: 14] The reference to entity "relVerMin" must end with the ';' delimiter.(relVerMin 是我的第二个参数,例如:".../delrel.xhtml?relVerMaj=1&relVerMin=0") - egbokul
哇哦!我在原始页面上使用了p:dataTable。我在空白页面上插入了相同的表格,问题出现了...没有自定义JavaScript...所以p:dataTable与p:linkButton不兼容? - egbokul
好的,我想我有点累了。也许我应该转义“&”符号!明天再试试看。 - egbokul
关于解析错误,您应该在XML中使用&amp;来表示& - BalusC
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接