0

关于该项目的一些信息:基本上一直在尝试使搜索功能起作用以更新我以前做过的列表视图并且没有问题;到现在。我似乎找不到问题,希望你们能找到!

运行时出现空指针问题:

modelList.setObject(exerciseJdbc.fetchExercisesByAttr(3,
                    exercise.getName(), null, exercise.getComplexity()
                            .getLevel()));

现在我确信这是一个简单的错误,但我终其一生都无法弄清楚。

public class ManageExercise extends BaseManagerPage {
private static final long serialVersionUID = -3241765837700606142L;

private ModalWindow mwAddExercise;


private LoadableDetachableModel<List<Exercise>> modelList;

@SpringBean
ExerciseJdbc exerciseJdbc;

public ManageExercise() {
    final Exercise exercise = new Exercise();
    add(mwAddExercise = new ModalWindow("mwAddExercise"));

    final WebMarkupContainer wmc = new WebMarkupContainer("wmc");
    wmc.setOutputMarkupId(true);

    try{
    modelList = exerciseModel();
    }
    catch(Exception e){
        e.printStackTrace();
    }

    Form<Exercise> searchForm = new Form<Exercise>("searchForm", new CompoundPropertyModel<Exercise>(exercise));
    searchForm.add(new TextField<Integer>("searchId", new PropertyModel<Integer>(exercise, "id")));
    searchForm.add(new TextField<String>("searchName", new PropertyModel<String>(exercise, "name")));   
    // searchForm.add(new DropDownChoice<String>("complexity.level",
    // new PropertyModel<String>(this, "complexity")));
    searchForm.add(new AjaxLink<String>("searchSubmit") {
        private static final long serialVersionUID = 1L;
@Override
        public void onClick(AjaxRequestTarget target) {
            modelList.setObject(exerciseJdbc.fetchExercisesByAttr(exercise.getId(), exercise.getName(), null, exercise.getComplexity().getLevel()));
            target.add(wmc);
        }

    });



    add(searchForm);

    ListView<Exercise> lvExercise = new ListView<Exercise>("lvExercise",
            modelList) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(final ListItem<Exercise> item) {
            final Exercise model = item.getModelObject();
            item.add(new Label("id", Integer.toString(model.getId())));
            item.add(new Label("name", model.getName()));
            item.add(new ExternalLink("demoUrl", model.getDemoUrl(), model
                    .getDemoUrl()));
            item.add(new Label("complexity", model.getComplexity()
                    .getLevel()));
            item.add(new AjaxLink<String>("edit") {
                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(AjaxRequestTarget target) {
                    renderAddExercise(item.getModelObject());
                    mwAddExercise.show(target);
                }
            });
            item.add(new AjaxLink<String>("delete") {
                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(AjaxRequestTarget target) {
                    exerciseJdbc.disableExercise(model);
                    target.add(wmc);

                }
            });
        }
    };

    wmc.add(lvExercise);
    add(wmc);

    mwAddExercise.setWindowClosedCallback(new WindowClosedCallback() {
        private static final long serialVersionUID = 1L;

        public void onClose(AjaxRequestTarget target) {
            target.add(wmc);
        }
    });

    AjaxLink<String> addExercise = new AjaxLink<String>("addExercise") {
        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            renderAddExercise(new Exercise());
            mwAddExercise.show(target);

        }

    };

    add(addExercise);

}

private LoadableDetachableModel<List<Exercise>> exerciseModel() {
    LoadableDetachableModel<List<Exercise>> exercideModel = new LoadableDetachableModel<List<Exercise>>() {
        private static final long serialVersionUID = 1L;

        protected List<Exercise> load() {
            return exerciseJdbc.fetchAllExercises();
        }
    };
    return exercideModel;
}

private void renderAddExercise(Exercise exercise) {
    mwAddExercise.setContent(new AddExercisePanel(mwAddExercise
            .getContentId(), exercise, mwAddExercise));
}

}

如果我错过了任何所需的信息,请告诉我,但我有一种可怕的感觉,这是一种非常愚蠢的错误。

4

2 回答 2

0

问题是您使用的AjaxLink是 searchSubmit ,因此TextFields不会将值提交给服务器。

有两种方法可以解决这个问题:

  • 摆脱AjaxLink并使用表单提交解决方案

或者

  • TextField像下面的代码一样添加一个 Ajax 行为:

//ajax onblur 行为
searchForm.add(new TextField("searchId", new PropertyModel(exercise, "id")).add(new AjaxFormComponentUpdatingBehavior("onblur") {

                  @Override
                  protected void onUpdate(AjaxRequestTarget target) {
                      //should have the id here
                      exercise.getId();   
                  }
                }
              ));
于 2013-05-16T09:21:23.113 回答
0

所以回想起来,这个问题的答案相当简单:

我用 AjaxSubmitLink 替换了我的 AjaxLink,将此链接放在表单之外,并将表单作为构造函数传递给链接。

在 OnSubmit 方法中,我在模型上调用了 detatch,然后调用了 setObject;通过调用从数据库中获取新数据。

这很好用,希望这对处于类似情况的任何人有所帮助,如果出现任何问题,请添加。

于 2013-05-17T18:26:10.770 回答