【问题标题】:Passing Values from page to other page JSF将值从页面传递到其他页面 JSF
【发布时间】:2016-05-27 19:19:23
【问题描述】:

我是 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");    
    }  
}

有什么建议吗?

【问题讨论】:

  • 将值设置为您的经理或 cdi bean 之一的属性。

标签: jsf jsf-2


【解决方案1】:

这里有 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"
    }   

}

【讨论】:

【解决方案2】:

有几种方法可以做到这一点,但这里是其中之一。 您需要将 inputText 值保存到 bean 的属性中,并且您的 h:inputTexth:commanButton 应该在同一个 h:form 元素中

这是一个示例代码

在你看来

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

如果您希望您的属性 (someValue) 在不同的页面中可用,您的托管 bean 至少应该是 会话范围。托管 bean 的内容也应如下所示:

private String someValue;

// Getter and setter for `someValue`

public String execute() {
    // ...
    return "/Quizy.xhtml?faces-redirect=true";
}

如果要在第二页中检索该值,只需使用#{myBean.someValue}

【讨论】:

  • 但我认为 execute() 应该返回 String 而不是 void !
  • 不客气。我将编辑我的答案以执行 return String
【解决方案3】:

要完成此操作,您只需在此处将您的组件的 Value inputText 或 radioButton 设置为 Managed bean 或 Cdi beanProperty /strong> 在页面上调用当然你不会忘记在你的 bean 中为你的属性设置 getter 和 setter 方法。最后确保 Ur bean 的范围允许它在整个会话中处于活动状态(及其所有属性的值)。然后,您可以从您的最终页面调用您的 Managed bean 或 Cdi bean 属性作为页面组件的值

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 2011-10-13
    • 1970-01-01
    • 2016-01-24
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多