jsf - 将值从页面传递到其他页面 JSF

标签 jsf jsf-2

我是 java server faces (JSF) 的初学者,我需要将文本输入的内容传递到第二页以显示它,同样适用于第二页:我想将单选按钮值传递到第三页。我搜索并尝试了很多但没有成功。 例如我试过

 <h:commandButton value="Next" action="#{myBean.execute(input_id.value)}"/>

执行方法是:

public void execute(String value) {
// ...

    try{
         FacesContext.getCurrentInstance().getExternalContext().dispatch("/Quizy.xhtml?faces-redirect=true");
    }
    catch(Exception e){
        System.out.println("err");    
    }  
}

有什么建议吗?

最佳答案

这里有 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:attribute 标签传递参数值,并通过backing bean 中的action listener 取回。

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

}

关于jsf - 将值从页面传递到其他页面 JSF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37490853/

相关文章:

java - faces-config 导致启动 Web 应用程序时出现异常

javascript - JSF setPropertyActionListener 设置 javascript 整数值

jsf - Primefaces 消息用于验证,其余部分则咆哮

ajax - h :graphicImage is not downloaded when its value is updated through f:ajax

jsf - 检测 JSF session 超时/刷新/后退/前进并返回登录屏幕

java - 丰富的 :column sortBy doesn't sort properly

java - 根据从另一个数据表中选择记录的复选框来填充数据表

java - 如何让 JSF2 与 Seam2 一起工作

jsf-2 - 数据表中输入字符串的 NumberFormatException

spring - p :remoteCommand only update ="@all" PF5