标签: view-scope

为什么@PostConstruct回调每次都会触发,即使bean是@ViewScoped?JSF

我在页面上使用datatable并使用绑定属性将其绑定到我的支持bean.这是我的代码: -

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
            <h:form prependId="false">

                <h:dataTable var="item" value="#{testBean.stringCollection}" binding="#{testBean.dataTable}">
                    <h:column>
                        <h:outputText value="#{item}"/>
                    </h:column>
                    <h:column>
                        <h:commandButton value="Click" actionListener="#{testBean.action}"/>
                    </h:column>
                </h:dataTable>

            </h:form>

    </h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是我的豆子: -

package managedBeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlDataTable;

@ManagedBean(name="testBean")
@ViewScoped
public class testBean implements Serializable {

    private List<String> stringCollection;

    public List<String> getStringCollection() {
        return stringCollection;
    }

    public void …
Run Code Online (Sandbox Code Playgroud)

jsf facelets postconstruct view-scope

32
推荐指数
1
解决办法
2万
查看次数

@ViewScoped在每个回发请求上调用@PostConstruct

这似乎不对.我正在清理我的代码,我只是注意到了这一点.每个ajax请求都会触发构造函数和@PostConstruct我的@ViewScopedbean.即使是简单的数据库分页也会触发它.

知道@ViewScoped@RequestScoped任何请求都要重建并且不应该重建.只有在通过GET重新加载完整页面之后.

jsf constructor postconstruct jsf-2 view-scope

28
推荐指数
1
解决办法
2万
查看次数

如何在JSF 2.0/2.1中用CDI替换@ManagedBean/@ViewScope

我目前正在使用RichFaces评估Java EE 6/JSF 2.1.

声明为的bean

@ManagedBean
@ViewScoped
Run Code Online (Sandbox Code Playgroud)
  1. 获取ID集(以准备例如删除操作).
  2. 通过JSF显示确认弹出窗口.
  3. 如果用户确认,则调用delete方法并删除在步骤1中存储了ID的行.

由于CDI bean没有ViewScope,我试图将bean声明为:

@Named
@ConversationScoped
Run Code Online (Sandbox Code Playgroud)

现在,处理在步骤3中失败,因为步骤1中设置的值(已选中)不再可用.

我必须使用Conversation.begin()Conversation.end()方法吗?

如果是这样,哪里可以调用它们的好地方?

jsf cdi jsf-2 view-scope

24
推荐指数
2
解决办法
2万
查看次数

JSF 2.x @ViewScoped托管bean线程是否安全?

我一直在谷歌搜索几个小时在这个问题上没有评估.

关于所提供示波器的线程安全性,WELD文档和CDI规范非常明确.

例如:

  • 适用范围 - 不安全

  • 会话范围 - 不安全

  • 请求范围 - 安全,始终绑定到单个线程

  • 会话范围 - 安全(由于WELD代理序列化来自多个请求线程的访问)

我在JSF 2.x定义的View Scope上找不到任何东西.

它与对话范围大致相同,因为尽管绑定到单个视图/用户,但多个请求很可能同时命中范围.我不知道的是JSF实现是否从多个请求序列化对bean的访问.

任何人都知道规格或Morraja/MyFaces的实施可以解决这个问题吗?

concurrency jsf cdi jsf-2 view-scope

17
推荐指数
1
解决办法
4942
查看次数

在不使用GET参数的情况下在@ViewScoped bean之间传递对象

我有一个browse.xhtml浏览列表的地方cars,我想在details.xhtml按下"查看更多"按钮时查看汽车的详细信息.他们的支持豆分别@ViewScoped被称为BrowseBeanDetailsBean.

现在,我不希望用户/客户端在URL中看到汽车ID,因此我想避免使用GET参数,如此此处所示.

有没有办法实现这个目标?我正在使用Mojarra 2.2.8和PrimeFaces 5以及OmniFaces 1.8.1.

jsf parameter-passing omnifaces view-scope jsf-2.2

16
推荐指数
1
解决办法
2万
查看次数

如何以及何时在JSF中销毁@ViewScoped bean?

所述的生命周期 @RequestScoped@SessionScopedBean 管理豆由Servlet容器自身管理,因为它们是基本上作为一个属性存储HttpRequestHttpSession分别.JSF如何管理生命周期@ViewScopedBean?我知道它是在创建视图时创建的,并且可以使用,直到有回发到另一个视图.但是我发现在我们离开那个视图后不会立即收集垃圾.

jsf destroy jsf-2 managed-bean view-scope

15
推荐指数
1
解决办法
2万
查看次数

即使ManagedBean已经被实例化(例如,在AJAX调用上),也会调用@PostConstruct方法

我有一个@ViewScope ManagedBean和一个@PostConstruct初始化方法.创建新实例时会调用此方法,但也会在每次调用ajax时调用此方法.为什么会这样?

在AJAX调用中,调用并执行init-Method,但不会看到任何更改.例如,如果我在init-Method中更改属性,则仅在实例化时可见,而不是在AJAX调用中可见.对于AJAX调用,值更改在@ViewScoped Bean中不是持久的.

任何人都可以告诉为什么会这样吗?我怎么能改变这个?

ajax jsf postconstruct jsf-2 view-scope

10
推荐指数
1
解决办法
9848
查看次数

如何检测和删除(在会话期间)未使用垃圾的未使用的@ViewScoped bean

编辑:这个问题提出的问题在codebulb.ch的这篇文章中得到了很好的解释和证实,包括JSF @ViewScoped,CDI @ViewSCoped和Omnifaces 之间的一些比较@ViewScoped,以及JSF @ViewScoped"设计漏洞" 的明确声明:5月24日, 2015 Java EE 7 Bean范围比较了2的第2部分


编辑:2017-12-05用于这个问题的测试用例仍然非常有用,但原始帖子(和图像)中关于垃圾收集的结论是基于JVisualVM,我发现它们无效.请改用NetBeans Profiler!现在我正在从NetBeans配置,而不是JVisualVM连接到GlassFish /似鲭水狼牙鱼,在这里我感到场得到引用(甚至@PreDestroy称为后)仍持有中强制GC的OmniFaces ViewScoped完全一致的结果与试验应用sessionListeners型的com.sun.web.server.WebContainerListenerContainerBase$ContainerBackgroundProcessor,他们不会GC.


众所周知,在JSF2.2中,对于使用@ViewScoped bean的页面,使用以下任何一种技术导航(或重新加载)将导致@ViewScoped bean在会话中"悬空"的实例它不会被垃圾收集,导致堆内存不断增长(只要GET引发):

