SAStruts Form

SAStruts use Form to receive parameters and mapping to properties from request

  1. SAStruts Form used for some purpose

    • Receive parameters and Mapping to properties from request
    • Convert type for Action-methods
    • Pass values from Action-methods to JSP
    • Check parameters validation
    • View Helper

    You must create a Form-class for each Action-class

  2. Properties

    A browser send request input value with key. The key is name attribute of input-tag in HTML. SAStruts receive parameter value with key. The value is copied to match the key same property of Form-object.

    At HTML

    <input name="age" value="25">
    

    At Form class

    public class Programmer {
      public String age;
    }
    

    A property type is String-type (recommended). Because input parameters could anything.

  3. Setter

    You create setter method in Form-class. The method can be used as one way property for input parameters.

    At HTML

    <input name="age" value="25">
    

    At Form class

    public class Programmer {
      public void setAge;
    }
    

    If you want to create setter method for Action-method, You should name like "fillAge". Use word "fill" instead "set".

  4. Simple input validation check

    For simple input validation check, you can use input validation annotations to properties. http://sastruts.seasar.org/annotationReference.html#Validation

  5. Complex input validation check

    Implement of Complex validation is in Action-classes.

  6. Parameters getter for Action-methods

    All properties are String-type. An Action-method need other type parameters. For example Long, Date, Integer... So, you should implement converter for action-method.

    The converter method naming is "property" + "As" + "needType".

    public long idAsLong() {
      return Long.valueOf(this.id);
    }
    
  7. View Helper role

    You create getter method in Form-class. The method can be used as one way property for JSP.

    Getter in Java

    public String getParamsForUrl(){
      return "id=" + this.id + "&" + "name=" + this.name;
    }
    

    EL syntax in JSP

    ${demoForm.paramsForUrl}
    

    The name is without "get".

  8. Control Form object in Session scope

    Operation flow In this case ... a user open the Edit page. the user input values. the user send parameters to server. the server do save proc.(error) returned Edit page with redirect ... This case can't replay user input value. The Form object has gone, because Form object is managed request scope. If you need that replay user input values, switch from managed request scope to session scope.

    Put Component-annotation to a Form-class

    @Component(instance = InstanceType.SESSION)
    public class DemoForm implements Serializable {
      private static final long serialVersionUID = 1L;
    }
    
  9. How to receive variable complex structured parameters

    For example, you edit structured data like this

    name age memo
    a 20 programmer
    b 22 manager
    c 29 designer
    ... ... ...

    At JSP

    <table>
      <tr>
        <th>name</th>
        <th>age</th>
        <th>memo</th>
      </tr>
      <tr>
        <td><input name="employeeList[0].name" type="text" value="${employeeEditForm.employeeList[0].name}" /></td>
        <td><input name="employeeList[0].age" type="text" value="${employeeEditForm.employeeList[0].age}" /></td>
        <td><input name="employeeList[0].memo" type="text" value="${employeeEditForm.employeeList[0].memo}" /></td>
      </tr>
      <tr>
        <td><input name="employeeList[1].name" type="text" value="${employeeEditForm.employeeList[1].name}" /></td>
        <td><input name="employeeList[1].age" type="text" value="${employeeEditForm.employeeList[1].age}" /></td>
        <td><input name="employeeList[1].memo" type="text" value="${employeeEditForm.employeeList[1].memo}" /></td>
      </tr>
      <tr>
        <td><input name="employeeList[2].name" type="text" value="${employeeEditForm.employeeList[2].name}" /></td>
        <td><input name="employeeList[2].age" type="text" value="${employeeEditForm.employeeList[2].age}" /></td>
        <td><input name="employeeList[2].memo" type="text" value="${employeeEditForm.employeeList[2].memo}" /></td>
      </tr>
    </table>
    

    At Form

    public class EmployeeEditForm {
      public List employeeList;
      public static class Employee {
        public String name;
        public String age;
        public String memo;
      }
    }
    

    You want to control a session scope. Attach Serializable interface the Form class and static inner class.

    public class EmployeeEditForm implements Serializable {
      private static final long serialVersionUID = 1L;
      public List employeeList;
      public static class Employee implements Serializable {
        private static final long serialVersionUID = 1L;
        public String name;
        public String age;
        public String memo;
      }
    }
    

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections