JSTL c:forEach causes @ViewScoped bean to invoke @PostConstruct on every request

℡╲_俬逩灬. 提交于 2019-11-27 22:27:12
BalusC

This problem has the same grounds as your previous question which is answered here: Why does @PostConstruct callback fire every time even though bean is @ViewScoped? JSF.

This is indeed a bug in JSF2. It's a chicken-egg issue. The view scoped beans are stored in the JSF view state. So the view scoped beans are only available after restore view phase. However, JSTL tags runs during restore view phase, while the view scoped beans are not available yet. This causes creation of a brand new view scoped bean instance, which is then later replaced by the real view scoped bean which was stored in the restored JSF view state.

This is reported as JSF issue 1665 and JSF spec isssue 787 which is fixed for JSF 2.2 and ported back in Mojarra 2.1.18. So, just upgrading to a minimum of Mojarra 2.1.18 should do.

If you can't upgrade, then your best bet is to use JSTL tags on request/session/application scoped beans exclusively, or to look for alternate ways for the particular functional requirement. You can replace the <c:forEach> with <ui:repeat> as you already found out.

See also:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!