JSF

 

To run it one need: 

 

Docs: 

http://java.sun.com/j2ee/javaserverfaces/1.1/docs/api/

http://java.sun.com/j2ee/javaserverfaces/1.1/docs/tlddocs/

 http://myfaces.apache.org/

 http://horstmann.com/corejsf/faces-config.html

 

Projects: 

 

 

JSF Flow of control 

 

JSF Flow of Control: 

 

Steps using JSF 

 

 

Input Form: 

<%@ page language="java" contentType="text/html; charset=UTF-8" 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">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<f:view>

        HTML markup

    <f:loadBundle basename="org.apache.myfaces.examples.resource.example_messages" var="example_messages"/>

 

        <h:form id="form1">

                        HTML markup and <h:xxxx> tags

            <h:outputLabel for="form1:number1" value="#{example_messages['sample1_number']} 1" />

            <h:outputText value="#{validationController.number1ValidationLabel}"/>

            <f:verbatim>: </f:verbatim>

            <h:inputText id="number1" value="#{calcForm.number1}" maxlength="10" size="25" required="true" >

               <f:validateLongRange minimum="1" maximum="10" />

            </h:inputText>

            <h:message id="number1Error" for="form1:number1" styleClass="error" /><f:verbatim><br></f:verbatim>

 

                </h:form>

 

</f:view>

</body>

</html>

 

 

public class ValidationController

    public String getNumber1ValidationLabel()

    {

        FacesContext facesContext = FacesContext.getCurrentInstance();

        UIInput number1 = (UIInput)facesContext.getViewRoot().findComponent("form1:number1");

        Validator[] validators = number1.getValidators();

        if (validators != null && validators.length > 0)

        {

            long min = ((LongRangeValidator)validators[0]).getMinimum();

            long max = ((LongRangeValidator)validators[0]).getMaximum();

            return " (" + min + "-" + max + ")";

        }

        else

        {

            return "";

        }

    }

 

 

 

public class CalcForm

    implements Serializable

    /**

     * serial id for serialisation versioning

     */

    private static final long serialVersionUID = 1L;

    private BigDecimal number1 = new BigDecimal(0d);

    private BigDecimal number2 = new BigDecimal(0d);

    private BigDecimal result = new BigDecimal(0d);

 

    public void add()

    {

        result = number1.add(number2);

    }

 

    public void subtract()

    {

        result = number1.subtract(number2);

    }

 

    public BigDecimal getNumber1()

    {

        return number1;

    }

 

    public void setNumber1(BigDecimal number1)

    {

        this.number1 = number1;

    }

 

    public BigDecimal getNumber2()

    {

        return number2;

    }

 

    public void setNumber2(BigDecimal number2)

    {

        this.number2 = number2;

    }

 

    public BigDecimal getResult()

    {

        return result;

    }

 

    public void setResult(BigDecimal result)

    {

        this.result = result;

    }

 

 

 

 

 

example-config.xml 

 

<?xml version="1.0"?>

 

<!DOCTYPE faces-config PUBLIC

  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"

  "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

 

<!-- =========== FULL CONFIGURATION FILE ================================== --> 

 

<!-- simple web app -->

 

