添加@Named时,@ SessionScoped不起作用 [英] @SessionScoped does not work when adding @Named
问题描述
考虑以下支持bean:
Consider the following backing bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class Counter {
int counter;
public Counter() {
this.counter = 0;
}
public void Increment() {
this.counter++;
}
public void Decrement() {
this.counter--;
}
public int getCounter() {
return this.counter;
}
}
我创建了一个JSF页面,该页面显示计数器的值,并具有两个用于递增和递减的按钮.它按预期工作.但是,当我将注释javax.inject.Named
添加到Bean时,它似乎不再具有会话作用域.这些按钮仍然起作用(在服务器端处理单击),但是计数器的值始终保持为零.我使用注解是因为在我的实际应用程序中,我需要将其他bean注入该注解中,并且似乎需要注解(如果我错了,请更正我).这种行为的原因是什么?我该怎么办才能解决呢?
I have created a JSF-page that displays the value of the counter and has two buttons to increment and decrement it. It works as expected. However, when I add the annotation javax.inject.Named
to the bean, it does not seem to have session scope anymore. The buttons still work (the click is handled on the server-side), but the value of the counter always remains zero. I am using the annotation because in my real application I need to inject other beans into this one, and this annotation seems to be required (please correct me if I am wrong). What is the reason for this behavior? What can I do to work around it?
推荐答案
您不能将JSF与CDI注释混合使用.使用一个或另一个.在这种情况下,您需要导入 javax.enterprise.context.SessionScoped
代替javax.faces.bean.SessionScoped
.
You cannot mix JSF with CDI annotations. Use the one or the other. In this case, you need to import javax.enterprise.context.SessionScoped
instead of javax.faces.bean.SessionScoped
.
这篇关于添加@Named时,@ SessionScoped不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!