JSF-受管Bean字段未在PostConstruct上初始化 [英] JSF - Managed Bean fields not initialized at PostConstruct

查看:147
本文介绍了JSF-受管Bean字段未在PostConstruct上初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellow,我正在尝试根据我从上一页传递过来的参数加载jsf页面.

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

注意:我正在构建的应用程序并非纯粹是JSF,我正在使用Jdeveloper来构建Java ee Web应用程序,其中包括JSF,JSP和servlet.我的网页是.jspx 这是我的代码 第1页:

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支持bean:

page2 backing bean:

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

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

但出现以下异常:

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)
.
    .

有什么帮助吗?

推荐答案

我认为您正在尝试将bean用作后备bean,并且我想您在jspx <h:outputText binding="#{myBean.outputText}" />中有此标记,所以您需要始终实例化此变量,容器不会为此组件注入任何实例,可以在使用它之前从构造函数或在postconstruct方法中进行初始化.

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

这篇关于JSF-受管Bean字段未在PostConstruct上初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