Java 在jsf页面中显示bean值
这是我的Java 在jsf页面中显示bean值,java,jsf-2,Java,Jsf 2,这是我的登录栏: @ManagedBean @ViewScoped public class LoginBean implements Serializable { private String email; private String password; private LoginBean currentUser; CustomerService customerService = new CustomerService(); public String login() { c
登录栏
:
@ManagedBean
@ViewScoped
public class LoginBean implements Serializable {
private String email;
private String password;
private LoginBean currentUser;
CustomerService customerService = new CustomerService();
public String login() {
currentUser = customerService.findCustomerByUserPass(email, password);
if (getCurrentUser() != null) {
username = customerService.getCustomerUsername(email);// i want to display this in xhtml page
return "Home2?faces-redirect=true";
}
else{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Unknown login, try again"));
return (email = password = null);
}
}
public boolean isLoggedIn() {
return getCurrentUser() != null;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public LoginBean getCurrentUser() {
return currentUser;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public LoginBean getCurrentUser() {
return currentUser;
}
}
这是我的jsf
页面:
<?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://xmlns.jcp.org/jsf/html">
<h:head>
</h:head>
<h:body>
<h:panelGroup rendered="#{loginBean.loggedIn}">
<p>Welcome #{loginBean.currentUser.username} </p>
</h:panelGroup>
</h:body>
</html>
欢迎#{loginBean.currentUser.username}
但结果显示为:welcome
,username
为null
为什么?您应该使用
sessionScoped
而不是ViewScoped
bean
您的currentUser
类型应该与数据库表的类型相同,类似于客户的类型,而不是登录域的类型
然后您可以访问当前用户的用户名值
在哪里填充用户名
?它是否填充了null
也许?getUserName方法是什么样子的?还有,为什么loginBean.currentUser的类型是loginBean?