处理vaadin中单击按钮的视图

时间:2016-03-29 19:00:19

标签: java spring maven vaadin vaadin7

对Vaadin来说非常新鲜。我正在通过查看Github和其他文档设置项目,使用Spring-security,Vaadin,Maven。

我使用spring security项目创建了示例vaadin-maven。现在我正在登录页面,然后在suucessful登录后,我得到一些gcd

我尝试更改视图MainView.java按钮。但我无法做到这一点。请建议我如何点击Vaadin中的按钮进入另一个视图。

以下是代码:

MyUI.java

onclick

MainView.java

@Component
@Theme("mytheme")
@Scope("prototype")
@Widgetset("com.mygubbi.GameProject.MyAppWidgetset")
public class MyUI extends UI implements ErrorHandler {
    @Override
    protected void init(final VaadinRequest request) {
        VaadinSession.getCurrent().setErrorHandler(this);
        setSizeFull();
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);

        DiscoveryNavigator navigator = new DiscoveryNavigator(this, this);
    }

    /**
     * Exception on action
     */
    @Override
    public void error(com.vaadin.server.ErrorEvent event) {
        // connector event
        if (event.getThrowable().getCause() instanceof AccessDeniedException) {
            AccessDeniedException accessDeniedException = (AccessDeniedException) event.getThrowable().getCause();
            Notification.show(accessDeniedException.getMessage(), Notification.Type.ERROR_MESSAGE);

            // Cleanup view. Now Vaadin ignores errors and always shows the 
            //view.  :-(
            // since beta10
            setContent(null);
            return;
        }

        // Error on page load. Now it doesn't work. User sees standard error 
        //page.
        if (event.getThrowable() instanceof AccessDeniedException) {
            AccessDeniedException exception = (AccessDeniedException) event.getThrowable();

            Label label = new Label(exception.getMessage());
            label.setWidth(-1, Unit.PERCENTAGE);

            Link goToMain = new Link("Go to main", new ExternalResource("/"));

            VerticalLayout layout = new VerticalLayout();
            layout.addComponent(label);
            layout.addComponent(goToMain);
            layout.setComponentAlignment(label, Alignment.MIDDLE_CENTER);
            layout.setComponentAlignment(goToMain, Alignment.MIDDLE_CENTER);

            VerticalLayout mainLayout = new VerticalLayout();
            mainLayout.setSizeFull();
            mainLayout.addComponent(layout);
            mainLayout.setComponentAlignment(layout, Alignment.MIDDLE_CENTER);

            setContent(mainLayout);
            Notification.show(exception.getMessage(), Notification.Type.ERROR_MESSAGE);
            return;
        }

        DefaultErrorHandler.doDefault(event);
    }
}

请建议我如何在这里使用两个按钮。如果我点击按钮1,它应该转到@Component @Scope("prototype") @VaadinView(MainView.NAME) public class MainView extends Panel implements View { public static final String NAME = ""; private Label usernameLabel = new Label(); private Label rolesLabel = new Label(); @PostConstruct public void PostConstruct() { setSizeFull(); final VerticalLayout layout = new VerticalLayout(); layout.setSpacing(true); layout.setMargin(true); HorizontalLayout usernameLayout = new HorizontalLayout(); usernameLayout.setSpacing(true); usernameLayout.addComponent(new Label("Username:")); usernameLayout.addComponent(usernameLabel); HorizontalLayout userRolesLayout = new HorizontalLayout(); userRolesLayout.setSpacing(true); userRolesLayout.addComponent(new Label("Roles:")); userRolesLayout.addComponent(rolesLabel); layout.addComponent(usernameLayout); layout.addComponent(userRolesLayout); Link userView = new Link("ROLE_USER View (disabled, if user doesn't have access) ", new ExternalResource("#!" + RoleUserView.NAME)); Link roleView = new Link("ROLE_ADMIN View (disabled, if user doesn'thave access) ", new ExternalResource("#!" + RoleAdminView.NAME)); userView.setEnabled(SpringSecurityHelper.hasRole("ROLE_USER")); roleView.setEnabled(SpringSecurityHelper.hasRole("ROLE_ADMIN")); layout.addComponent(userView); layout.addComponent(roleView); layout.addComponent(new Link("ROLE_ADMIN View (throw exception, if user doesn 't have access)", new ExternalResource("#!" + RoleAdminView.NAME))); layout.addComponent(new Link("Logout", new ExternalResource("/j_spring_security_logout"))); Button button = new Button("Click Me"); button.addClickListener(new Button.ClickListener() { public void buttonClick(ClickEvent event) { layout.addComponent(new Label("Thank you for clicking")); } }); layout.addComponent(button); setContent(layout); } @Override public void enter(ViewChangeListener.ViewChangeEvent event) { User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); List<String> roles = new ArrayList<String>(); for (GrantedAuthority grantedAuthority : user.getAuthorities()) { roles.add(grantedAuthority.getAuthority()); } usernameLabel.setValue(user.getUsername()); rolesLabel.setValue(StringUtils.join(roles, ",")); } } ,如果我点击按钮2它应该转到oneView.java

提前一百万吨的感谢。 :)希望我的stackoverflow大师有一个很好的回应。 :)

0 个答案:

没有答案