在jsf页面和托管bean之间,为什么两次调用getter方法 [英] Between a jsf page and a managed bean, why the getter method is called twice

查看:146
本文介绍了在jsf页面和托管bean之间,为什么两次调用getter方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jsf页面,其中的表单中有一个outputtext.从支持Bean(或托管Bean)中调用outputtext组件的值.我知道将其编码为#{MyBean.myString} Jsf将其重命名并调用getMyString()方法.但是,更奇怪的是,当我在该组件的getter方法上设置一个断点时,我看到在页面呈现期间它被调用了两次. outputtext格式为h:form,它是绑定到backingbean的唯一组件.我的意思是,太奇怪了,jsf初次使用getter方法时应该获取值,但是它需要两次使用getter方法. 您能解释一下jsf中这种行为的原因吗?

I have a jsf page with a form has an outputtext in it. The value of outputtext component is called from a backing bean (or managed bean). I know when I code it as #{MyBean.myString} Jsf rename it and calls getMyString() method. However the wierd thing is, when I put a breakpoint to the getter method of this component, I see it is called twice during the page is being rendered. The outputtext is in a h:form, and it is the only component wich is bind to a backingbean. I mean, it is so wierd that jsf should get the value when it first come to the getter method, however it needs to go to the getter method twice. Can you explain what is the reason of this behaviour in jsf?

任何帮助,我们将不胜感激, 最好的祝愿, 巴里斯

Any help would be appreciated, Best wishes, Baris

推荐答案

getter就像它的名字已经被自我描述了一样,它的存在纯粹是为了检索数据. JSF不缓存此数据.它将在需要时调用它.实际上,调用吸气剂的成本是虚无的-除非您要做的事情不仅仅是返回数据,例如每次访问数据库时,都应将此逻辑移出getter或转换为延迟加载.

The getter, as its name already self-describes, is just there with the pure purpose to retrieve the data. JSF doesn't cache this data. It will call it whenever needed. The cost of calling a getter is in practice nihil --unless you do something more than returning the data, e.g. hitting the DB everytime, this logic should then be moved out of the getter or be turned into lazy loading.

在提交表单的情况下,通常会在验证阶段触发第一个get调用,以检查是否存在任何初始值,以便JSF可以处理值更改事件.通常在渲染响应阶段触发第二个调用,以在视图中显示模型值.

In case of a form submit, the first get call is usually fired during validations phase to check if there is any initial value so that JSF can handle the value change event. The second call is usually fired during render response phase to display the model value in the view.

您可能会发现这篇文章对了解有关JSF生命周期的更多信息.您可能会发现此答案对于了解有关以下方法的更多信息很有用在支持bean中进行预处理/初始化.

You may find this article useful as well to learn more about the JSF lifecycle. You may find this answer useful to learn more about ways to do preprocessing/initialization in a backing bean.

这篇关于在jsf页面和托管bean之间,为什么两次调用getter方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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