简体   繁体   English

JSF-受管Bean字段未在PostConstruct上初始化

[英]JSF - Managed Bean fields not initialized at PostConstruct

Hellow I am trying to load a jsf page depending on a parameter I pass from a previous page. 您好,我尝试根据我从上一页传递的参数加载jsf页面。

note: the application I am building is not purely JSF, I am using Jdeveloper to build a java ee web application, which includes JSF, JSP and servlets. 注意:我正在构建的应用程序并非纯粹是JSF,我正在使用Jdeveloper来构建Java ee Web应用程序,其中包括JSF,JSP和servlet。 My web pages are .jspx here is my code page1: 我的网页是.jspx,这是我的代码page1:

<a href="page2.jspx?displayText=test"> goto page2 </a>

page2 backing bean: page2支持bean:

@ManagedProperty(value= "#{param.displayText}")
private String displayText;
private HtmlOutputText outputText;

@PostConstruct
public void testMethod(){
 getOutputText().setValue(displayText);
}

but I get the following exception: 但我得到以下异常:

java.lang.NullPointerException
at view.backing.page2.testMethod(Results_page.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
.
    .

any help please? 有什么帮助吗?

I think you're trying to use your bean as a backing bean, and I suppose you have this tag in your jspx <h:outputText binding="#{myBean.outputText}" /> well you need to always instantiate this variable the container doesn't inject any instance for this component, you could init from the constructor or in the postconstruct method before you used it. 我认为您正在尝试将bean用作后备bean,并且我想您在jspx <h:outputText binding="#{myBean.outputText}" />有此标记,那么您需要始终实例化该变量容器不为此组件注入任何实例,可以在使用它之前从构造函数或postconstruct方法中进行初始化。

@PostConstruct
public void testMethod(){
   outputText = new HtmlOutputText();
   getOutputText().setValue(displayText);
}

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

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