【问题标题】:Sending query parameters via p:commandButton action does not work通过 p:commandButton 操作发送查询参数不起作用
【发布时间】:2017-05-15 20:43:52
【问题描述】:

在将查询参数传递到下一个 JSF 页面时,我需要帮助。我已经尝试了一切,但我被卡住了。如果我自己输入网址,我可以在第二页中检索参数。示例:来自 primefaces commandButton 操作的 localhost:8080/devices?propId=24&faces-redirect=true。但是,如果我尝试动态执行此操作,则会收到如下错误:Not a Valid Method Expression: property/devices?propId=#{recentPropertyFEnd.selected.idpropertytable}&faces-redirect=true。

我什么都试过了。 f:param、f:viewParam 等,但没有任何效果。

我想问题是,从 JSF 中的 commandButton 发送查询参数的正确方法是什么?

第一页的命令按钮:

<p:commandButton value="Show Devices" action="property/devices?propId=#{recentPropertyFEnd.selected.idpropertytable}&amp;faces-redirect=true"> </p:commandButton>

第二页 f:viewParam:

<f:metadata>
        <f:viewParam name="propId" value="#{deviceFEnd.propId}"/>
        <f:viewAction action="#{deviceFEnd.actioncheck()}" />
    </f:metadata>

管理bean第二页:

    @Named(value = "deviceFEnd")
    @RequestScoped

public class DeviceFEnd implements Serializable {

    private int propId;

    public DeviceFEnd() {
    }

    public int getPropId() {
        return propId;
    }

    public void setPropId(int propId) {
        this.propId = propId;
    }

    public void actioncheck() {
        int x = 0;
        x++;
    }

    @PostConstruct
    public void init() {

    }

}

就像我说的,如果我自己在操作中键入它,commandButton 可以工作,但如果我尝试使用来自 bean 的值会失败。 请帮忙...

【问题讨论】:

    标签: parameters commandbutton


    【解决方案1】:

    我想通了。使用 commandButton 的操作调用生成字符串 url 的方法。喜欢:

    <p:commandButton value="Show Devices" action="firstBean.createString()"> </p:commandButton>
    

    然后在 firstBean 中创建方法 createString 为:

    public String createString() {
        return "property/devices?propId=" + selected.getIdpropertytable() + "&faces-redirect=true";
    }
    

    这将创建字符串 url = http://localhost:8080/yourApp/property/devices?propId=46

    就是这样,您可以使用 viewParam 从第二个 xhtml 文件中获取它。

    【讨论】:

    • 请注意,这只适用于&amp;faces-redirect=true。没有它,任何参数都不会传递给 url。
    猜你喜欢
    • 2013-09-28
    • 2014-09-17
    • 2020-12-21
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多