您的位置:首页 > Web前端 > JavaScript

JSF页面传参数到后台bean的四种方式

2014-10-21 13:57 357 查看
有四种方法可以把JSF页面的参数传到后台Bean:
Method expression (JSF 2.0)
f:param
f:attribute
f:setPropertyActionListener

我们一个一个的来看例子

1. Method expression

JSF 2.0后的版本允许你通过方法表达式(Method expression)的方式来传参数,像这样{bean.method(param)}.

JSF page…

<h:commandButton action="#{user.editAction(delete)}" />


Backing bean…

@ManagedBean(name="user")
@SessionScoped
public class UserBean{

public String editAction(String id) {
//id = "delete"
}

}


2. f:param

用f:param标签传参数,backing bean通过request parameter获取传过来的参数。

JSF page…

<h:commandButton action="#{user.editAction}">
<f:param name="action" value="delete" />
</h:commandButton>


Backing bean…

@ManagedBean(name="user")
@SessionScoped
public class UserBean{

public String editAction() {

Map<String,String> params =
FacesContext.getExternalContext().getRequestParameterMap();
String action = params.get("action");
//...

}

}


 

3. f:atribute

用f:atribute标签,后台通过action listener获取.

JSF page…

<h:commandButton action="#{user.editAction}" actionListener="#{user.attrListener}">
<f:attribute name="action" value="delete" />
</h:commandButton>


Backing bean…

@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标签传参数,它会直接把参数值设置到backing bean对应的属性。

JSF page…

<h:commandButton action="#{user.editAction}" >
<f:setPropertyActionListener target="#{user.action}" value="delete" />
</h:commandButton>


Backing bean…

@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"
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: