[英]@ManagedProperty null after @PostConstruct in JSF 2.2
我的规格:
请注意,我已经研究了该主题并找到了一些相关的文章。
例如
@ManagedProperty + @PostConstruct + init()=空指针
@PostConstruct之后注入@ManagedProperty
但是,所提出的解决方案都不适合我或不适用于我的情况。 我不混合使用CDI和/或JSF和/或Spring。 仅限于JSF 2.2注释。
我注入@ManagedProperty(“#{country}”)国家(地区)国家; 到我的ChangeCountrySystemEventListener,但是@ManagedProperty国家/地区的值为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>
AppBean.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());
}
}
ChangeCountrySystemEvent.java
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!");
}
}
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...
}
}
Country.java
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... ");
}
}
控制台输出:
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
可以在用ManagedBean注释的类的字段上使用ManagedProperty,以将值注入此属性。
如果此注释存在于不具有ManagedBean注释的类上,则实现必须对此注释不采取任何措施。
请参阅http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html
由于类ChangeCountrySystemEventListener没有使用ManagedBean进行注释,因此对ManagedProperty字段国家/地区及其空值不采取任何措施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.