首页 \ 问答 \ 每次请求时,JSF ViewScoped bean都会调用3次(JSF ViewScoped beans called 3 times at every request)

每次请求时,JSF ViewScoped bean都会调用3次(JSF ViewScoped beans called 3 times at every request)

我在所有JSF bean中都有一个奇怪的行为(使用View,Request和Custom范围)。

在我的web.xml ,状态保存方法设置为client

无论我在托管bean中定义哪个范围,在每个GET请求中都会调用3次bean构造函数。 它适用于所有观点。

据我所知,一个bean被构造一次并放入一个范围。 有人想知道为什么JSF在渲染视图之前总是创建它们3次吗?

运行在Mojarra 2.1.13,PrimeFaces 3.5,JDK 1.6.x. 和Apache Tomcat 7.0.27谢谢!

谢谢。


I have a strange behavior in all of my JSF beans (with View, Request and Custom scopes).

In my web.xml the state saving method is set to client.

No matter which scope I define in my managed beans, at every GET request the bean constructor is called 3 times. It happens for all views.

As far as I know, a bean is constructed 1 time and put in a scope. Does anybody wonder why JSF is always creating them 3 times before rendering the view?

Running on Mojarra 2.1.13, PrimeFaces 3.5, JDK 1.6.x. and Apache Tomcat 7.0.27 Thanks!

Thanks.


原文:https://stackoverflow.com/questions/19326902
更新时间:2023-09-18 10:09

最满意答案

在您的示例中,您需要提供额外的空括号来初始化基类:

Foo f = {{}, 8};
Foo f{{}, 8};

一般而言,并非每个类都可以进行聚合初始化。 如果有类,则认为该类是聚合(参见源代码 ):

  • 没有私有或受保护的非静态数据成员
  • 没有用户提供的,继承的或显式的构造函数(允许显式默认或删除构造函数)
  • 没有虚拟,私有或受保护的基类
  • 没有虚拟成员功能

此外,没有“聚合构造函数”这样的东西,并且默认构造函数与聚合初始化无关。

除非using Base::Base来执行此操作,否则不会继承基类的构造函数。 复制和移动构造函数也不会被继承 。 它们由编译器为每个类自动生成,除非明确定义或隐式删除它们。


In your example you need to provide additional empty braces to initialize base class:

Foo f = {{}, 8};
Foo f{{}, 8};

Generally speaking though, not every class can be aggregate-initialized. The class is considered to be an aggregate if it has (see the source):

  • no private or protected non-static data members
  • no user-provided, inherited, or explicit constructors (explicitly defaulted or deleted constructors are allowed)
  • no virtual, private, or protected base classes
  • no virtual member functions

Also, there is no such thing as an "aggregate constructor" and a default constructor is not related to aggregate initialization in any way.

Constructors of base classes are not inherited unless you use using Base::Base to do so. Copy and move constructors are also not inherited. They are automatically generated by the compiler for each class unless they are explicitly defined or implicitly deleted.

相关问答

