从vaadin按钮单击调用spring bean

时间:2014-11-28 12:33:03

标签: java spring vaadin

我有带注释@Component的窗口类实现。在这个类中,我使用注释@Autowired来声明对象。

在我的窗口表单上,我有一个按钮Create,它应该从TextField读取数据,创建新对象并将其存储在数据库中。

@Component("newProjectWindow")
public class NewProjectWindow {

    private Window createProjectWindow;

    @Autowired
    private ProjectService service;

    public Window createWindow() {
        createProjectWindow = new Window("New project");
        initWindow();
        fillWindow();

        return createProjectWindow;
    }

    private void initWindow() {
        createProjectWindow.setSizeUndefined();
        createProjectWindow.setResizable(false);
        createProjectWindow.setModal(true);
        createProjectWindow.addCloseListener(new CloseListener(){

            @Override
            public void windowClose(CloseEvent e) {
                Notification.show("Closed");
            }

        });
    }

    private void fillWindow() {
        final TextField projectName = new TextField("Project name");
        final TextField projectOwner = new TextField("Project owner");
        Button create = new Button("Create");
        create.addClickListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                Project newProject = new Project();
                newProject.setProjectName(projectName.getValue());
                newProject.setProjectOwner(projectOwner.getValue());
                //save it somehow
            }
        });

        Button close = new Button("Cancel");
        close.addClickListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                createProjectWindow.close();
            }
        });

        HorizontalLayout layout = new HorizontalLayout(create, close);
        FormLayout formLayout = new FormLayout(projectName, projectOwner, layout);
        formLayout.setMargin(true);

        createProjectWindow.setContent(formLayout);
    }

}

但问题是如何在数据库中存储对象。我无法访问实例化的ProjectService(使用ProjectRepisitory使用SqlSessionTemplate等),因为它受Spring控制 - 并且匿名{{1} } 不是。

但是如何存储对象?

2 个答案:

答案 0 :(得分:0)

我倾向于不使用匿名内部方法来实现Click侦听器,而是使用我自己的类来实现ClickListner。所以在你的例子中我会改变这样的类:

@Component("newProjectWindow")
public class NewProjectWindow {

    private Window createProjectWindow implements Button.ClickListener;

    @Autowired
    private ProjectService service;
    private Button create = new Button("Create", this);
    private Button cancel new Button("Cancel", this);;

    public Window createWindow() {
        createProjectWindow = new Window("New project");
        initWindow();
        fillWindow();

        return createProjectWindow;
    }

    private void initWindow() {
        createProjectWindow.setSizeUndefined();
        createProjectWindow.setResizable(false);
        createProjectWindow.setModal(true);
        createProjectWindow.addCloseListener(new CloseListener(){

            @Override
            public void windowClose(CloseEvent e) {
                Notification.show("Closed");
            }

        });
    }

    private void fillWindow() {
        final TextField projectName = new TextField("Project name");
        final TextField projectOwner = new TextField("Project owner");


        HorizontalLayout layout = new HorizontalLayout(create, close);
        FormLayout formLayout = new FormLayout(projectName, projectOwner, layout);
        formLayout.setMargin(true);

        createProjectWindow.setContent(formLayout);
    }


    @Override
    public void buttonClick(ClickEvent event) {

         if (event.getButton() == cancel)
         {
              createProjectWindow.close();
         }
         else
         {

              Project newProject = new Project();
              newProject.setProjectName(projectName.getValue());
              newProject.setProjectOwner(projectOwner.getValue());
              //save it somehow
         }
     }

}

答案 1 :(得分:0)

要在示例中从侦听器访问服务,请考虑以下解决方案:

  • 匿名内部类可以引用外部类(使用 OuterClassName.this 语法 - 在您的情况下 NewProjectWindow.this.service )。
  • 您可以声明(内部)类并传递适当的引用。
  • 您可以使用实现侦听器接口的父类的Chris M suggestion