在managedbeans primefaces之间传递参数

时间:2012-12-23 03:23:45

标签: jsf primefaces

我的管理员有问题。我无法在它们之间传递参数。这是一个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中,我正在检查用户输入是否与数据库记录匹配。如果是,那么我让他进入系统。与此同时,我正在全名。

我的backbean:

@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之间传递这个变量?

提前致谢。

1 个答案:

答案 0 :(得分:7)

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

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

另一个,也许更合适,可能是注入豆本身:

@ManagedProperty("#{loginBean}")
private LoginBean loginBean;