我有以下示例代码.最初,只有commandButton Two可见.当我单击此按钮时,commandButton One也可见.但是当我单击One时,后备bean方法click1不会被触发.
以下是我的代码:
XHTML
<h:form id="form1">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button1" value="One" action="#{bean.click1}"
rendered="#{bean.show1}" />
</h:form>
<h:form id="form2">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button2" value="Two" action="#{bean.click2}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
支持bean
@RequestScoped
@Named("bean")
public class JsfTrial implements Serializable {
private static final long serialVersionUID = 2784462583813130092L;
private boolean show1; // + getter, setter
public String click1() {
System.out.println("Click1()");
return null;
}
public String click2() {
System.out.println("Click2()");
setShow1(true);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我找到了BalusC提供的非常丰富的答案.
如果我理解正确,我的问题是由于这个答案的第5点.
这是否也意味着我们不能将隐藏的commandButton与@RequestScoped支持bean一起使用?
您可以使用请求范围,只应将条件作为请求参数传递给后续请求,<f:param>而不是通过JSF隐藏输入字段<h:inputHidden>.隐藏输入字段的值仅在"更新模型值"阶段期间在模型中设置,而rendered属性的条件已在"应用请求值"阶段(之前)进行评估.
所以,使用<f:param>而不是<h:inputHidden>:
<h:form id="form1">
<h:commandButton id="button1" value="One" action="#{bean.click1}"
rendered="#{bean.show1}">
<f:param name="show1" value="#{bean.show1}" />
</h:commandButton>
</h:form>
<h:form id="form2">
<h:commandButton id="button2" value="Two" action="#{bean.click2}">
<f:param name="show1" value="#{bean.show1}" />
</h:commandButton>
</h:form>
Run Code Online (Sandbox Code Playgroud)
这样你就可以在bean的(post)构造函数中将它们作为请求参数提取出来.
public JsfTrial() {
String show1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("show1");
this.show1 = (show1 != null) && Boolean.valueOf(show1);
}
Run Code Online (Sandbox Code Playgroud)
丑陋,但CDI没有提供内置的注释,这是JSF的替代品@ManagedProperty("#{param.show1}").但是,您可以homegrow这样的注解.
| 归档时间: |
|
| 查看次数: |
2266 次 |
| 最近记录: |