JSF - Managed Bean fields not initialized at PostConstruct

浪尽此生 提交于 2020-01-07 05:05:22

问题


Hellow I am trying to load a jsf page depending on a parameter I pass from a previous page.

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. My web pages are .jspx here is my code page1:

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

page2 backing 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?


回答1:


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.

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


来源:https://stackoverflow.com/questions/23310094/jsf-managed-bean-fields-not-initialized-at-postconstruct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!