Using an integer range field validator annotation (Struts2)

XWork provides several mechanisms for validating form fields as part of its validation framework. One of these is through the use of annotations, and provides internationalization lookup, minimums and maximums, and automatic display of messages when validation fails.

This example uses the setting of the port for the SLAMD client listener - using an individual field validation annotation. The port is a TCP port and should conform to TCP restrictions. The basic code is:



  /\*\*
   \* Sets the port used by the SLAMD client listener. The client
   \* listener acts as listener on the specified port. This field is
   \* required and is keyed to the string "client_listen_port" in
   \* slamd.conf and in the {@link Configuration} object map.
   \*
   \*
   \* @param clientListenPort the port upon which the client listener
   \*                         communicates with SLAMD clients.
   \*/
  @IntRangeFieldValidator (
    min="0",max="65535",key="error.range.client_listen_port",
    message="value out of range")
  public void setClientListenPort(final Integer clientListenPort)
  {
    Config.getInstance().setClientListenPort(clientListenPort);
  }
The JSP has:

        <%-- client listener port --%>
        <div class="client_listen_port_entry textfield">
          <h2><s:text name="client_listener.client_listen_port.label"/></h2>
          
          <span class="description">
            <s:text name="client_listener.client_listen_port.description"/>
          </span>
          
          <s:textfield
             name="clientListenPort"
             value="%{clientListenPort}"
             tabindex="0"
             required="true"
             size="60"
             labelposition="left"
             label="%{getText('client_listener.client_listen_port.label')}"/>
        </div>
The IntRangeFieldValidator offers a minimum check, a maximum check, and a message to display in the event that the integer entered on the form falls out of the range. Xwork matches the field name, in this case 'clientListenPort', with the field name on the form and displays the message above the field.
The "key" parameter is used to look up a value in a resources bundle. The message parameter is used in the event that the key is not found in a resources bundle.
The following screenshot shows the result of attempting to save an invalid value:

validators result screenshot
Comments:

Post a Comment:
Comments are closed for this entry.
About

Sun, LDAP, SLAMD, DSLA, java, Struts, networking, chess, books, cooking, wine, and many other things.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today