使用 ManagedProperty 时遇到问题
我正在尝试在多个 SessionScoped bean 之间共享一个对象。但我收到错误,我真的不知道为什么。
@ManagedProperty(value="#{tb}")
private testBean tb;
我相信这是正确的语法,但任何像 tb.getName 这样的调用都会导致异常。
@ManagedBean(name = "tb")
public class testBean
{
private String name = "sumthing";
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
我是否完全误解了 ManagedProperty 的工作原理?
I'm trying to share an object between several SessionScoped beans. I get errors though and I really don't know why.
@ManagedProperty(value="#{tb}")
private testBean tb;
I believe that this is the right syntax, but any call like tb.getName
results in an exception.
@ManagedBean(name = "tb")
public class testBean
{
private String name = "sumthing";
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
Have I completely misunderstod how ManagedProperty works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能您的消费者类没有
tb
的 setter/getterProbably your consumer class doesn't have setters/getters for
tb
为什么不将
@SessionScoped
添加到您的 JavaBean 中?请参阅我对此SO问题的解释。其次,您不执行
#{tb.getName}
,而是使用 EL 表达式#{tb.name}
代替。Why not add
@SessionScoped
to your JavaBean? See my explanation to this SO Question.Secondly, you don't do
#{tb.getName}
, rather use EL Expression#{tb.name}
instead.您还可以查看 Flash 范围,如果您只想将值/对象从一个视图传递到另一个视图,并且不想给服务器带来会话状态的负担,则可以使用它。
有关示例,请参阅: http://jugojava.blogspot.com/ 2011/06/jsf2-flash-scope-example.html
You can also take a look at the Flash scope, the idea is to use this if you just want to pass values/objects from one view to another, and you don't want to burden the server with a session state.
For an example see: http://jugojava.blogspot.com/2011/06/jsf2-flash-scope-example.html