Manual with Struts

Introduction

This section describes how integrate quickly FormView with Struts with basic configuration of FormView. It is based on an example which must display a form which manage CREATE, UPDATE and READ Project characteristics. It contains :

  • field with name projectId. This field is all the time HIDDEN.
  • field with name projectName. This field is required and maxlength="50". This field is HTML input at READ-WRITE state.
  • field with name projectDate. This field is not required and is date. This field is HTML input at READ-WRITE state with calendar picture.
  • field with name managerId. This field is not required and contains list of managers. This field is HTML select at READ-WRITE state.

Fields validation depends on required, maxlength and date .

Form Struts

Create ProjectForm form with the for fields :

public class ProjectForm extends ActionForm {

        private String projectId;
        private String projectName;
        private String projectDate;
        private String managerId;
        
        public String getManagerId() {
                return managerId;
        }
        public void setManagerId(String managerId) {
                this.managerId = managerId;
        }
        public String getProjectDate() {
                return projectDate;
        }
        public void setProjectDate(String projectDate) {
                this.projectDate = projectDate;
        }
        public String getProjectId() {
                return projectId;
        }
        public void setProjectId(String projectId) {
                this.projectId = projectId;
        }               
        public String getProjectName() {
                return projectName;
        }
        public void setProjectName(String projectName) {
                this.projectName = projectName;
        }       
}

Action Struts

Create ProjectAction which implements methods load and display of class FormViewDispatchAction :

  public ActionForward load(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, 
                            HttpServletResponse response) throws Exception { 
    ProjectForm form = (ProjectForm)actionForm;
    // Load Project if Id project is null (UPDATE) and do nothing otherwise
    if (form.getProjectId() != null && form.getProjectId().length() > 0) {
      // Load Project with id get project ID and update form with value of project.
    }
    return display(mapping, form, request, response);
  }
  
  public ActionForward display(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, 
                            HttpServletResponse response) throws Exception { 
      // Load list of managers and save it into request
      List managerList = ....
      request.setAttribute("managerList", managerList);
      return mapping.findForward("display");
  }
  

Action & form into Struts-config

Declare form and action into struts-config :

  ....
  <form-bean name="projectForm" 
             type="net.sourceforge.formview.usecases.form.ProjectForm"/>                                                                        
                  
  ....
  <action path="/project" 
          parameter="dispatch" 
          name="projectForm"   
          validate="false" 
          scope="request" 
          type="net.sourceforge.formview.usecases.action.ProjectAction" >                       
          <forward name="display" path="/project.jsp" redirect="false"/>
  </action>
  ....

Validation.xml

Create XML file /WEB-INF/validation.xml :

  <form name="/project">
          <field property="projectName" depends="required,maxlength">
                  <arg0 key="projectForm.projectName.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">
                  <arg0 key="projectForm.projectDate.error"/>
                  <var>
                          <var-name>datePattern</var-name>
                          <var-value>${datePattern}</var-value>
                  </var>
          </field>                                      
  </form>

Form-view.xml

Create /WEB-INF/form-view.xml like this :

  <?xml version="1.0" encoding="UTF-8"?>
  <form-view>
          <!-- Definition about CRU states (CREATE, READ and UPDATE) -->
          <states>
                  <state name="CREATE" behaviour="READ-WRITE" />
                  <state name="READ" behaviour="READ-ONLY" />
                  <state name="UPDATE" behaviour="READ-WRITE" />
          </states>  
  </form-view>        

FormView Plugin

Add line into struts-config.xml to configure FormView plugin and Validator Plugin :

      <!-- FormView Plugin -->
      <plug-in className="net.sourceforge.formview.struts.plugin.FormViewPlugIn">
              <set-property property="formViewConfig" value="/WEB-INF/form-view.xml"/>
              <set-property property="validatorConfig" value="/WEB-INF/validation.xml"/>
      </plug-in>        
      
      <!-- Validator Plugin -->
      <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
      </plug-in>              

By default, FormView manage input type="text" and select HTML. If you want customize your render for another HTML elements or manage another state, you must parameter your file displayer-config my-displayer-config.xml and register it with plugin like this :

      <!-- FormView Plugin -->
      <plug-in className="net.sourceforge.formview.struts.plugin.FormViewPlugIn">
              <set-property property="formViewConfig" value="/WEB-INF/form-view.xml"/>
              <set-property property="validatorConfig" value="/WEB-INF/validation.xml"/>
              <set-property property="displayerConfig" value="/WEB-INF/my-displayer-config.xml"/>
      </plug-in>        
              

See sections Initialize FormView and How customize FormView DisplayerConfig for more informations. .

JSP & Taglib Formview

Create projetct.jsp :

  <%@ taglib uri="/WEB-INF/form-view.tld" prefix="formview" %>
  ....
  <formview:page>
    <html:form action="/project" >
      <html:text property="projectName" />
      <html:text property="projectDate" />
      <html:select property="managerId" >      
        <html:options collection="managerList" property="id" labelProperty="name"/>
      </html:select>
    </html:form> 
   </formview:page>     

Using

To use form with CREATE behaviour, call url with dispatch loadCreate (/project.do?dispatch=loadCreate). HTML genrated will be :

    <form action="/project.do" >
      <input type="text" name="projectName" /> * 
      <input type="text" property="projectDate" /><img src="calendar.gif" />
      <select name="managerId" >      
        <option value="1">Name 1</option>
        <option value="2" selected="selected" >Name 2</option>        
      </select>
    </form> 

To use form with READ behaviour, call url with dispatch loadRead (/project.do?dispatch=loadRead). HTML genrated will be :

    <form action="/project.do" >
      <input type="text" name="projectName" readonly="readonly" />
      <input type="text" property="projectDate" readonly="readonly" />
      <input type="hidden" name="managerId" value="2" />
      <input type="text" name="mangerIdLabel" value="Name 2" readonly="readonly" />
    </form>