简体   繁体   English

CDI生产方法-注射

[英]CDI Produces Method - Injection

I'm trying to learn the use of @Produces methods in CDI. 我正在尝试学习在CDI中使用@Produces方法。 I have made really simple web-app to test it out. 我已经制作了非常简单的网络应用来对其进行测试。 What I'm trying to do is basically is upon submission of the form pass the value of one bean (Controller) to the other (Cont). 我想做的基本上是在提交表单时将一个bean(控制器)的值传递给另一个(Cont)。

The problem is though that the str value never gets "injected". 问题在于,str值永远不会“注入”。 Obviously there are other ways to do this (inject the entire controller) but I'm trying to learn this specific way. 显然,还有其他方法可以做到这一点(注入整个控制器),但是我正在尝试学习这种特定方法。

.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>
</h:head>

<h:body>
    <h:form>

        <h:outputText value="Input: " />
        <h:inputText value="#{str}" />
        <br />
        <br />

        <h:outputText value="#{cont.str}" />
        <br />
        <br />

        <h:commandButton value="Submit">
        </h:commandButton>
    </h:form>
</h:body>

Controller.java Controller.java

@Named
@SessionScoped
public class Controller implements Serializable {


    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Produces
    @Named("str")
    private String str;


    public String getStr() {
        return str;
    }


    public void setStr(String str) {
        this.str = str;
    }
}

Cont.java Cont.java

@Named
@SessionScoped
public class Cont implements Serializable {



    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Inject
    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }


}

Change your h:inputText to use #{cont.str} 更改您的h:inputText以使用#{cont.str}

The use of a producer here is awkward. 在这里使用生产者很尴尬。 @Named is always evaluated, it's best to use a wrapper object to handle it. @Named总是被求值,最好使用包装对象来处理它。

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

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