我是Java和JSf的新手,在控制器中访问后端bean中的getter值时,我遇到了空指针错误。以下是代码片段:
输入文本区,我可以在setter中看到sysout,从页面提交的值,但不能访问getter值。faces-config.xml
<managed-bean>
<managed-bean-name>researchHisttoryController</managed-bean-name>
<managed-bean-class>com.controller.ResearchHisttoryController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>history</property-name>
<property-class>com.researchhistory.model.ShipmentHistory</property-class>
<value>#{history}</value>
</managed-property>
</managed-bean>
<managed-bean>
Controller class:
private ShipmentHistory history;
//getters and setter followed;我以getHistory.getTrackNumber; NPE error身份从ShipmentHistory类访问varaible
ShipmentHistory.java
private String trackNumber;
//getters and setters 我哪里做错了,请帮帮我。耽误您时间,实在对不起。
发布于 2012-02-08 08:55:49
那么#{history}就是null。显然,您还没有将其声明为托管bean。
<managed-bean>
<managed-bean-name>history</managed-bean-name>
<managed-bean-class>com.researchhistory.model.ShipmentHistory</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>但这有点奇怪。模型不应被视为控制器。根据功能需求,肯定会有更好的方法。也许您只想在每次创建ResearchHisttoryController时都创建一个新的ShipmentHistory空白实例。在这种情况下,请在支持bean的(Post)构造函数中执行此工作。
public ResearchHisttoryController() {
history = new History();
}(请注意,在支持bean的类名中有一个拼写错误)
https://stackoverflow.com/questions/9186142
复制相似问题