1

マネージド Bean に問題があります。それらの間でパラメーターを渡すことができません。XHTML スニペットを次に示します。基本的にはログイン用のフォームです。パラメータをバックビーンに送信するだけです。

<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でのコミュニケーションに関するブログ投稿全体を作成しました。これは、本当に時間の価値があります。それを読むと、それを行う方法が複数あることがわかります。そのうちの1つは、他のBeanにプロパティ自体を注入することです。

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

そして、おそらくもっと適切な別の方法は、Bean自体を注入することです。

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