<faces-config>

 

    <application>

        <variable-resolver>org.apache.myfaces.examples.accessedbeans.AccessTrackingVariableResolver</variable-resolver>

    </application>

 

 

        <managed-bean>

                <managed-bean-name>environmentInfo</managed-bean-name>

                <managed-bean-class>org.apache.myfaces.examples.EnvironmentInfo</managed-bean-class>

                <managed-bean-scope>application</managed-bean-scope>

        </managed-bean>

 

        <!-- Accessed beans on the last request -->

    <managed-bean>

        <managed-bean-name>accessedBeans</managed-bean-name>

        <managed-bean-class>org.apache.myfaces.examples.accessedbeans.AccessedBeans</managed-bean-class>

        <managed-bean-scope>request</managed-bean-scope>

    </managed-bean>

 

    <!-- Managed Beans for sample1.jsp -->

 

    <managed-bean>

        <managed-bean-name>calcForm</managed-bean-name>

        <managed-bean-class>org.apache.myfaces.examples.example1.CalcForm</managed-bean-class>

        <managed-bean-scope>request</managed-bean-scope>

    </managed-bean>

 

    <managed-bean>

        <managed-bean-name>validateForm</managed-bean-name>

        <managed-bean-class>org.apache.myfaces.examples.validate.ValidateForm</managed-bean-class>

        <managed-bean-scope>request</managed-bean-scope>

    </managed-bean>

   

        <navigation-rule>

                <from-view-id>/schedule_editsettings.jsp</from-view-id>

                <navigation-case>

                        <from-outcome>success</from-outcome>

                        <to-view-id>/schedule3.jsp</to-view-id>

                        <redirect />

                </navigation-case>

                <navigation-case>

                        <from-outcome>cancel</from-outcome>

                        <to-view-id>/schedule3.jsp</to-view-id>

                        <redirect />

                </navigation-case>

                <navigation-case>

                        <from-outcome>failure</from-outcome>

                        <to-view-id>/schedule_editsettings.jsp</to-view-id>

                        <redirect />

                </navigation-case>

        </navigation-rule>

 

</faces-config>

 

 

web.xml 

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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" version="2.4">

  <description>debug web.xml</description>

  <context-param>

    <description>Comma separated list of URIs of (additional) faces config files.

            (e.g. /WEB-INF/my-config.xml)

            See JSF 1.0 PRD2, 10.3.2

            Attention: You do not need to put /WEB-INF/faces-config.xml in here.

    </description>

    <param-name>javax.faces.CONFIG_FILES</param-name>

    <param-value>/WEB-INF/examples-config.xml,/WEB-INF/testSuite-config.xml</param-value>

  </context-param>

  <context-param>

    <description>State saving method: "client" or "server" (= default)

            See JSF Specification 2.5.3</description>

    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>

    <param-value>client</param-value>

  </context-param>

  <context-param>

    <description>Only applicable if state saving method is "server" (= default).

            Defines the amount (default = 20) of the latest views are stored in session.</description>

    <param-name>org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION</param-name>

    <param-value>20</param-value>

  </context-param>

 

 

  <filter>

    <filter-name>extensionsFilter</filter-name>

    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>

    <init-param>

      <description>Set the size limit for uploaded files.

                Format: 10 - 10 bytes

                        10k - 10 KB

                        10m - 10 MB

                        1g - 1 GB</description>

      <param-name>uploadMaxFileSize</param-name>

      <param-value>100m</param-value>

    </init-param>

    <init-param>

      <description>Set the threshold size - files

                    below this limit are stored in memory, files above

                    this limit are stored on disk.

 

                Format: 10 - 10 bytes

                        10k - 10 KB

                        10m - 10 MB

                        1g - 1 GB</description>

      <param-name>uploadThresholdSize</param-name>

      <param-value>100k</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>extensionsFilter</filter-name>

    <url-pattern>*.jsf</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>extensionsFilter</filter-name>

    <url-pattern>/faces/*</url-pattern>

  </filter-mapping>

  <servlet>

    <servlet-name>Faces Servlet</servlet-name>

    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet>

    <servlet-name>SourceCodeServlet</servlet-name>

    <servlet-class>org.apache.myfaces.shared_tomahawk.util.servlet.SourceCodeServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>Faces Servlet</servlet-name>

    <url-pattern>*.jsf</url-pattern>

  </servlet-mapping>

  <servlet-mapping>

    <servlet-name>SourceCodeServlet</servlet-name>

    <url-pattern>*.source</url-pattern>

  </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

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

  </welcome-file-list>

        <error-page>

                <error-code>500</error-code>

                <location>/error.jsp</location>

        </error-page>

</web-app>

 

 

 

some form tags: 

 <h:inputText/> => <input type="text">

 <h:inputsecret/> => <input type="password">

 <h:commandButton action="success" value="Submit" /> => <input type="submit">

 <h:outputText/> => Ordinary text