JuFree
JuFree

Reputation: 23

Value can't be set in managedBean in jsf2 and tomcat6

I'm very new to jsf and doing a login sample.

The page can display and when I click submit button, the logic method is invoked but the

setter method is not , the value is null. After that, it redirects to the welcome.jsp (just

one line).

Here is my code:

web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>JSF Sample</display-name>  

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>
        javax.faces.webapp.FacesServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>    

index.html

 <html>
<head><title>Redirecting to main page ...</title></head>
<meta HTTP-EQUIV='Refresh' CONTENT='0;URL=/jsfSample/index.faces'>
</html>

index.jsp

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>  
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<f:view>
<head>
    <title>This is a sample</title>
</head>
<h:form>
    <table>
        <tr>
            <td>id:</td>
            <td><h:inputText value="#{user.id}" required="true"/></td>
        </tr>
        <tr>
            <td>password:</td>
            <td><h:inputSecret value="#{user.password}"         
required="true"/></td>
        </tr>
    </table>
    <p>
        <h:commandButton value="login" action="#{user.login}"/>
    </p>
</h:form>
</f:view>

faces-config.xml

<?xml version="1.0"?>  
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"  
version="2.0">
<navigation-rule>
    <from-view-id>/index.jsp</from-view-id>
    <navigation-case>
        <from-action>#{user.login}</from-action>
        <from-outcome>welcome</from-outcome>
        <to-view-id>/welcome.jsp</to-view-id>
    </navigation-case>
</navigation-rule>  

<managed-bean>
    <managed-bean-name>user</managed-bean-name>
    <managed-bean-class>com.test.jsf.UserBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

</faces-config>

and bean class:

package com.test.jsf;

public class UserBean implements java.io.Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String id;
private String password;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

public String login() {
    System.out.println("id=" + id);
    System.out.println("password=" + password);

    System.out.println("111111111");
    return "welcome";
}
}

The tomcat I used is 6.0.32 and all the jars are

jsf-api-2.1.3.jar, jsf-impl-2.1.3.jar, standard-1.1.2.jar, jstl-1.2.jar

and the jsf should be the sun implementation.

I've searched for almost one day but no cause found.

Very appreciate and thanks a lot!

Upvotes: 0

Views: 575

Answers (1)

BalusC
BalusC

Reputation: 1108567

Please pay attention to the versions. Please pay attention to the versions mentioned and discussed in the books/tutorials you're reading and which you're really using. They have to match up.

At least the following things are not right.

  • Tomcat 6 is a Servlet 2.5 container, but you've declared your web.xml conform Servlet 2.4 which causes Tomcat 6 to run in Servlet 2.4 fallback modus.

  • JSF 2.1 requires Servlet 3.0, so you would need Tomcat 7 with a Servlet 3.0 compatible web.xml. If you want to stick to Tomcat 6, you would need JSF 2.0 which requires Servlet 2.5.

  • Since JSF 2.0, JSP is deprecated as default view technology and succeeded by Facelets. Do not use JSP anymore.

  • The navigation rule and managed bean declarations in faces-config.xml are typical for JSF 1.x, but not for JSF 2.x anymore. This fact, combined with using JSP instead of Facelets and still saying "Sun JSF", suggests that you were trying to learn JSF by a JSF 1.x targeted resource.

That's too much wrong. It's impossible to pinpoint the real cause of your problem because there's too much wrong. I recommend to stop with whatever you're doing now, throw away the entire project setup in the trashcan and then concentrate on finding a proper JSF 2.x tutorial/book/resource. Do not search for JSF 1.x targeted ones.

At the bottom of our JSF wiki page you can find several tutorial links.

Upvotes: 1

Related Questions