FormView

Overview

WEB application often contains CRUD (CREATE, UPDATE, READ, DELETE) forms. According these states (CREATE, READ...) render of fields of form depends on state. Form must for instance display value into HTML input for CREATE state, and write value for READ state. For CREATE, UPDATE form you would like for example display :

and for READ form you would like display :

That's to say, for CREATE form, you want generate HTML :

  <input type="text" name="project" value="Project name 3" />

for READ form you want generate HTML :

  <input type="text" name="project" value="Project name 3" readonly="readonly" />

or

  Project name 3

To do this case with JSP, you can :

  • create one JSP for CREATE form and one JSP for READ form.
  • create one JSP with condition on state (CREATE, READ) into your JSP for generate HTML input or write value.

These solutions are borring, when you must add new field into your JSP (manage two JSP for the first solution or two fields for the second solution). When you must manage Workflow with several states, you must create one JSP per states or create on JSP with several condition on states. It begins awfull to manage your project.

A better solution is to develop specifical taglib which manage state of HTML field, like struts-layout. This solution is good, but you must use specifical taglib to manage state in your JSP. If your company has developed their own taglib, you can't use struts-layout for instance. Moreever it is difficult with taglib to configurate HTML generated (add readonly or write value for READ form). FormView aims at manage these problems.

What is FormView ?

FormView is JSP taglib to manage postrender fields of form according to a state and fields characteristcs. FormView Tag update HTML fields after than HTML was generated by JSP (JSP which contains element HTML or any JSP taglib). This postrender of HTML fields depends on :

  • a state (CREATE, READ, UPDATE..).
  • fields characteristic (MAXLENGTH (maxlength of field), DATE (field is date or not), and REQUIRED (field is required or not).

So with Formview you develop just ONE JSP to manage READ-ONLY and READ-WRITE form. It seems like struts-layout, but with FormView you can manage simple HTML input or any JSP taglib which generate HTML input (eg : html:text taglib for struts).

After configure FormView, to use form view in your only ONE JSP add just taglib formview:page between your form like this :

   <%@ taglib uri="/WEB-INF/form-view.tld" prefix="formview" %>
   ....
   
   <formview:page>
    <!-- HTML input text -->
    <input type="text" name="project" value="Struts Project" />
    <!-- Struts HTML input text -->
    <html:text property="projectDate" />
     .... other JSP taglib.... 
   </formview:page>

To display form with READ state, you save state in the request like this :

 request.setAttribute(FormViewConstants.REQUEST_FORMSTATE_KEY, FormViewConstants.STATE_READ);

HTML rendered will be for instance :

    <!-- HTML input text -->
    <input type="text" name="project" value="Struts Project" readonly="readonly"/>
    <!-- Struts HTML input text -->
    <input type="text" name="projectDate" value="" readonly="readonly"/>
    ....

FormView with Struts

FormView can be used with Struts. After configure FormView, to save state and characteristics about field you can :

  • use your own Action/DispatchAction :
      public final ActionForward loadRead(ActionMapping mapping, ActionForm form,
                            HttpServletRequest request, HttpServletResponse response)
                            throws Exception {
        // Set state to CREATE
        WEBFormViewUtil.saveState(request, getServlet().getServletContext(), FormViewConstants.STATE_READ);
        // Save characteristics fields of form 
        WEBFormViewUtil.saveFormView(request, getServlet().getServletContext(), mapping.getPath());
        return mapping.getInputForward();
      } 
  • implement load and display dispatch of class FormViewDispatchAction and after call ActionForward loadRead to set state to read, loadCreate to set state to create and loadUpdate to set state to update.

It's very interesting to use FormView with the Struts Validator (validation.xml) because Validator information can be merged with FormView to give more information about field :

  • if field depends on maxlength into validation XML, you could add attribute maxlength to HTML input text automaticly.
  • if field depends on required into validation XML, you could add red character * automaticly after input text.
  • if field depends on date into validation XML, you could add Calendar picture automaticly after input text.

For instance if you have validation XML like this :

  ....
  <form name="/project">
    <field property="project" depends="required,maxlength">
      <arg0 key="project.error"/>
      <arg1 name="maxlength" resource="false" key="${var:maxlength}"/>
      <var>
        <var-name>maxlength</var-name>
          <var-value>50</var-value>
        </var>
    </field>            
    <field property="projectDate" depends="date" />
  </form>
  ....

And if you register state to CREATE, HTML rendered will be for instance :

    <!-- HTML input text -->
    <input type="text" name="project" value="Struts Project" maxlength="50" /><b>*</b>
    <!-- Struts HTML input text -->
    <input type="text" name="projectDate" value="" /><img src="/img/calendar.gif" />
    ....