ViewScoped bean being initialized multiple times with includeViewParams="true"

huangapple go评论85阅读模式
英文:

ViewScoped bean being initialized multiple times with includeViewParams="true"

问题

我目前正在将一个使用JSF开发的Web应用程序从Wildfly 26迁移到Wildfly 28。所有的代码都已重构为jakarta.*,并且我在使用ViewScoped bean时遇到了一些奇怪的行为。

在包含<h:link ... includeViewParams="true"/>的页面上,相应的后台bean会被多次初始化。

页面Bean

  1. package mypackage;
  2. import java.io.Serializable;
  3. import jakarta.annotation.PostConstruct;
  4. import jakarta.faces.view.ViewScoped;
  5. import jakarta.inject.Named;
  6. @Named
  7. @ViewScoped
  8. public class PageOne implements Serializable {
  9. private static final long serialVersionUID = 1L;
  10. private String page = null;
  11. private String search = null;
  12. @PostConstruct
  13. private void init() {
  14. System.out.println("Page One PostConstruct");
  15. }
  16. public String getValue() {
  17. return "pageTwo";
  18. }
  19. public String getPage() {
  20. return page;
  21. }
  22. public void setPage(String page) {
  23. this.page = page;
  24. }
  25. public String getSearch() {
  26. return search;
  27. }
  28. public void setSearch(String search) {
  29. this.search = search;
  30. }
  31. }

页面One XHTML

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:f="jakarta.faces.core"
  4. xmlns:ui="jakarta.faces.facelets"
  5. xmlns:h="jakarta.faces.html"
  6. xmlns:a="jakarta.faces.passthrough">
  7. <f:metadata>
  8. <f:viewParam name="page" value="#{pageOne.page}" />
  9. <f:viewParam name="search" value="#{pageOne.search}" />
  10. </f:metadata>
  11. <body>
  12. <main>
  13. <div>
  14. <h:link value="#{pageOne.value}" outcome="pageTwo?faces-redirect=true" includeViewParams="true" />
  15. </div>
  16. </main>
  17. </body>
  18. </html>

页面Two XHTML

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:f="jakarta.faces.core"
  4. xmlns:ui="jakarta.faces.facelets"
  5. xmlns:h="jakarta.faces.html"
  6. xmlns:a="jakarta.faces.passthrough">
  7. <f:metadata>
  8. <f:viewParam name="page" value="#{pageOne.page}" />
  9. <f:viewParam name="search" value="#{pageOne.search}" />
  10. </f:metadata>
  11. <body>
  12. <main>
  13. <div>Page Two</div>
  14. </main>
  15. </body>
  16. </html>

当访问页面One时,页面bean会被多次实例化。

相同的代码在Wildfly 26上按预期工作(只创建一个实例)。

即使使用@org.omnifaces.cdi.ViewScoped注解,问题仍然存在。

是否有人曾经遇到类似的问题?

感谢您的帮助!

英文:

I'm currently migrating an web application developed in JSF from Wildfly 26 to Wildfly28.
All the code was refactored to jakarta.* and I'm experiencing some strange behaviour with ViewScoped beans.

On pages containing &lt;h:link ... includeViewParams=&quot;true&quot;/&gt; the corresponding backing bean is being initialized multiple times.

Page Bean

  1. package mypackage;
  2. import java.io.Serializable;
  3. import jakarta.annotation.PostConstruct;
  4. import jakarta.faces.view.ViewScoped;
  5. import jakarta.inject.Named;
  6. @Named
  7. @ViewScoped
  8. public class PageOne implements Serializable {
  9. private static final long serialVersionUID = 1L;
  10. private String page = null;
  11. private String search = null;
  12. @PostConstruct
  13. private void init() {
  14. System.out.println(&quot;Page One PostConstruct&quot;);
  15. }
  16. public String getValue() {
  17. return &quot;pageTwo&quot;;
  18. }
  19. public String getPage() {
  20. return page;
  21. }
  22. public void setPage(String page) {
  23. this.page = page;
  24. }
  25. public String getSearch() {
  26. return search;
  27. }
  28. public void setSearch(String search) {
  29. this.search = search;
  30. }
  31. }

Page One XHTML

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
  3. xmlns:f=&quot;jakarta.faces.core&quot;
  4. xmlns:ui=&quot;jakarta.faces.facelets&quot;
  5. xmlns:h=&quot;jakarta.faces.html&quot;
  6. xmlns:a=&quot;jakarta.faces.passthrough&quot;&gt;
  7. &lt;f:metadata&gt;
  8. &lt;f:viewParam name=&quot;page&quot; value=&quot;#{pageOne.page}&quot; /&gt;
  9. &lt;f:viewParam name=&quot;search&quot; value=&quot;#{pageOne.search}&quot; /&gt;
  10. &lt;/f:metadata&gt;
  11. &lt;body&gt;
  12. &lt;main&gt;
  13. &lt;div&gt;
  14. &lt;h:link value=&quot;#{pageOne.value}&quot; outcome=&quot;pageTwo?faces-redirect=true&quot; includeViewParams=&quot;true&quot; /&gt;
  15. &lt;/div&gt;
  16. &lt;/main&gt;
  17. &lt;/body&gt;
  18. &lt;/html&gt;

Page Two XHTML

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
  3. xmlns:f=&quot;jakarta.faces.core&quot;
  4. xmlns:ui=&quot;jakarta.faces.facelets&quot;
  5. xmlns:h=&quot;jakarta.faces.html&quot;
  6. xmlns:a=&quot;jakarta.faces.passthrough&quot;&gt;
  7. &lt;f:metadata&gt;
  8. &lt;f:viewParam name=&quot;page&quot; value=&quot;#{pageOne.page}&quot; /&gt;
  9. &lt;f:viewParam name=&quot;search&quot; value=&quot;#{pageOne.search}&quot; /&gt;
  10. &lt;/f:metadata&gt;
  11. &lt;body&gt;
  12. &lt;main&gt;
  13. &lt;div&gt;Page Two&lt;/div&gt;
  14. &lt;/main&gt;
  15. &lt;/body&gt;
  16. &lt;/html&gt;

When accessing page one, the page bean is instanciated multiple times.

The same code works as expected on Wildfly 26 (only one instance is created).

Even using @org.omnifaces.cdi.ViewScoped annotation, the issue remains.

Has anyone ever had a similar issue?

Thanks for your help!

答案1

得分: 0

Mojarra 4.0.3上的问题已解决。

在将Mojarra从4.0.2更新到4.0.3后,成功在Wildfly 29.0.0 Final上进行了测试。

https://github.com/eclipse-ee4j/mojarra/issues/5290

英文:

Issue fixed on Mojarra 4.0.3

Tested successfully on Wildfly 29.0.0 Final after updating Mojarra from 4.0.2 to 4.0.3

https://github.com/eclipse-ee4j/mojarra/issues/5290

huangapple
  • 本文由 发表于 2023年5月30日 00:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定