首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么vaadin重复了View的方法调用?

为什么vaadin重复了View的方法调用?
EN

Stack Overflow用户
提问于 2014-01-25 19:19:25
回答 1查看 1.2K关注 0票数 0

领航员级:

代码语言:javascript
复制
@CDIUI
@Title("RUT — Remote University Tool")
public class NavigatorUI extends UI {

    public static final String MAINVIEW = MainView.NAME;

    @EJB
    private TestCRUDService testCRUDService;
    @EJB
    private DisciplineService disciplineService;

    @Override
    protected void init(VaadinRequest request) {
        CreateQuestionsPoolView createQuestionsPoolView = new CreateQuestionsPoolView();
        createQuestionsPoolView.setTestCRUDService(testCRUDService);
        createQuestionsPoolView.setDisciplineService(disciplineService);

        MainView mainView = new MainView();

        Navigator navigator = new Navigator(this, this);
        navigator.addView(CreateQuestionsPoolView.NAME, createQuestionsPoolView);
        navigator.addView(MainView.NAME, mainView);

        navigator.navigateTo(MAINVIEW);
    }
}

MainView:

代码语言:javascript
复制
public class MainView extends VerticalLayout implements View {

    public static final String NAME = "main";

    private Button openCreateQuestionPoolViewButton = new Button("Создать тестовое задание");

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        addComponent(openCreateQuestionPoolViewButton);
        final Navigator NAVIGATOR = event.getNavigator();

        openCreateQuestionPoolViewButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                NAVIGATOR.navigateTo(CreateQuestionsPoolView.NAME);
            }
        });
    }
}

My实现:

代码语言:javascript
复制
public class CreateQuestionsPoolView extends VerticalLayout implements View {

    public static final String NAME = "test-creation";
    private Logger logger = Logger.getLogger(CreateQuestionsPoolView.class.getName());

    // ------------ main layout
    private TextField poolThemeField = new TextField("Тема");
    private TextField branchIdField = new TextField("Группа");
    private TextField disciplineField = new TextField("Дисциплина");

    private Button addQuestionButton = new Button("Добавить вопрос");
    private Button exportPoolButton = new Button("Экспортировать вопросы");
    private Button createQuestionsPoolButton = new Button("Создать новый тест");
    private Button saveQuestionsPoolButton = new Button("Сохранить тест");
    private Button openMainUIButton = new Button("Вернуться на стартовую страницу");

    // ------------ question layout
    private Layout questionLayout;
    private TextArea questionTitle = new TextArea("Вопрос");
    private TextArea[] answers = {
            new TextArea("Вариант ответа 1"),
            new TextArea("Вариант ответа 2"),
            new TextArea("Вариант ответа 3"),
            new TextArea("Вариант ответа 4")
    };
    private CheckBox[] isCorrectAnswerCheckboxes = {
            new CheckBox("Правильный"),
            new CheckBox("Правильный"),
            new CheckBox("Правильный"),
            new CheckBox("Правильный")
    };

    private Button saveQuestionButton = new Button("Сохранить вопрос");

    // ------------ download layout
    private Layout downloadLayout;
    private Button downloadButton = new Button("Скачать");

    // ------------ shared data
    private QuestionsPool questionsPool;
    private Navigator navigator;
    private TestCRUDService testCRUDService;
    private DisciplineService disciplineService;

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        logger.info("View will be initialized now");
        initMainLayout();
        initQuestionLayout();
        initDownloadLayout();

