【问题标题】:Display bean value in jsf page在jsf页面中显示bean值
【发布时间】:2015-03-16 10:43:27
【问题描述】:

这是我的LoginBean

@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>

但结果显示为:welcome,而usernamenull

为什么?

【问题讨论】:

  • 您在哪里填充username?它是否填充了null
  • getUserName 方法是什么样的?
  • 另外,为什么 loginBean.currentUser 的类型是 LoginBean?

标签: java jsf-2


【解决方案1】:

您应该使用sessionScoped 而不是ViewScoped bean。

并且您的currentUser 类型应该与您的数据库table 类型相同,类似于Customers,而不是loginBean

然后您可以访问currentUser 的用户名值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 2013-09-25
    • 2012-09-09
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多