Thursday, October 21, 2010

Problems if <h:form> is not used in JSF

I will be explainingpProblems if <h:form> is not used in JSF with a simple example.
1)I created a new project in JSF and added comman button to the jsp page  without using <h:form&gt tag
<%@page contentType="text/html" pageEncoding="UTF-8"%>

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%--
    This file is an entry point for JavaServer Faces application.
--%>
<f:view>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>JSP Page</title>
        </head>
        <body>
            <h1><h:outputText value="JavaServer Faces"/></h1>
                <h:commandButton id="bt_SAVE"  value="Save" action="#{test.saveProcess}" />
        </body>
    </html>
</f:view>

2) Made necessary mapping in faces-config.xml.
<managed-bean>
        <managed-bean-name>test</managed-bean-name>
        <managed-bean-class>bean.test</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean> 


3) Created package bean and added test class to the bean package
 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

/**
 *
 * @author subin_s
 */
public class test {

    public void saveProcess() {
        System.out.println("Debug inside saveProcess");
    }
}



Now if you run the project and click on save button  saveProcess method will not be executed you may think why button is not working ,problem is  you have not given <h:form> tag.

No comments:

Post a Comment