0

私は初めてJSFを学んでいます。4 つのファイルで小さなログイン プロジェクトを作成しました: 1.User.java 2.Login.jsp 3.Loginfailed.jsp 4.faces-config.xml 5.Sucess.jsp

ユーザー名とパスワードが一致する場合は「Success.jsp」ページに移動し、一致しない場合は「Loginfailed.jsp」ページに移動します。しかし、そのチェックをどのように配置するか、どこに配置するか、「faces-config.xml」でナビゲーターを設定する方法はわかりません。

これは私のコードです: User.java:

package test;

    public class User {
      private String name;
      private String password;

      public String getName() {
        return name;
      }
      public void setName(String name) {
        this.name = name;
      }
      public String getPassword() {
        return password;
      }
      public void setPassword(String password) {
        this.password = password;
      }

      public String login(){
        // Image here a database access to validate the users
        if (name.equalsIgnoreCase("tester") && password.equalsIgnoreCase("tester")){
          return "success";
        } else {
          return "failed";
        }

      }

    }

ログイン.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<f:view>
  <f:loadBundle basename="messages.messages" var="msg" />
  <h:form>
    <h:panelGrid columns="2">
      <h:outputLabel value="#{msg.user}"></h:outputLabel>
      <h:inputText value="#{user.name}">      
      </h:inputText>
      <h:outputLabel value="#{msg.password}"></h:outputLabel>
      <h:inputSecret value="#{user.password}">
      </h:inputSecret>
    </h:panelGrid>
    <h:commandButton action="#{user.login}" value="#{msg.login}"></h:commandButton>    
  </h:form>
</f:view>
</body>
</html>

顔-config.xml:

<faces-config>
    <managed-bean>
        <managed-bean-name>user</managed-bean-name>
        <managed-bean-class>test.User</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>
4

2 に答える 2

1

faces-config.xml によるナビゲーション ルールは奇妙です。jsf 2.x で xml ナビゲーション ルールを使用しないことをお勧めします。

アクション メソッドから返された文字列は、リダイレクトされるページを指定します。

public String myAction()
{
    return "navigatedPage";
}

正確な URL にリダイレクトする場合は、次のコードを抜粋して使用できます。

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect("URL");
于 2013-11-29T08:33:38.657 に答える
0

読者の皆様にはご迷惑をおかけして申し訳ありませんが、答えを見つけました。他の誰かが同じ状況に遭遇した場合に備えて、これが問題を解決したものです:

<navigation-rule>
   <from-view-id>/pages/LoginView.jsp</from-view-id>
    <navigation-case>
     <from-outcome>success</from-outcome>
     <to-view-id>/pages/Trainer.jsp</to-view-id>
   </navigation-case>
  </navigation-rule>
<navigation-rule>
   <from-view-id>/pages/LoginView.jsp</from-view-id>
    <navigation-case>
     <from-outcome>failed</from-outcome>
     <to-view-id>/pages/FailedLogin.jsp</to-view-id>
   </navigation-case>
  </navigation-rule>

これはfaces-config.xmlでうまくいきました

于 2013-11-29T08:34:05.953 に答える