简体   繁体   English

使用 CDI(上下文和依赖注入)支持 bean 而不是托管 Bean

[英]Using CDI (Context & Dependency Injection) backing beans instead of Managed Beans

I rode that is recommended to use CDI beans as backing beans instead of JSF managed beans.我认为建议使用 CDI bean 作为支持 bean 而不是 JSF 托管 bean。

So i decided to create a little example, to understand how it works, for a @RequestScopedBean :因此,我决定为@RequestScopedBean创建一个小示例,以了解其工作原理:

-instead of using @ManagedBean("beanName") ,i use @Named("beanName") - 而不是使用@ManagedBean("beanName") ,我使用@Named("beanName")

-instead of using javax.faces.bean.RequestScopped i use javax.enterprise.context.RequestScoped; - 而不是使用javax.faces.bean.RequestScoped我使用javax.enterprise.context.RequestScoped;

The demo program is very simple, i have a field and a submit button, when the user inputs something and the page is refreshed, the inputed value is not displayed anymore(It last while the request lasts right?).演示程序非常简单,我有一个字段和一个提交按钮,当用户输入内容并刷新页面时,输入的值不再显示(它会持续到请求持续对吗?)。 I think i did everything ok, but i get an exception that says:我想我做的一切都很好,但我得到一个例外,上面写着:

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception javax.el.PropertyNotFoundException: /index.xhtml @19,47 value="#{cdiBean.passedValue}": Target Unreachable, identifier 'cdiBean' resolved to null警告:StandardWrapperValve [Faces Servlet]:PWC1406:Servlet Faces Servlet 的 Servlet.service() 引发异常 javax.el.PropertyNotFoundException:/index.xhtml @19,47 value="#{cdiBean.passedValue}":目标无法访问,标识符'cdiBean' 解析为 null

This is how my program looks like:这就是我的程序的样子:

index.xhtml索引.xhtml

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<h:head>
        <title>RequestScope demo CDI(Component Dependency Injection)</title>
</h:head>

<h:body>

    <h:form>

    <h3>RequestScope demo CDI(Component Dependency Injection)</h3>

    <h:inputText value="#{cdiBean.passedValue}"/>
    <br/>
    <h:commandButton value="submit" action="index"/>

    </h:form>
</h:body>
</html>

DemoBB.java演示BB.java

package backingbeans;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named("cdiBean")//The Named anotation indicates that this is a CDI bean
@RequestScoped//If we use CDI beans the @RequestScoped annotation must come from: javax.enterprise.context.RequestScoped;
public class DemoBB {

    //This value will be saved on the session only until the server responds to the request
    private String passedValue;

    public String getPassedValue() {
        return passedValue;
    }

    public void setPassedValue(String passedValue) {
        this.passedValue = passedValue;
    }   
}

-Where is my mistake? -我的错误在哪里?

-What is the advantage of using this approach? - 使用这种方法有什么好处? I still don't understand that.我还是不明白。

Do you have an empty beans.xml along with your web.xml ?你有一个空的beans.xml和你的web.xml吗? I think it is mandatory to be there.我认为去那里是必须的。

Read section 15.6 here . 在此处阅读第 15.6 节。 Quote from it:引用它:

CDI doesn't define any special deployment archive. CDI 没有定义任何特殊的部署存档。 You can package beans in JARs, EJB-JARs or WARs—any deployment location in the application classpath.您可以 JARs、EJB-JAR 或 WAR 中的 package bean — 应用程序类路径中的任何部署位置。 However, the archive must be a "bean archive".但是,存档必须是“bean 存档”。 That means each archive that contains beans must include a file named beans.xml in the META-INF directory of the classpath or WEB-INF directory of the web root (for WAR archives).这意味着每个包含 bean 的存档都必须在类路径的 META-INF 目录或 web 根目录的 WEB-INF 目录中包含一个名为 beans.xml 的文件(对于 WAR 存档)。 The file may be empty.该文件可能为空。 Beans deployed in archives that do not have a beans.xml file will not be available for use in the application.在没有 beans.xml 文件的档案中部署的 Bean 将无法在应用程序中使用。

In an embeddable EJB container, beans may be deployed in any location in which EJBs may be deployed.在可嵌入的 EJB 容器中,bean 可以部署在可以部署 EJB 的任何位置。 Again, each location must contain a beans.xml file.同样,每个位置都必须包含 beans.xml 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM