英文:
ViewScoped bean being initialized multiple times with includeViewParams="true"
问题
我目前正在将一个使用JSF开发的Web应用程序从Wildfly 26迁移到Wildfly 28。所有的代码都已重构为jakarta.*
,并且我在使用ViewScoped
bean时遇到了一些奇怪的行为。
在包含<h:link ... includeViewParams="true"/>
的页面上,相应的后台bean会被多次初始化。
页面Bean
package mypackage;
import java.io.Serializable;
import jakarta.annotation.PostConstruct;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
@Named
@ViewScoped
public class PageOne implements Serializable {
private static final long serialVersionUID = 1L;
private String page = null;
private String search = null;
@PostConstruct
private void init() {
System.out.println("Page One PostConstruct");
}
public String getValue() {
return "pageTwo";
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getSearch() {
return search;
}
public void setSearch(String search) {
this.search = search;
}
}
页面One XHTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="jakarta.faces.core"
xmlns:ui="jakarta.faces.facelets"
xmlns:h="jakarta.faces.html"
xmlns:a="jakarta.faces.passthrough">
<f:metadata>
<f:viewParam name="page" value="#{pageOne.page}" />
<f:viewParam name="search" value="#{pageOne.search}" />
</f:metadata>
<body>
<main>
<div>
<h:link value="#{pageOne.value}" outcome="pageTwo?faces-redirect=true" includeViewParams="true" />
</div>
</main>
</body>
</html>
页面Two XHTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="jakarta.faces.core"
xmlns:ui="jakarta.faces.facelets"
xmlns:h="jakarta.faces.html"
xmlns:a="jakarta.faces.passthrough">
<f:metadata>
<f:viewParam name="page" value="#{pageOne.page}" />
<f:viewParam name="search" value="#{pageOne.search}" />
</f:metadata>
<body>
<main>
<div>Page Two</div>
</main>
</body>
</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 <h:link ... includeViewParams="true"/>
the corresponding backing bean is being initialized multiple times.
Page Bean
package mypackage;
import java.io.Serializable;
import jakarta.annotation.PostConstruct;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
@Named
@ViewScoped
public class PageOne implements Serializable {
private static final long serialVersionUID = 1L;
private String page = null;
private String search = null;
@PostConstruct
private void init() {
System.out.println("Page One PostConstruct");
}
public String getValue() {
return "pageTwo";
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getSearch() {
return search;
}
public void setSearch(String search) {
this.search = search;
}
}
Page One XHTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="jakarta.faces.core"
xmlns:ui="jakarta.faces.facelets"
xmlns:h="jakarta.faces.html"
xmlns:a="jakarta.faces.passthrough">
<f:metadata>
<f:viewParam name="page" value="#{pageOne.page}" />
<f:viewParam name="search" value="#{pageOne.search}" />
</f:metadata>
<body>
<main>
<div>
<h:link value="#{pageOne.value}" outcome="pageTwo?faces-redirect=true" includeViewParams="true" />
</div>
</main>
</body>
</html>
Page Two XHTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="jakarta.faces.core"
xmlns:ui="jakarta.faces.facelets"
xmlns:h="jakarta.faces.html"
xmlns:a="jakarta.faces.passthrough">
<f:metadata>
<f:viewParam name="page" value="#{pageOne.page}" />
<f:viewParam name="search" value="#{pageOne.search}" />
</f:metadata>
<body>
<main>
<div>Page Two</div>
</main>
</body>
</html>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论