简体   繁体   中英

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.

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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