JSP Bean通信 [英] JSP Beancommunication
问题描述
我正在尝试为评估创建JSP方面. 目前,我对一些简单的问题有些困惑:
I'm trying to create a JSP side for my assessment. At present I'm a little stuck over some simple problems:
我有一个LoginBean和一个ActionBean.
I have a LoginBean, and a ActionBean.
对于ActionBean的一种方法,我需要用户名,该用户名存储在LoginBean中.
For one Method of the ActionBean I need the Username, that is stored in the LoginBean.
我已经将Loginbean设置为会话作用域.
I already set the Loginbean to session scope.
问题:
如何从ActionBean的LoginBean对象获取Username变量?
How to get the Username variable from the LoginBean Object at the ActionBean?
我尝试过的事情:
FacesContext context= FacesContext.getCurrentInstance();
context.getAttributes().put("Username",Usernamevariable);
Bean中的
,Loginpage用于检查用户是否可以登录 和
in the Bean which is used by the Loginpage to check if the user can login and
Username=context.getAttributes().get("Username").toString();
在想要执行需要用户名的操作的Bean中.
in the Bean that wants to performe the Action for which the Username is needed.
第二个想法没有用,因为它是到不同的页面,因此上下文是不同的.
On second thought that did not work, because it are to different pages so the context is different.
什么是可行的解决方案?
What would a working solution?
推荐答案
这在ActionBean中应该可以完成
This in ActionBean should do the job
LoginBean loginBean= (LoginBean) FacesContext.getCurrentInstance()
.getApplication()
.evaluateExpressionGet(facesContext, "#{LoginBean}", LoginBean.class);
// after that you can do sth like this
String username = loginBean.getUsername();
这篇关于JSP Bean通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!