JSF 2.0注入具有不同作用域的托管Bean [英] JSF 2.0 Injecting managed bean with a different scope

查看:96
本文介绍了JSF 2.0注入具有不同作用域的托管Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无状态的控制器,负责处理表单.定义为ApplicationScoped.在我的页面上,我有一个与定义为ViewScoped的后备bean相关联的表单.

I have a controller that is stateless which takes care of processing forms. This is defined as ApplicationScoped. On my page I have a form associated to a backing bean defined as a ViewScoped.

我要处理表单时遇到的错误:

The error I got when I want to process the form:

serverError: class com.sun.faces.mgbean.ManagedBeanCreationException Unable to create managed bean myController.  The following problems were found:
     - The scope of the object referenced by expression #{myFormBean}, view, is shorter than the referring managed beans (myController) scope of application

以我的形式:

       Name: <h:inputText value="#{myFormBean.name}" id="name" />
        <h:commandButton value="Save Name" action="#{myController.processForm}">
            <f:ajax render="nameResult" />
        </h:commandButton>
       Your name is <h:outputText value="#{myFormBean.name}" id="nameResult"/>

控制器:

@ManagedBean
@ApplicationScoped
public class MyController {
    @ManagedProperty("#{myFormBean}")
    private MyFormBean myBean;
    public void processForm() {
        System.out.println(myBean.getName());
        // Save current name in session
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(
                "name", myBean.getName());
    }
}

后备豆:

@ManagedBean
@ViewScoped
public class MyFormBean {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

我可以通过将控制器设置为SessionScoped来解决此问题,但这不是一种干净的方法,因为该控制器是无状态的,因此我不需要为每个会话使用一个控制器.整个应用程序只需一个控制器即可.

I could solve that by setting the controller as SessionScoped but it’s not a clean way since the controller is stateless, so I don’t need one controller for each session. One controller for the whole application should be sufficient.

我具有Spring MVC背景,这就是为什么我对如何使用JSF 2.0感到困惑的原因

I have a Spring MVC background, that’s why I am confused on how to do things with JSF 2.0

推荐答案

您的设计存在缺陷.您的控制器根本不是无状态的.对于每个请求/视图,它都有一个不同的属性,即myBean.如果支持,则每个新的请求/视图都将覆盖先前设置的请求/视图,并且最终用户将面对完全不同的最终用户的属性值.这会导致在高并发情况下出现问题.

There is a flaw in your design. Your controller is not stateless at all. It has a property which is different for each request/view, namely the myBean. If it was supported, then every new request/view would override the previously set one and the enduser will face the property value of a completely different enduser. This leads to problems in high concurrent situations.

您需要使其在请求/视图范围内而不是在应用程序范围内.即便如此,我仍然认为您必须采取完全不同的方法.您是在action方法的会话范围内手动设置属性,而不是将其设置为(注入的)会话范围Bean的属性.如何正确解决问题取决于功能需求,而这个问题尚不清楚.

You need to make it request/view scoped instead of application scoped. Still then, I believe that you have to approach it completely different. You're manually setting an attribute in the session scope in the action method instead of setting it as a property of an (injected) session scoped bean. How to solve it properly depends on the functional requirement which is not clear from the question.

这篇关于JSF 2.0注入具有不同作用域的托管Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