这是我在这个美丽网站上的第一个问题。我用谷歌搜索了很多,但我没有找到任何解决方案。
我是 JSF 的新手,我正在使用 Kent Ka lok Tong 的“JSF 2 APIs and JBoss Seam”来学习它。
现在我遇到了一个简单的登录实现问题。我有一个登录页面:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Login</title>
</h:head>
<h:body>
<h1>Login</h1>
<h:messages for="loginForm" />
<h:form id="loginForm">
<h:inputText id="username" value="#{loginRequest.username}" required="true" />
<h:inputSecret id="password" value="#{loginRequest.password}" required="true" />
<h:commandButton value="Login" action="#{loginRequest.doLogin}"></h:commandButton>
</h:form>
</h:body>
</html>
和一个支持bean:
package app.controller;
import app.model.beans.User;
import javax.faces.bean.RequestScoped;
import javax.inject.Named;
@Named("loginRequest")
@RequestScoped
public class LoginRequest {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public LoginRequest(){
System.out.println("created " + this.toString());
}
public String doLogin(){
if(this.username != null && this.password != null){
if(this.username.equals("user") && this.password.equals("password")){
//this.userHolder.setCurrentUser(username);
return "success";
}
return "failure";
}
return "failure";
}
}
当我运行应用程序时,我的用户名和密码属性结果为空。我调试了我的应用程序,我看到 setters 方法被正确调用。问题是,当调用 setUsername 时,存在 LoginRequest 的实例,而当调用 setPassword 函数时,实例是不同的!似乎应用程序这样做:
obj1 = new LoginRequest() //username and password = null;
obj1.username = username;
obj1 = new LoginRequest() //username and password = null;
obj1.password = password;
obj1 = new LoginRequest() //username and password = null;
obj1.doLogin();
我哪里有麻烦了?错误在哪里?
非常感谢!
最好的祝福
马可