我完全不习惯使用<f:viewParam>
和<f:viewAction>
。
我已经阅读了几个类似this的类似问题,但却无法使其发挥作用。
简单示例如下:
test.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>
testBean这个
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>
- 标签丢失了。此外,<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>