我在使用struts 2和hibernate ...
调用Web服务时遇到了问题HTTP状态500 - 无法实例化Action,actions.events.rest.EventController,为命名空间'/'中的'event'定义错误创建名为'actions.events.rest.EventController'的bean:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[actions.events.rest.EventController]:构造函数抛出异常;嵌套异常是java.lang.NullPointerException
这是我的来源:
EventController:
public class EventController extends BaseAction implements ModelDriven<Object> {
private static final long serialVersionUID = 1L;
private String id;
private Object model;
private static Map<String, Event> map;
{
List<Event> events = services.getEvents();
for(Event event : events){
map.put(event.getId() + "", event);
}
}
public HttpHeaders index() {
model = map;
return new DefaultHttpHeaders("index").disableCaching();
}
public String add(){
services.createEvent("Event1");
return "SUCCESS";
}
public String getId() {
return id;
}
public void setId(String id) {
Integer iid = Integer.parseInt(id);
model = services.getEventById(iid);
this.id = id;
}
public Object getModel() {
return model;
}
BaseAction:
public class BaseAction extends ActionSupport {
// So that spring can inject the business singleton
protected Services services;
public void setServices(Services value) {
services=value;
}
// For redirect results
protected String redirectUrl;
public String getRedirectUrl() {
return redirectUrl;
}
public String redirect(String to) {
redirectUrl = to;
return "redirect";
}
当我调试时,List<Event> events = services.getEvents();
中的EventController
出现错误。它能是什么?
答案 0 :(得分:1)
异常是由于您从静态初始化块 中引用注入的bean services
这一事实引起的>:
{
List<Event> events = services.getEvents();
for(Event event : events){
map.put(event.getId() + "", event);
}
}
完全删除它,使用prepare()
方法或@PostConstruct
方法或任何地方执行此类操作,但只保留静态块。
那就是说,你正在遵循一些不好的做法:
您正在返回"SUCCESS"
,而"success"
或SUCCESS
( "success"
)
你在逻辑中设置逻辑,最好不要
我还建议放弃ModelDriven,完全放弃Spring和use CDI,但这最后是我的拙见。顺便说一句,确保take a look at what I'm talking about。