@SessionScoped does not work when adding @Named

谁说胖子不能爱 提交于 2019-12-10 21:34:09

问题


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;
    }

}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/6244965/sessionscoped-does-not-work-when-adding-named

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!