- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
@ViewScoped calls @PostConstruct on every postback request
(1 个回答)
5年前关闭。
我在 JSF 2.2.7 和 Primefaces 5 中打开对话框时遇到问题。我有一个打开对话框的按钮,问题是每次单击按钮时都会执行 @PostConstruct 方法。为什么?
我只想调用 @PostConstruct 1 次,但我不想将范围更改为 Session(使用 @SessionScope 注释它可以完美运行)。
这是我的观点:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form id="f1">
<p:dialog widgetVar="trainingDialog2" id="d1">
<h:outputText value="#{userViewBean.errorMessage}" />
</p:dialog>
<br />
<p:dataTable id="dt1" value="#{userViewBean.infoList}" var="item">
<p:column>
<p:commandButton id="btn" update=":f1:d1"
oncomplete="PF('trainingDialog2').show()"
styleClass="ui-icon ui-icon-calendar">
<f:setPropertyActionListener value="#{item.id}"
target="#{userViewBean.errorMessage}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
package pl.jrola.java.www.vigym.viewcontroller.beans.userview;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
@ManagedBean(name = "userViewBean")
@ViewScoped
public class UserViewBean implements Serializable {
private static final long serialVersionUID = 6994205182090669165L;
private String errorMessage;
private List<UserProfileInfoBean> infoList;
public List<UserProfileInfoBean> getInfoList() {
return infoList;
}
public void setInfoList(List<UserProfileInfoBean> infoList) {
this.infoList = infoList;
}
public UserViewBean() {
}
@PostConstruct
public void postConstruct() {
this.infoList = new ArrayList<UserProfileInfoBean>();
for (long i = 0; i < 3; i++) {
this.infoList.add(new UserProfileInfoBean(i));
}
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
最佳答案
您正在混合 JSF bean 和 CDI bean 的注释,有效地使 bean @RequestScoped,因为它是 @ManagedBean 的默认值。
如果您使用 JSF bean,请使用:
javax.faces.bean.ManagedBean;
javax.faces.bean.ViewScoped;
javax.inject.Named;
javax.faces.view.ViewScoped;
关于jsf - @ViewScoped 的 @PostConstruct 在每个请求上被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25462342/
我们正在将我们的项目迁移到针对 JBoss Wildfly 的 Java EE 7。 我们有几十个 @ManagedBean @javax.faces.bean.ViewScoped (旧的非 CDI
引自此 nice article , The new view scope should solve exactly those issues. A @ViewScoped bean will liv
我已经使用 JSF 很多年了,在下一个项目中,我们的目标是使 Web 层尽可能无状态。我正在探索的一种可能性是删除 @ViewScoped bean 类支持@RequestScoped (根据需要加上
这个问题在这里已经有了答案: JSF 2.1 ViewScopedBean @PreDestroy method is not called (2 个回答) 2年前关闭。 我有一个 @ViewScop
我有一个按钮,点击那个按钮我有客户端代码来设置 viewScope 值: var val = 'TEST_VALUE'; "#{javascript: viewScope.testVal =
我正在使用 JSF 构建一个单页应用程序,如下所示: 左侧的菜单是动态生成的,单击其中一个子菜单会创建一个选项卡,我在其中显示 中的内容。 . 菜单和选项卡代码:
在应用程序中,我需要知道 viewScope 变量是否已初始化。创建 viewScope 变量时,该值可能为 null。所以 viewScope.isEmpty("SomeName") 并没有告诉我它
我有一个按钮,点击那个按钮我有客户端代码来设置 viewScope 值: var val = 'TEST_VALUE'; "#{javascript: viewScope.testVal =
我正在使用带有多个和 fileUploadListener 方法的 primefaces fileUpload。每次上传的每个文件都会调用监听器,我想将每个文件存储在 arrayList 中,并在上传
如果我们没有相关页面所需的信息,我在从 View 作用域 bean 进行重定向时遇到问题。 @PostContruct 中的日志条目在与尝试呈现自身而不是遵循我的重定向的 View 相关的 NPE 之
此代码总是抛出 ViewExpiredException @ManagedBean(name = "test") @ViewScoped public class test implements Se
我要将一个参数从一个页面 (Facelet) 传递到范围为 View 范围的托管 Bean。 我试着这样做: import javax.faces.bean.ManagedBean; import j
我正在使用 JSF 2.0 和 Primefaces 3.4.2,我有一个使用延迟加载填充的数据表。 当我查看 managedbean 的范围时,数据表 selectedRow 给出空指针异常。如果我
使用下面的代码,我在 selectOneRadio 上使用监听器重定向到 url 中包含查询字符串的页面。 问题是,当我被重定向时,newsTitle 和 selectedNews 的值为空。为什么是
我已经看到有关调用 bean 构造函数和 ViewScope 的其他问题,但我仍然遇到困难。我看到的问题涉及我的应用程序中的两个特定页面。第一个是数据表(现在它填充了随机生成的数据,但最终会调用数据库
我想知道在两个 ViewScoped bean 之间传递数据(对象)的最佳实践是什么。 由于出色解释的问题,它们需要在 View 范围内here (简而言之:在这两个 View 中,我都在 h:dat
我的 h:commandButton "Login"有问题:当我使用 @ViewScoped 并按下此按钮时,会出现 ViewExpiredException,但是当我使用 @SessionScope
这个问题在这里已经有了答案: @ViewScoped calls @PostConstruct on every postback request (1 个回答) 5年前关闭。 我在 JSF 2.2.
在 Spring 3 和 JSF 2.0 中,当我们想要使用 JSF View 范围时,我们必须引入自定义 ViewScope 实现并指示 Spring 通过 CustomScopeConfigure
我在 jsf facelet 上有一个日历、编辑器、fileUpload 和一个 dataTable primefaces 控件。 代码如下, #{lbl.SSTitle}
我是一名优秀的程序员,十分优秀!