1

我的托管豆有问题。我无法设法在它们之间传递参数。这是一个 XHTML 片段。它基本上是一个登录表单。它只是将参数发送到后台bean。

<h:outputLabel for="username" value="Kullanıcı Adı: *" />
  <p:inputText id="username" value="#{loginBean.username}" required="true" requiredMessage="Kullanıcı adı giriniz.">
    <f:validateLength minimum="2" />
  </p:inputText>
  <p:message for="username" display="icon"/>
  <h:outputLabel for="password" value="Şifre: *" />
  <p:inputText id="password" value="#{loginBean.password}" required="true" requiredMessage="Şifreyi giriniz!" type="password">
    <f:validateLength minimum="2" />
  </p:inputText>
  <p:message for="password" id="msgPass" display="icon"/>
  <f:facet name="footer">
    <center>
      <p:commandButton id="submit" value="Giriş" icon="ui-icon-check" action="#{loginBean.check}" style="margin:0" update="grid"/>
    </center>
  </f:facet>

在我的支持 bean 中,我正在检查用户输入是否与数据库记录匹配。如果是,那我让他进入系统。同时,我要取他的全名。

我的豆豆:

@ManagedBean
@RequestScoped
public class LoginBean {
  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 String getMgs() {
    return mgs;
  }
  public void setMgs(String mgs) {
    this.mgs = mgs;
  }
  public String getFullname() {
    return fullname;
  }
  public void setFullname(String fullname) {
    this.fullname = fullname;
  }
  public String getOriginalURL() {
    return originalURL;
  }
  public void setOriginalURL(String originalURL) {
    this.originalURL = originalURL;
  }
  private String username;
  private String password;
  private String mgs;
  private String fullname;
  private String originalURL;
  private static Logger log = Logger.getLogger(LoginBean.class.getName());
  public String check() throws Exception {
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projetakip", "root", "");
    Statement stmt = con.createStatement();
    String md5Pass = md5(password);
    String SQL = "select * from users where username='" + username + "' and password='" + md5Pass + "'";
    ResultSet rs = stmt.executeQuery(SQL);
    while (rs.next()) {
      if (username.matches(rs.getString("username")) && md5Pass.matches(rs.getString("password"))) {
        this.fullname = rs.getString("ad") + " " + rs.getString("soyad");
        return "panel?faces-redirect=true";
      } else {
        FacesMessage msg = new FacesMessage("Yanlış kullanıcı adı/şifre.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return "index?faces-redirect=true";
      }
    }
    return "index?faces-redirect=true";
  }
  public void getProductSetupData(ActionEvent event) {
    FacesContext context = FacesContext.getCurrentInstance();
    Data data = context.getApplication().evaluateExpressionGet(context, "#{data}", Data.class);
  }

我想要的是传递fullName给其他bean(或页面)。如何在我的 bean 之间传递这个变量?

提前致谢。

4

1 回答 1

7

BalusC 写了一篇关于JSF 2.0 通信的完整博客文章,真的值得你花时间。阅读它,您会发现有不止一种方法可以做到这一点,其中一种是在您的其他 bean 中注入属性本身:

@ManagedProperty("#{loginBean.fullName}")
private String fullName;

另一个可能更合适的方法是注入 bean 本身:

@ManagedProperty("#{loginBean}")
private LoginBean loginBean;
于 2012-12-23T11:37:54.743 回答