        navigator = event.getNavigator();
    }

    public void setTestCRUDService(TestCRUDService testCRUDService) {
        logger.info("TestCRUDService set up");
        this.testCRUDService = testCRUDService;
    }

    public TestCRUDService getTestCRUDService() {
        logger.info("TestCRUDService requested");
        return testCRUDService;
    }

    public void setDisciplineService(DisciplineService disciplineService) {
        logger.info("disciplineService set up");
        this.disciplineService = disciplineService;
    }

    public DisciplineService getDisciplineService() {
        logger.info("disciplineService requested");
        return disciplineService;
    }

    private void initDownloadLayout() {
        logger.info("Download layout initialization");
        downloadLayout = new GridLayout();
        downloadLayout.setVisible(false);
        downloadLayout.addComponent(downloadButton);
        addComponent(downloadLayout);
    }

    private void initQuestionLayout() {
        logger.info("Question layout initialization");
        questionLayout = new GridLayout();
        questionLayout.setSizeFull();
        questionLayout.setVisible(false);
        questionLayout.addComponent(questionTitle);
        questionTitle.setWidth("100%");
        for (int i = 0; i < answers.length; i++) {
            HorizontalLayout horizontalLayout = new HorizontalLayout();
            horizontalLayout.addComponent(answers[i]);
            horizontalLayout.addComponent(isCorrectAnswerCheckboxes[i]);
            horizontalLayout.setWidth("100%");
            horizontalLayout.setHeight("20%");
            questionLayout.addComponent(horizontalLayout);

            answers[i].setWidth("90%");
            answers[i].setHeight("100%");
        }
        questionLayout.addComponent(saveQuestionButton);

        saveQuestionButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                Question question = new Question(questionTitle.getValue());
                for (int i = 0; i < answers.length; i++) {
                    question.getAnswers().put(
                            new Answer(answers[i].getValue()), isCorrectAnswerCheckboxes[i].getValue()
                    );
                }
                questionsPool.getQuestions().add(question);
                Notification.show("Вопрос сохранен", Notification.Type.TRAY_NOTIFICATION);

                questionLayout.setVisible(false);
            }
        });
        addComponent(questionLayout);
    }

    private void initMainLayout() {
        logger.info("Main layout initialization");
        setWidth("100%");
        addComponent(poolThemeField);
        addComponent(branchIdField);
        addComponent(disciplineField);

        HorizontalLayout buttonsLayout = new HorizontalLayout();
        buttonsLayout.addComponent(addQuestionButton);
        buttonsLayout.addComponent(exportPoolButton);
        buttonsLayout.addComponent(createQuestionsPoolButton);
        buttonsLayout.addComponent(saveQuestionsPoolButton);
        buttonsLayout.addComponent(openMainUIButton);

        addComponent(buttonsLayout);

        addQuestionButton.setVisible(false);
        exportPoolButton.setVisible(false);

        addQuestionButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                logger.info("Add question button pressed");
                for (int i = 0; i < answers.length; i++) {
                    answers[i].setValue("");
                    isCorrectAnswerCheckboxes[i].setValue(false);
                }
                questionTitle.setValue("");

                questionLayout.setVisible(true);
            }
        });

        exportPoolButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                logger.info("Export pool button pressed");
                String serializedPool = JsonTransformer.toString(questionsPool);
                downloadLayout.setVisible(true);

                FileDownloader fileDownloader = new FileDownloader(createResource(serializedPool));
                fileDownloader.extend(downloadButton);
            }
        });

        createQuestionsPoolButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                logger.info("Create pool button pressed");
                questionsPool = new QuestionsPool(poolThemeField.getValue());
                questionsPool.setBranchId(Integer.valueOf(branchIdField.getValue()));
                questionsPool.setQuestions(new ArrayList<Question>());

                addQuestionButton.setVisible(true);
                exportPoolButton.setVisible(true);
            }
        });

        saveQuestionButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                logger.info("Save question button pressed");
                testCRUDService.addQuestionsPool(
                        questionsPool, disciplineService.getDisciplineByName(disciplineField.getValue()));
            }
        });

        openMainUIButton.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                logger.info("Back to main UI button pressed");
                navigator.navigateTo(NavigatorUI.MAINVIEW);
            }
        });
    }
}

应用程序从NavigatorUI类开始,然后导航到MainView。我导航到CreateQuestionsPoolView。当我调用视图的任何方法时,调用都是重复的。我对Vaadin很陌生,不明白原因。

Vaadin 7.0.5,玻璃鱼4

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-28 20:07:55

如果在enter方法中添加单击侦听器,则每次输入视图时都会添加新的侦听器。因此,第二次输入视图时,当您单击openCreateQuestionPoolViewButton时,会附加两个侦听器,并且它的操作会被触发两次。

您可以考虑使用MVC和事件总线,这将使您的导航器保持在一个演示器中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21355093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档