  • 使用h:链接获取新页面.

  • 使用h:outputLink(或HTML A标记)获取新页面.

  • 使用RELOAD命令或按钮在浏览器中重新加载页面.

  • 使用键盘重新加载页面在浏览器URL上输入ENTER(也是GET).

相比之下,通过使用一个h:commandButton来传递JSF导航系统会导致@ViewScoped bean的发布,从而可以对其进行垃圾回收.

这是由(由BalusC)在JSF 2.1上解释的.ViewScopedBean @PreDestroy方法未被我的小型NetBeans示例项目/sf/answers/2128728101/在JSF2.2和Mojarra 2.2.9中调用和演示,其中项目说明了各种导航案例,可在此处下载.(编辑:2015-05-28:完整代码现在也可以在下面找到.)

[编辑:2016-11-13现在还有一个改进的测试网络应用程序,包含完整的说明,@ViewScoped并在GitHub上与OmniFaces 和结果表进行比较:https://github.com/webelcomau/JSFviewScopedNav]

我在这里重复一个index.html的图像,它总结了导航案例和堆内存的结果:

在此输入图像描述

问:如何检测由GET导航引起的这种"悬挂/悬挂"@ViewScoped bean并将其删除,或以其他方式呈现垃圾收集?

请注意,我不会在会话结束时询问如何清理它们,我已经看到了各种解决方案,我正在寻找在会话期间清理它们的方法,以便在会话期间堆内存不会过度增长由于无意中的GET导航.


jsf garbage-collection heap-memory jsf-2 view-scope

10
推荐指数
1
解决办法
3547
查看次数

链接的ViewScoped bean导致内存泄漏

在JBoss 7.1.1上的JavaEE6项目(EJB3,JSF2)中,似乎我们有@ViewScoped bean的内存泄漏.最后一天,我花时间在这个问题调查上.所以我创建了两个页面的简单项目,以保证在第一页离开@ViewScoped bean之后将被释放.

<context-param>  //web.xml
   <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
   <param-value>server</param-value>
</context-param>
<context-param>
   <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
   <param-value>false</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

TreeBean.java

@ManagedBean
@ViewScoped
public class TreeBean implements Serializable {
 private TreeNode root;  
 public static AtomicInteger count = new AtomicInteger(0);

@Override
protected void finalize() throws Throwable {
    System.out.println("TreeBean beans count: " + count.decrementAndGet() + " (FINALISATION)");
}


public TreeBean() {  
    super();
    System.out.println("TreeBean beans count: " + count.incrementAndGet() + " (INITIALISATION)");
}  
Run Code Online (Sandbox Code Playgroud)

first.xhtml

  ....
  <h:form id="frm">
        <p:tree
            value="#{treeBean.root}"
            var="node"
            id="tree">
    ....
   <p:commandLink
            action="second.xhtml?faces-redirect=true"
            value="toSecond" />
    ....            
Run Code Online (Sandbox Code Playgroud)

second.xhtml

  ....
  <h:form …
Run Code Online (Sandbox Code Playgroud)

out-of-memory jsf-2 view-scope

9
推荐指数
1
解决办法
4267
查看次数

ViewExpiredException:找不到保存的视图状态:在JSF中提交表单时

我在尝试提交表单时遇到以下异常.

javax.faces.application.ViewExpiredException: /page1.xhtml No saved view state could be found for the view identifier: /page1.xhtml
at org.apache.myfaces.lifecycle.RestoreViewExecutor.execute(RestoreViewExecutor.java:132)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.doFilter(AbstractPreAuthenticatedProcessingFilter.java:94)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at …
Run Code Online (Sandbox Code Playgroud)

primefaces jsf-2 viewexpiredexception view-scope notserializableexception

9
推荐指数
1
解决办法
2万
查看次数