更多
  • 从QObject继承的所有类都可以通过parent()方法访问父类,在您的情况下,您的类继承自QWidget,QWidget继承自QObject,因此您的类也具有该方法。 因此您无需创建属性。 根据文件 : QObject * QObject :: parent()const 返回指向父对象的指针。 在你的情况下: void PopupServer::showPopup(const QString &text,const int &tim ) { QLabel qPopup= new QLabel; ...
  • 您只能初始化一次对象。 之后你只能分配它们: RandUniform::RandUniform() : downlimit(0), uplimit(1) { mean = 0.5; sigma = sqrt(1./12); } 或者,您可以将基类变量的初始化委托给基类构造函数: //Make it protected if you do not want end user to see it. RandomVar::RandomVar(double m, double s) : mean ...
  • “say”方法在父类上,而不在子类上。 因此,当它要求“消息”成员时,它会查看自己的成员,而不是孩子的成员。 通过子类进行调用的事实与其无关。 实际上,这里不会覆盖成员变量。 这是预期的行为。 编辑: Java语言规范说“如果类声明了一个具有特定名称的字段,那么该字段的声明就会隐藏在超类中具有相同名称的字段的任何和所有可访问声明,以及该类的超接口。” “继承”并不意味着“复制”。 当您在子实例上调用“say”方法时,它不是被调用方法的“代码副本”,而是父类的方法,因为它是在父类中定义的。 并且父类对子变量成 ...
  • 您不能这样做,基类构造函数首先在初始化顺序中执行,在数据成员初始化之前和执行派生类构造函数的主体之前执行。 如果这些是昂贵的计算,最好的办法可能是将它们移出构造函数。 编辑:技术上有一种解决此问题的方法,通过创建第二个构造函数,或者具有默认参数值的默认构造函数,可以用来停止基类中的计算,如下所示: struct SkipCalculatePrice {}; class Car { public: Car(); protected: Car(SkipCalculatePrice); }; ...
  • 在Java中,当您创建一个数组时,它会自动填充null值(除非您使用的是基元数组,在这种情况下该数组填充了零)。 你在做什么是访问一个空值,并试图获得它的一个领域。 你的代码实质上是执行null.name = "Rover" 。 将myDogs[0]设置为有效的实例,否则会得到NullPointerException。 你可以像这样在元素中创建一个Dog的新实例: myDogs[0] = new Dog(); 或者你可以在制作数组时这样做: Dog[] myDogs = {new Dog(), new D ...
  • 确实是非常奇怪的继承使用。 根据良好的面向对象设计原则,基类应该理想地定义接口并且尽可能地包含尽可能少或者没有状态。 这是可行的,因为你的foo()在每次被调用时重置A::s的值。 尝试打印A::s的地址。 只有一个对象。 如果你没有每次设置这个值,并且你有多个使用另一个成员函数bar()读取A :: s的值的对象,这将不起作用。 如果B和C对象也在单独的线程中创建,则可能会遇到同步问题。 你会以UB结束。 A very odd use of inheritance indeed. The base cla ...
  • 不,你不能在构造之后初始化const成员。 但是,不要忘记您可以在初始化列表中调用静态函数,因此在大多数情况下,您可以从初始化列表中初始化成员 class A { public: A(){} initialize(int x):c(computeC(x)) {} private: const int c; static int computeC(int){/*...*/} }; 您还可以为该成员定义特殊的getter,并使用它来访问成员。 class A { public: ...
  • 你在代码中注释/* cannot construct the object here */ ,但事实是成员是在输入复合语句之前构造的。 这是一个有效的实现吗? 我可以简单地将对象放入已由Element类构造分配的空间中吗? 不可以。必须首先销毁默认构造的成员,然后才能使用新的位置 - 除非该成员是可以轻易破坏的。 然而,这是毫无意义的。 无论你在复合语句中做什么,你也可以在初始化列表中做。 如果一个表达式不够,那么你可以简单地编写一个单独的函数,然后调用它。 UIElement init_uie(); // ...
  • 在您的示例中,您需要提供额外的空括号来初始化基类: Foo f = {{}, 8}; Foo f{{}, 8}; 一般而言,并非每个类都可以进行聚合初始化。 如果有类,则认为该类是聚合(参见源代码 ): 没有私有或受保护的非静态数据成员 没有用户提供的,继承的或显式的构造函数(允许显式默认或删除构造函数) 没有虚拟,私有或受保护的基类 没有虚拟成员功能 此外,没有“聚合构造函数”这样的东西,并且默认构造函数与聚合初始化无关。 除非using Base::Base来执行此操作,否则不会继承基类的构造函数。 ...
  • 这不是名称问题,而是可访问性问题。 私有字段仍然是派生类的成员状态,它恰好是您无法通过名称直接访问的成员状态,除非派生类是基类的嵌套类。 public class Foo { private int bar; public virtual void Do() { this.bar = 1; } private class DerivedFoo : Foo { public override void Do() ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)