1

struts.xml

<package name="backpower" namespace="/backpower" extends="struts-default">
    <default-interceptor-ref name="defaultStack"></default-interceptor-ref>
    <!-- backup battery manager -->
    <action name="backpower" class="com.cps.backpower.action.BackpowerAction">
        <result name="addBattery">/WEB-INF/views/backpower/info/addBattery.jsp</result>
        <result name="editBattery">/WEB-INF/views/backpower/info/editBattery.jsp</result>
        <result>/WEB-INF/views/backpower/info/list.jsp</result>
    </action>
</package>

add.jsp

    <div class="form-actions">
        <button name="method:save" type="submit" class="btn btn-primary">
            save
        </button>
        <button type="reset" class="btn">
            reset
        </button>
    </div>

我的BackpowerAction

public class BackpowerAction extends BaseManageAction{

private static final long serialVersionUID=1L;
public BackpowerService backpowerService;
public PaginationSupport backpowerPagination;
public PageTip pageTip;
public BackpowerInfo backpowerInfo;
public Long id;


public String execute() throws Exception{
    return search();
}

public String search(){
    String hql="from BackpowerInfo where 1=1";
    backpowerPagination=backpowerService.getPage(hql,getPageNo(),getPageSize());
    return SUCCESS;
}

public void setBackpowerInfo(BackpowerInfo backpowerInfo){
    this.backpowerInfo=backpowerInfo;
}

public String add(){
    return "addBattery";
}

public String save() {
    if (backpowerInfo.getId() == null) {
        try {
            backpowerService.save(backpowerInfo);
            return SUCCESS;
        } catch (Exception e) {
            return add();
        }
    } else {
        try {
            backpowerService.update(backpowerInfo.getId(),backpowerInfo.getName(),backpowerInfo.getLocation(),backpowerInfo.getManager());
            pageTip.setOk(true);
            pageTip.setTip("修改用户信息成功");
            return search();
        } catch (Exception e) {
            e.printStackTrace();
            pageTip.setOk(false);
            pageTip.setTip("修改用户信息失败");
            return edit();
        }
    }

}

public String edit() {
    backpowerInfo=backpowerService.findById(getId());

    return "editBattery";
}

public String del() throws Exception {
    try {
        backpowerService.delete(getId());
        pageTip.setOk(true);
        pageTip.setTip("删除成功");
    } catch (Exception e) {
        pageTip.setOk(false);
        pageTip.setTip("删除失败");
    }
    return search();
}

public BackpowerInfo getBackpowerInfo(){
    return backpowerInfo;
}




public PaginationSupport getBackpowerPagination(){

    return backpowerPagination;
}

public void setBackpowerPagination(PaginationSupport backpowerPagination){
    this.backpowerPagination=backpowerPagination;
}

public BackpowerService getBackpowerService(){
    return backpowerService;
}

public void setBackpowerService(BackpowerService backpowerService){
    this.backpowerService=backpowerService;
}

public PageTip getPageTip(){
    return pageTip;
}

public void setPageTip(PageTip pageTip){
    this.pageTip=pageTip;
}

public long getId(){
    return id;
}

public void setId(long id){
    this.id=id;
}
}

java.lang.NullPointerException
com.cps.backpower.action.BackpowerAction.save(BackpowerAction.java:39)

当我点击网页上的保存按钮时,它给出了Struts2问题报告,我错在哪里,我应该如何纠正它?

4

3 回答 3

1

我认为backpowerInfo在您的代码中为空。

在调用 getter 之前,您还没有填充或实例化它。

于 2013-07-31T10:39:47.220 回答
0

我认为backpowerServicenull,在计算代码中的行数之后。您应该在使用前注入服务。

我看到有一个 setter,backpowerService但应该在执行任何操作之前调用它。我不知道你用哪种方式实例化一个BackpowerServicebean,哪个容器和DI。但是你不要用拦截器来做,因为你的配置没有反映它。

我的建议是使用一些 DI 框架并将您的服务 bean 放在那里并@Inject在 setter 或字段上使用以将服务作为依赖项注入,即

@Inject
public void setBackpowerService(BackpowerService backpowerService){
    this.backpowerService=backpowerService;
}
于 2013-07-31T10:54:53.190 回答
0

BackpowerInfo模型中的实例对象的BackpowerAction名称应与中使用的名称相同add.jsp

于 2013-08-02T01:37:03.190 回答