tags:

views:

302

answers:

1

In an simple web application I have to combine jsp with jsf (for a school assignment). I am using NetBeans 6.7.1. In my login page I have the following code:

<html>
<body>
    <form name="form" method="post" action="processuserinfo.jsp" >
        UserName: <input type="text" name = "username" value=""><br>
        Password: <input type="password" name="userpassword" value=""><br>
        <input type="submit" name="submit" value="Login">
    </form>
</body>
</html>
</body>

The processuserinfo.jsp:

<%@page import="java.util.*" %>
<jsp:useBean id="idhandler" class="dsassignment1java.LoginManager" scope="session">
<jsp:setProperty name="idhandler" property="*"/>
</jsp:useBean>
<% idhandler.validateUser(session, pageContext); %>

However, the method validateUser never gets called. I assume it has something to do with the navigation rules defined in faces-config.xml, but I do not know how to properly set a rule. I've tried to define a rule like this:

<navigation-case>
        <from-outcome>goProcessUserInfo</from-outcome>
        <to-view-id>/processuserinfo.jsp</to-view-id>
    </navigation-case>

and specify goProcessUserInfo as the action for my form but it doesn't work.

+2  A: 

You aren't using JSF in any way - this is a plain HTML page that invokes a JSP (which doesn't contain any JSF tags).

To reformulate the login page as a JSF JSP, you might write it something like this:

<?xml version="1.0" encoding="UTF-8" ?><jsp:root
  xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"&gt;&lt;jsp:directive.page
    language="java"
    contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" /><jsp:text><![CDATA[<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; ]]>
    </jsp:text>
  <html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
  <f:view>
    <h:form>
      user: <h:inputText value="#{idhandler.username}" /><br />
      pw: <h:inputText value="#{idhandler.password}" /><br />
      <h:commandButton value="login" action="#{idhandler.validateUser}" />
    </h:form>
  </f:view>
  </body>
  </html>
</jsp:root>

This defines a JSF view with a form control that contains two text input controls and a button. The inputs and button are bound to a managed bean. The managed bean might look like this:

public class LoginManager {
  private String username;
  private String password;

  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 validateUser() {
    // TODO: check login details
    boolean authenticated = true;
    return authenticated ? "goProcessUserInfo" : "errorPage";
  }
}

The return value of the validateUser method is used for matching the <from-outcome> of the navigation case. The managed bean would be defined in your faces-config.xml:

  <managed-bean>
    <managed-bean-name>idhandler</managed-bean-name>
    <managed-bean-class>dsassignment1java.LoginManager</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>

FYI: when writing JSF applications, you minimise the use of any standard actions (jsp:... tags in the http://java.sun.com/JSP/Page namespace), usually avoid JSTL tags (<c:... tags in the http://java.sun.com/jsp/jstl/... namespaces) and never use scriptlets (<% ... %> view-embedded code).

See the JavaServer Faces section of the JEE5 tutorial for more.

McDowell