Java 处理vaadin中单击按钮的视图
我对瓦丁很陌生。我通过查看Github和其他文档来建立这个项目,我使用的是SpringSecurity、Vaadin和Maven 我用spring安全项目创建了示例vaadin maven。现在我得到了登录页面,然后在成功登录之后,我得到了一些Java 处理vaadin中单击按钮的视图,java,spring,maven,vaadin,vaadin7,Java,Spring,Maven,Vaadin,Vaadin7,我对瓦丁很陌生。我通过查看Github和其他文档来建立这个项目,我使用的是SpringSecurity、Vaadin和Maven 我用spring安全项目创建了示例vaadin maven。现在我得到了登录页面,然后在成功登录之后,我得到了一些MainView.java 我正在尝试更改视图onclick按钮。但我没办法做到。请建议我如何单击Vaadin中的按钮转到其他视图 代码如下: MyUI.java @Component @Theme("mytheme") @Scope("prototype
MainView.java
我正在尝试更改视图onclick
按钮。但我没办法做到。请建议我如何单击Vaadin中的按钮转到其他视图
代码如下:
MyUI.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);
}
}
@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, ","));
}
}
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);
}
}
@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, ","));
}
}
@组件
@范围(“原型”)
@VaadinView(MainView.NAME)
公共类MainView扩展面板实现视图{
公共静态最终字符串名称=”;
私有标签usernamelab=新标签();
私有标签rolesLabel=新标签();
@施工后
施工后公共空间(){
设置大小();
最终垂直布局=新建垂直布局();
布局。设置间距(真);
布局。设置页边距(真);
HorizontalLayout usernameLayout=新建HorizontalLayout();
usernameLayout.setSpacing(true);
添加组件(新标签(“用户名:”);
usernameLayout.addComponent(usernameLabel);
HorizontalLayout userRolesLayout=新建HorizontalLayout();
userRolesLayout.setSpacing(true);
添加组件(新标签(“角色:”);
userRolesLayout.addComponent(rolesLabel);
layout.addComponent(用户名布局);
layout.addComponent(userRolesLayout);
Link userView=新链接(“角色用户视图(禁用,如果用户没有访问权限)”,新外部资源(“#!”+RoleUserView.NAME));
Link roleView=新链接(“角色管理视图(禁用,如果用户没有访问权限)”,新外部资源(“#!”+RoleAdminView.NAME));
setEnabled(SpringSecurityHelper.hasRole(“角色用户”);
setEnabled(SpringSecurityHelper.hasRole(“角色\管理员”);
layout.addComponent(用户视图);
布局。添加组件(roleView);
layout.addComponent(新链接(“角色\管理视图(如果用户没有访问权限,抛出异常)”,新外部资源(“\!”+RoleAdminView.NAME));
layout.addComponent(新链接(“注销”,新外部资源(“/j_-spring\u-security\u-Logout”));
按钮按钮=新按钮(“单击我”);
button.addClickListener(新建button.ClickListener(){
公共作废按钮单击(单击事件){
layout.addComponent(新标签(“感谢您点击”);
}
});
布局。添加组件(按钮);
设置内容(布局);
}
@凌驾
公共无效输入(ViewChangeListener.ViewChangeEvent事件){
User User=(User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
列表角色=新的ArrayList();
for(GrantedAuthority GrantedAuthority:user.getAuthories()){
添加(grantedAuthority.getAuthority());
}
usernamelab.setValue(user.getUsername());
rolesLabel.setValue(StringUtils.join(角色,“,”);
}
}
请建议我如何使用这里的两个按钮。如果我单击按钮1,它应该转到
oneView.java
,如果我单击按钮2,它应该转到twoView.java
千万吨提前感谢。:)希望我的导师能给我一个好的回应:) 首先,如果您使用的是SpringBoot,那么其中还包括一章关于导航的内容(即使您没有使用boot,您也会阅读它,因为概念类似)。否则,Vaadin的书中有[对导航功能也有很好的解释],需要对spring()进行一些小的调整。其次,在代码中调用
createNavigationButton
方法的位置…感谢您的响应。我没有使用弹簧靴。我不知道如何在点击按钮上浏览视图。抱歉,我尝试了createNavigationButton。我现在删除那个方法。您可以看到更新的代码。关于这个文件,我在这里写文章之前已经读过了(通常,您必须找到navigator实例并告诉它转到另一个视图,类似于getUI().getNavigator().navigateTo(“viewName”);
。如果您已经尝试过,您遇到了什么问题?另外,您可以在回复某人的评论时使用@username,以便他们得到通知;-)@当我使用button.addClickListener(事件-->getUI().getNavigator().navigateTo(viewName))时会出现问题;显示错误“事件无法解析为变量”,lambda中只有1个减号。它应该是->
而不是-->
,并且还要确保您使用的是JDK 8。