Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
@JSF 2.2中@PostConstruct之后的ManagedProperty null_Jsf_Jsf 2_Glassfish - Fatal编程技术网

@JSF 2.2中@PostConstruct之后的ManagedProperty null

@JSF 2.2中@PostConstruct之后的ManagedProperty null,jsf,jsf-2,glassfish,Jsf,Jsf 2,Glassfish,我的规格: 动态Web模块3.1 GlassFish Web Extensions 4.0 Java 1.8 JavaScript 1.0 JavaServerFaces2.2 服务器:glassfish-4.1.1 OS:赢10分 IDE:版本:Neon.2发行版(4.6.2) 请注意,我已经研究了这个话题,并找到了几个相关的帖子 e、 g 但这两个建议的解决方案都不适合我,也不适用于我的情况。 我不会混合使用CDI和/或JSF和/或Spring。它只是JSF2.2注释 我注入@Man

我的规格:

  • 动态Web模块3.1
  • GlassFish Web Extensions 4.0
  • Java 1.8
  • JavaScript 1.0
  • JavaServerFaces2.2
  • 服务器:glassfish-4.1.1
  • OS:赢10分
  • IDE:版本:Neon.2发行版(4.6.2)
请注意,我已经研究了这个话题,并找到了几个相关的帖子

e、 g

但这两个建议的解决方案都不适合我,也不适用于我的情况。 我不会混合使用CDI和/或JSF和/或Spring。它只是JSF2.2注释

我注入@ManagedProperty(“{country}”)country;对于my ChangeCountrySystemEventListener,但@ManagedProperty country的值为null。 我真的不明白问题在哪里。确实会调用Country构造函数

问题出在哪里

以下是我的完整代码:

index.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://xmlns.jcp.org/jsf/facelets"
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Title</title>
    </h:head>
    <h:body>

        <h3><h:outputText value="#{country.name}" /> </h3>
        <h:form>

            <h:commandButton
                id="changeCountryNameBtn"
                value="Change"
                action="result"
                actionListener="#{appBean.changeCountryName}"
             />

        </h:form>
    </h:body>
    </html>
ChangeCountrySystemEvent.java

    package com.test.beans;

    import javax.faces.application.Application;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.FacesContext;
    import javax.faces.event.ActionEvent;

    @ManagedBean
    @SessionScoped
    public class AppBean {

        public void changeCountryName(ActionEvent ev) {

            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            app.publishEvent(context, ChangeCountrySystemEvent.class, ev.getSource());
            System.out.println(">>>> AppBean.publishEvent(ChangeCountrySystemEvent) fired... " + ev.getSource());

        }

    }
    package com.test.beans;
    import javax.faces.event.SystemEvent;

    public class ChangeCountrySystemEvent extends SystemEvent {

        private static final long serialVersionUID = -1587717461942271611L;

        public ChangeCountrySystemEvent(Object source) {
            super(source);
            System.out.println(">>>> ChangeCountrySystemEvent.class :: constructor invoked!");
        }

    }
