FormEngine Sample Programs

Now we will write one sample login form and registration form using FormEngine Framework.
  1. Property file
  2. XML file

List of files for Sample Program
1.  index.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml" lang="de" xml:lang="de">
      <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title>FormEngine demo application</title>
      </head>
  <body>
    <div id="pageborder">
      <div id="main">
      <h1>FormEngine demo application</h1><br/><br/><br/>
      <%@ include file="includes/menu.jsp" %>
    </div>
  </body>
</html>

In index.jsp doesn't have much logic, just we have included menu.jsp file which has menu details.


2.  menu.jsp
 <div id="menu">
      <ul>
            <li><a href="form.jsp?form=login_page">Login Page</a></li>
            <li><a href="form.jsp?form=register_page">Register page</a></li>
      </ul>
 </div>

Observe the Login page and Registration page links.Same format we have to follow for all the forms. Based form name it will pickup property file and xml file.

form.jsp?form="form name"

3.  form.jsp
<%@ page contentType="text/html;charset=UTF-8"
import="de.imatics.forms.DefinitionFactory,de.imatics.forms.FormContext,
de.imatics.forms.FormDefinition,de.imatics.forms.render.html.HtmlFormRenderer,java.util.Locale"
%>

<html>
<%!
      private static HtmlFormRenderer renderer = new HtmlFormRenderer();
%><%

      final String formName = request.getParameter("form");
      Locale locale = request.getLocale();
      FormDefinition formDefinition = null;
      FormContext form = null;
      if (formName != null) {
            final String formPath = "/forms/" + formName + ".xml";
            formDefinition = DefinitionFactory.getInstance().loadDefinition(formPath);
            System.out.println("formDefinition "+formDefinition);
            form = FormContext.createFormContext(request, formDefinition, locale);
            System.out.println("form "+form);
      }


%>

<head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Example form<%= formName == null ? "s" : " " + formName %></title>
      <%-- includes all required resources for the form --%>
      <%= de.imatics.j2ee.filter.ResourceFilter.getAllRequestIncludes(request) %>      

</head>
<body>

<%
      if (form != null) {
%>

      <div id="headline">
            <h1><%= formName %></h1>
      </div>
<%
          // go to target.jsp after submit
          renderer.renderForm(form, out, "target.jsp");
     }

%>

</body>

</html>


Explanation of code:

1) required imports

 de.imatics.forms.DefinitionFactory,
 de.imatics.forms.FormContext,
 de.imatics.forms.FormDefinition,
 de.imatics.forms.render.html.HtmlFormRenderer, java.util.Locale"

2) get the form name from URL

 final String formName = request.getParameter("form");

3) based on form name, creating path for XML

 final String formPath = "/forms/" + formName + ".xml";

4) loading xml file using FormDefinition object

 formDefinition = DefinitionFactory.getInstance().loadDefinition(formPath);

5) After loading xml creating form using FormContext object

 form = FormContext.createFormContext(request, formDefinition, locale);

6) Rendering page using HtmlFormRenderer

 renderer.renderForm(form, out, "target.jsp");

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

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="http://www.imatics.de/forms/form.xsd"
            render-type="html" i18n-bundle="login_page#form-messages">

      <connector class="com.javaVillage.org.LoggingConnector"/>
      <component name="username" type="text" label-key="username.label"/>
      <component name="pwd" type="text" label-key="pwd.label"/>
</form>

FormEngine supports internalization.Based on below attribute, it will pickup the property file
Property file: form-messages
Property names prefix with: login_page
i18n-bundle="login_page#form-messages"
Property file also will be pickup based Locale.


5.  form-messages.properties
login_page.username.label=Enter User name

login_page.pwd.label=Enter Password


6.  form-engine.properties
engine.dynamic-validation = true
engine.dynamic-readonly = true
engine.dynamic-hidden = true
engine.target-with-form-id = false

7.  LoggingConnector.java
package com.javaVillage.org;
import de.imatics.forms.connector.DataConnector;
import de.imatics.util.naming.HierarchicName;
import org.apache.log4j.Logger;
import java.util.HashMap;
import java.util.Map;

/**
 * Example implementation for a connector.
 * This connector logs every value from the form via log4j
 */
public class LoggingConnector implements DataConnector {
      private static final Logger logger = Logger.getLogger(LoggingConnector.class);
      private Map submittedValues;
      /**
       * Methode is called while loading form for every form component
       * @param cName full name of the component
       * @return
       */
      public Object getValue(HierarchicName cName) {
            return null;
      }

      /**
       * Methode is called while loading form for every form repeat component
       * to retrieve the initial number of rows in repeat.
       * @param rName full name of the repeat component
       * @return
       */
      public int getRepeatCount(HierarchicName rName) {
            return 0;
      }
      /**
       * Is called on start of submit process
       */
      public void startSubmission() {
            logger.info("submission startet");
            submittedValues = new HashMap();
      }

      public void setValue(HierarchicName cName, Object value) {
            logger.info("got value for " + cName.toString() + ": " + value);
            submittedValues.put(cName.toString(), value);
      }

      public void setRepeatCount(HierarchicName rName, int rowCount) {
           logger.info("got repeat count for " + rName.toString() + ": " + rowCount);
            // here you can initialize a sub connector for instance
      }
      /**
       * Is called on end of submit process.
       * @return
       */
     public Object submitValues() {
            // Write your data to your backend. This may be a database or anything else.
           return submittedValues;
      }


8.  web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
      <filter>
            <filter-name>ResourceFilter</filter-name>
            <filter-class>de.imatics.j2ee.filter.ResourceFilter</filter-class>
      </filter>

      <filter>
           <filter-name>ActionFilter</filter-name>
           <filter-class>de.imatics.j2ee.filter.ActionFilter</filter-class>
           <init-param>
                   <param-name>action-mapping</param-name>
                   <param-value>FEAction=de.imatics.forms.render.html.ActionHandler</param-value>
           </init-param>
      </filter>

      <filter>
           <filter-name>SubmissionFilter</filter-name>
           <filter-class>de.imatics.forms.render.html.SubmissionFilter</filter-class>
      </filter>

      <filter-mapping>
           <filter-name>ResourceFilter</filter-name>
           <url-pattern>/*</url-pattern>
      </filter-mapping>

      <filter-mapping>
           <filter-name>ActionFilter</filter-name>
           <url-pattern>/*</url-pattern>
      </filter-mapping>

      <filter-mapping>
            <filter-name>SubmissionFilter</filter-name>
            <url-pattern>/*</url-pattern>
      </filter-mapping>
</web-app>

Same structure we can follow for all the forms, extra we have to mainitain specific xml file for each form. Coming to the property file we can maintain properties in same property file(form-messages.properties) for the labels

Required Jars

FormEngine framework required jars list

Structure of the project

FormeEngine Sample File Structure

Execution of the program

FormEngine Sample Index page

Login page after clicking on Logine_page

FormEngine Sample Login page

Enter the values and submit the page will go to target page

FormEngine Sample submition page

target jsp, just for a message, find the screenshot for code

FormEngine Sample resut page