我是Vaadin的新人,我遇到了问题。
我试过这个:https://vaadin.com/web/magi/home/-/blogs/model-view-presenter-pattern-with-vaadin并且示例应用程序还可以,但是当我尝试创建类似的东西时,它对我不起作用。
我有一个简单的登录表单(登录名,密码字段和一个按钮)。按钮有问题。它没有被触发。我可以单击,但操作未启动。
这是我的代码:
public interface LoginView {
public void getNotification(String text);
interface LoginViewListener{
void loginButtonClick(String login, String password);
}
public void addListener(LoginViewListener listener);
}
public class LoginViewImpl extends CustomComponent implements LoginView,Button.ClickListener {
private TextField loginField = new TextField();
private PasswordField passwordField = new PasswordField();
private Label loginLabel = new Label("Login:");
private Label passwordLabel = new Label("Password:");
private Button loginButton = new Button("Login");
LoginViewListener listener;
public LoginViewImpl(){
VerticalLayout layout = new VerticalLayout();
layout.setSizeFull();
layout.addComponent(loginLabel);
layout.addComponent(loginField);
layout.addComponent(passwordLabel);
layout.addComponent(passwordField);
layout.addComponent(loginButton);
setCompositionRoot(layout);
}
public void getNotification(String text){
Notification.show(text);
}
@Override
public void buttonClick(Button.ClickEvent event) {
listener.loginButtonClick("jeden","dwa");
}
public void addListener(LoginViewListener listener) {
this.listener = listener;
}
}
public class LoginModel {
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String login;
private String password;
}
public class LoginPresenter implements LoginView.LoginViewListener {
LoginModel model;
LoginView view;
public LoginPresenter(LoginModel model, LoginView view){
this.model = model;
this.view = view;
view.addListener(this);
}
@Override
public void loginButtonClick(String login, String password) {
view.getNotification(login + " " + password);
}
}
@SpringUI(path = "")
public class MainClass extends UI {
@Override
protected void init(VaadinRequest request) {
LoginModel model = new LoginModel();
LoginViewImpl view = new LoginViewImpl();
new LoginPresenter(model, view);
VerticalLayout layout = new VerticalLayout();
layout.setSizeFull();
layout.addComponent(view);
setContent(layout);
}
}
你能告诉我什么是错的吗? :(
答案 0 :(得分:1)
问题是LoginViewImpl
不会收听loginButton
事件。
查看按钮在the MVP tutorial中的创建方式:
// Add buttons and have them send click events
// to this class
for (String caption: operations)
layout.addComponent(new Button(caption, this));
请注意接受ClickListener
的{{3}}的使用情况。这种方式将视图实现注册为按钮的单击事件的处理程序。
您应该在代码中执行类似的操作:
public LoginViewImpl(){
//skip
loginButton.addClickListener(this);
layout.addComponent(loginButton);
setCompositionRoot(layout);
}