0

I am learning JSF for the first time. I have created a small Login project with 4 files: 1.User.java 2.Login.jsp 3.Loginfailed.jsp 4.faces-config.xml 5.Sucess.jsp

I want to navigate to the page "Success.jsp" if username and password match and to "Loginfailed.jsp" if it doesnt. But I do not how to put that check and where to put it and how to set the navigators in "faces-config.xml".

This is my code: 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";
        }

      }

    }

Login.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>

faces-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>
Nick Div
  • 5,338
  • 12
  • 65
  • 127
  • Are you really learning JSF2? JSP is deprecated since JSF2 and succeeded by Facelets (XHTML). I recommend to review your learning resources if you aren't accidentally learning JSF1. – BalusC Nov 29 '13 at 09:13

2 Answers2

1

navigation rules via faces-config.xml is odd and i suggest not to use xml navigation rules with jsf 2.x

The returned String from action method specifies the page which will be redirected.

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

If you want to redirect an exact URL you can use the following code snipped.

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect("URL");
erencan
  • 3,725
  • 5
  • 32
  • 50
0

I am sorry to bother all the readers, but I figured out an answer. Just in case someone else comes across the same situation, this is what solved the issue:

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

This in the faces-config.xml did the trick

Nick Div
  • 5,338
  • 12
  • 65
  • 127