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 的导航规则很奇怪,我建议不要将 xml 导航规则与 jsf 2.x 一起使用

action 方法返回的 String 指定将被重定向的页面。

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 回答