Java:Vaadin 登录屏幕 Web 应用程序行为不自然
Java: Vaadin Login-screen Web-App behaves unnaturaly
我目前正在使用 Vaadin 构建 java 服务器端 WebApp。现在,我是 Vaadin 的新手,所以请保持温柔。 ;)
这是我的问题:我目前正在构建登录屏幕。我将用户名和密码设置为全局变量以用于测试目的。在登录屏幕上,用户只需输入他的用户名和密码,在登录按钮上,代码会将文本字段中输入的值与全局变量的值进行比较。如果值匹配,则会弹出一条通知,说明登录成功,否则您只会收到一条错误通知。
问题是:当我调用 username.getValue()
的字符串值(来自文本字段)并将其与存储在全局变量中的用户名进行比较时,if-else 语句总是跳转到 else 分支这表明,由于某些深奥的原因,字符串不匹配。事实是,他们这样做了。只是为了确保我没有监督任何事情,我正在打印控制台上文本字段中输入的值,并且 .getValue()
获取它们的方式与数据存储在全局变量中的方式完全一致。
有谁知道为什么这段代码试图破坏我的理智?
代码如下:
package com.example.vaadintestgui;
import java.util.Locale;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
@SuppressWarnings("serial")
@Theme("vaadintestgui")
public class VaadintestguiUI extends UI {
private static final String username = "admin";
private static final String password = "r00t";
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = VaadintestguiUI.class)
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
UI.getCurrent().setLocale(new Locale("en"));
Page.getCurrent().setTitle("Login");
final TextField uname = new TextField("Username:");
final PasswordField pass = new PasswordField("Password:");
Button loginButton = new Button("Login");
loginButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
Notification.show("Cheking Credentials...");
// Just for debugg purposes:
System.out.println("uname: "+uname.getValue()+" password: "+pass.getValue());
// Something is wrong here....:
if(uname.getValue() == username && pass.getValue() == password){
Notification.show("Login Succesfull!");
} else {
Notification.show("Wrong login credentials. Please try again.");
}
}
});
layout.addComponent(uname);
layout.setComponentAlignment(uname, Alignment.MIDDLE_CENTER);
layout.addComponent(pass);
layout.setComponentAlignment(pass, Alignment.MIDDLE_CENTER);
layout.addComponent(loginButton);
layout.setComponentAlignment(loginButton, Alignment.MIDDLE_CENTER);
}
}
我是 运行 客户端 tomcat8 本地主机服务器上的应用程序(目前)。任何帮助将不胜感激!
比较字符串应始终使用 .equals() 而不是 ==
我目前正在使用 Vaadin 构建 java 服务器端 WebApp。现在,我是 Vaadin 的新手,所以请保持温柔。 ;)
这是我的问题:我目前正在构建登录屏幕。我将用户名和密码设置为全局变量以用于测试目的。在登录屏幕上,用户只需输入他的用户名和密码,在登录按钮上,代码会将文本字段中输入的值与全局变量的值进行比较。如果值匹配,则会弹出一条通知,说明登录成功,否则您只会收到一条错误通知。
问题是:当我调用 username.getValue()
的字符串值(来自文本字段)并将其与存储在全局变量中的用户名进行比较时,if-else 语句总是跳转到 else 分支这表明,由于某些深奥的原因,字符串不匹配。事实是,他们这样做了。只是为了确保我没有监督任何事情,我正在打印控制台上文本字段中输入的值,并且 .getValue()
获取它们的方式与数据存储在全局变量中的方式完全一致。
有谁知道为什么这段代码试图破坏我的理智?
代码如下:
package com.example.vaadintestgui;
import java.util.Locale;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
@SuppressWarnings("serial")
@Theme("vaadintestgui")
public class VaadintestguiUI extends UI {
private static final String username = "admin";
private static final String password = "r00t";
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = VaadintestguiUI.class)
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
UI.getCurrent().setLocale(new Locale("en"));
Page.getCurrent().setTitle("Login");
final TextField uname = new TextField("Username:");
final PasswordField pass = new PasswordField("Password:");
Button loginButton = new Button("Login");
loginButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
Notification.show("Cheking Credentials...");
// Just for debugg purposes:
System.out.println("uname: "+uname.getValue()+" password: "+pass.getValue());
// Something is wrong here....:
if(uname.getValue() == username && pass.getValue() == password){
Notification.show("Login Succesfull!");
} else {
Notification.show("Wrong login credentials. Please try again.");
}
}
});
layout.addComponent(uname);
layout.setComponentAlignment(uname, Alignment.MIDDLE_CENTER);
layout.addComponent(pass);
layout.setComponentAlignment(pass, Alignment.MIDDLE_CENTER);
layout.addComponent(loginButton);
layout.setComponentAlignment(loginButton, Alignment.MIDDLE_CENTER);
}
}
我是 运行 客户端 tomcat8 本地主机服务器上的应用程序(目前)。任何帮助将不胜感激!
比较字符串应始终使用 .equals() 而不是 ==