jsf2.0-如何在请求范围内获取其他jsf页面的bean中的值 [英] jsf2.0 - How to get the values in other jsf page's bean in request scope

查看:119
本文介绍了jsf2.0-如何在请求范围内获取其他jsf页面的bean中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个页面myaccount.xhtml和selectbank.xhtml 在我的帐户页面中,有一个充值帐户选项,当用户按下提交"按钮时,用户将输入金额,然后使用以下bean方法进入选择银行页面.

I have two pages myaccount.xhtml and selectbank.xhtml In my account page there is one option for recharge account in which user will enter the amount when user will press submit button then it will goto the select bank page using following bean method.

public String gotoPayMethod() {
    FacesMessage doneMessage=new FacesMessage("Redirecting to Payment Type Page");
    FacesContext.getCurrentInstance().addMessage(null, doneMessage);
    return "SelectBank";
}

当用户转到selectbank时,用户必须提交付款方式,但是在此页面中,该值将空值显示为上一页中输入的值. 这两个页面都使用相同的bean,并且bean的范围是请求范围.

When user will goto to selectbank there user will have to submit payment method but in this page it shows the amount as null which was entered in the previous page. Both the pages are using the same bean and the scope of the bean is request scope.

因此,如何在不通过URL GET方法传递此值的情况下访问该值. 令我满意的是,我使用了会话作用域,然后它才起作用,但是我知道那不是正确的方法,因为我开始为每个页面使用会话作用域,否则效率不高. 谢谢

So how can I access that value without passing this values through URL GET method. Just for my satisfaction I used session scope then it was working but I know thats not the proper way because I start using session scope for each pages then it will not be efficient. Thanks

推荐答案

好吧,如果您的bean是RequestScoped,那么您在两个页面上都没有相同的bean.将为每个请求重新创建这些bean,因此您应该传递参数.将您的gotoPayMethod()的退货声明更改为:

Well, if your beans are RequestScoped than you don't have same bean for both pages. These beans are recreated for every request, so you should pass parameters. Change return statement of your gotoPayMethod() to:

return "SelectBank?faces-redirect=true&includeViewParams=true";

,然后在selectbank.xhtml上添加:

<f:metadata>
  <f:viewParam name="amount" value="#{bean.amount}" />
</f:metadata>

使它适应您的属性和bean名称.

Adapt this to your property and bean name.

如果使用参数不是解决方案,则可以在会话中添加此参数,并在检索它时将其从第二个bean的会话中删除:

If using parameters is not a solution you can add this parameter in the session, and remove it from session in second bean when you retrieve it:

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("amount", amount);
((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession().removeAttribute("amount");

由于从getSessionMap()返回的Map是不可变的,因此有必要使用第二种构造来删除属性.

Second construction for removing the attribute is necessary as Map returned from getSessionMap() is immutable.

这篇关于jsf2.0-如何在请求范围内获取其他jsf页面的bean中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