2017-04-02T21:49:51.392-0300|Info: JSF_TestProject was successfully deployed in 579 milliseconds.
2017-04-02T21:49:52.251-0300|Info: >>>> Country constructor called...
2017-04-02T21:49:52.277-0300|Info: >>>> ChangeCountrySystemEventListener.class :: Listener constructor invoked!!!
2017-04-02T21:49:52.277-0300|Info: >>>> Country.class :: app.subscribeToEvent() called...
2017-04-02T21:50:16.572-0300|Info: >>>> ChangeCountrySystemEvent.class :: constructor invoked!
2017-04-02T21:50:16.572-0300|Info: >>>> ChangeCountrySystemEventListener.class :: processEvent() > country managed property is EMPTY !!!!
2017-04-02T21:50:16.572-0300|Info: >>>> AppBean.publishEvent(ChangeCountrySystemEvent) fired... javax.faces.component.html.HtmlCommandButton@424c250c
ChangeCountrySystemEventListener.java

    package com.test.beans;

    import javax.faces.bean.ManagedProperty;
    import javax.faces.context.FacesContext;
    import javax.faces.event.SystemEvent;
    import javax.faces.event.SystemEventListener;

    public class ChangeCountrySystemEventListener implements SystemEventListener {

        @ManagedProperty("#{country}")
        Country country;

        // getters and setters
        public Country getCountry() {
            return country;
        }

        public void setCountry(Country country) {
            this.country = country;
        }

        public ChangeCountrySystemEventListener(FacesContext fc) {
            super();
            System.out.println(">>>> ChangeCountrySystemEventListener.class :: Listener constructor invoked!!!");
        }

        @Override
        public void processEvent(SystemEvent se) {

            if (country != null) {
                country.setName("Sweden");
                System.out.println(">>>> ChangeCountrySystemEventListener.class :: SYSTEM EVENT PROCESSED... <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ");
            } else if (country == null) {
                System.out.println(">>>> ChangeCountrySystemEventListener.class :: processEvent() > country managed property is EMPTY !!!!");
            }
        }

        @Override
        public boolean isListenerForSource(Object source) {
            return true; // needs to be set to true, otherwise "processEvent" won't be called...
        }

    }
    package com.test.beans;

    import javax.annotation.PostConstruct;
    import javax.faces.application.Application;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.FacesContext;

    @ManagedBean(name = "country", eager = true)
    @SessionScoped
    public class Country {

        private String name = "Norway";

        public Country() {
            System.out.println(">>>> Country constructor called...");
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @PostConstruct
        public void init() {
            FacesContext fc = FacesContext.getCurrentInstance();
            Application app = fc.getApplication();
            app.subscribeToEvent(ChangeCountrySystemEvent.class, new ChangeCountrySystemEventListener(fc));
            System.out.println(">>>> Country.class :: app.subscribeToEvent() called... ");
        }
    }
控制台输出:

    package com.test.beans;

    import javax.faces.application.Application;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.FacesContext;
    import javax.faces.event.ActionEvent;

    @ManagedBean
    @SessionScoped
    public class AppBean {

        public void changeCountryName(ActionEvent ev) {

            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            app.publishEvent(context, ChangeCountrySystemEvent.class, ev.getSource());
            System.out.println(">>>> AppBean.publishEvent(ChangeCountrySystemEvent) fired... " + ev.getSource());

        }

    }
    package com.test.beans;
    import javax.faces.event.SystemEvent;

    public class ChangeCountrySystemEvent extends SystemEvent {

        private static final long serialVersionUID = -1587717461942271611L;

        public ChangeCountrySystemEvent(Object source) {
            super(source);
            System.out.println(">>>> ChangeCountrySystemEvent.class :: constructor invoked!");
        }

    }
2017-04-02T21:49:51.392-0300|Info: JSF_TestProject was successfully deployed in 579 milliseconds.
2017-04-02T21:49:52.251-0300|Info: >>>> Country constructor called...
2017-04-02T21:49:52.277-0300|Info: >>>> ChangeCountrySystemEventListener.class :: Listener constructor invoked!!!
2017-04-02T21:49:52.277-0300|Info: >>>> Country.class :: app.subscribeToEvent() called...
2017-04-02T21:50:16.572-0300|Info: >>>> ChangeCountrySystemEvent.class :: constructor invoked!
2017-04-02T21:50:16.572-0300|Info: >>>> ChangeCountrySystemEventListener.class :: processEvent() > country managed property is EMPTY !!!!
2017-04-02T21:50:16.572-0300|Info: >>>> AppBean.publishEvent(ChangeCountrySystemEvent) fired... javax.faces.component.html.HtmlCommandButton@424c250c

ManagedProperty可用于带有ManagedBean注释的类的字段,以将值注入该属性

如果此注释出现在没有ManagedBean注释的类上,则实现不得对此注释执行任何操作

请看


由于ChangeCountrySystemEventListener类未使用ManagedBean进行注释,因此不会对ManagedProperty字段country及其null采取任何操作。

感谢您的回复。不幸的是,添加ManagedBean注释(甚至SessionScoped)并不能解决这个问题。即使所有四个类(AppBean、ChangeCountrySystemEvent、ChangeCountrySystemEventListener、Country)都用ManagedBean注释,问题仍然存在。请在侦听器中使用以下代码访问国家管理的bean.Country=(Country)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(“国家”);非常感谢。你提出的解决方案对我有效。:)