1

我完全坚持使用<f:viewParam>and <f:viewAction>

我已经阅读了几个类似的问题但仍然无法正常工作。

简单示例如下:

测试.xhtml

<h:head>
    <title>Test</title>
</h:head>
<h:body>

    <f:view>
        <f:viewParam name="reason" value="#{testBean.reason}"/>
        <f:viewAction action="#{testBean.examineReason}"/>
    </f:view>

    <h:outputText value="Reason=#{testBean.reason}"/>
</h:body>

测试豆

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class TestBean {

    private String reason;

    @PostConstruct
    private void init() {
        System.out.println("urlParameter: "
                        + FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("reason"));

    }

    public void examineReason() {
        System.out.println("Reason is " + reason);
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
        System.out.println("Reason set: " + reason);
    }

}

GET-Request 由以下人员发起:

<h:button outcome="/pages/test.jsf" value="GET">
    <f:param name="reason" value="abcd" />
</h:button>

System.out 仅报告:

urlParameter: abcd

似乎 f:viewParam 和 f:viewAction 都不起作用。

我在 Tomcat 8.0.32 上使用 Mojarra 2.2 和 MyFaces 2.2 进行了测试

我错过了什么?

编辑:

问题<f:metadata>已解决:-tag 丢失。此外,<f:metadata>不能是 的子级<h:body>。通过 test.xhtml 中的以下修改,它对我有用:

<h:head>
    <title>Test</title>
</h:head>
<body>

    <f:view>
        <f:metadata>
        <f:viewParam name="reason" value="#{testBean.reason}"/>
        <f:viewAction action="#{testBean.examineReason}"/>
        </f:metadata>
    </f:view>

    <h:outputText value="Reason=#{testBean.reason}"/>
</body>
4

0 回答 0