这里有 4 种其他方法可以将参数值从 JSF 页面传递到其他页面 JSF:
1- Method expression (JSF 2.0)
2- f:param
3- f:attribute
4- f:setPropertyActionListener
1.方法表达式
从 JSF 2.0 开始,您可以像 #{bean.method(param)} 这样在方法表达式中传递参数值。
JSF 页面
<h:commandButton action="#{user.editAction(delete)}" />
ManagedBean
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String editAction(String id) {
//id = "delete"
}
}
2- f:param
通过 f:param 标签传递参数值,并通过 backing bean 中的请求参数取回。
JSF 页面
<h:commandButton action="#{user.editAction}">
<f:param name="action" value="delete" />
</h:commandButton>
ManagedBean
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String editAction() {
Map<String,String> params =
FacesContext.getExternalContext().getRequestParameterMap();
String action = params.get("action");
//...
}
}
3。 f:属性
通过 f:atribute 标记传递参数值,并通过支持 bean 中的动作侦听器取回。
JSF 页面
<h:commandButton action="#{user.editAction}" actionListener="#{user.attrListener}">
<f:attribute name="action" value="delete" />
</h:commandButton>
ManagedBean
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
String action;
//action listener event
public void attrListener(ActionEvent event){
action = (String)event.getComponent().getAttributes().get("action");
}
public String editAction() {
//...
}
}
4. f:setPropertyActionListener
通过 f:setPropertyActionListener 标签传递参数值,它会将值直接设置到您的支持 bean 属性中。
JSF 页面
<h:commandButton action="#{user.editAction}" >
<f:setPropertyActionListener target="#{user.action}" value="delete" />
</h:commandButton>
ManagedBean
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String action;
public void setAction(String action) {
this.action = action;
}
public String editAction() {
//now action property contains "delete"
}
}