JSF頁面傳引數到後臺bean的四種方式
阿新 • • 發佈:2019-02-09
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" } }