Display modes

Struts-Layout 0.4 introduced the notion of form and field display modes.

To explain their use, let's imagine you are running an online shop selling books. There is one jsp to display information about the book (title, author, size, price etc.). The customers can only view the information, but the site administrator can view and modify the information. Normally, you would have to make two jsps to achieve this: one to display the information, and one to modify it (you can also make one jsp with the complicated logic stuff).

With Struts-Layout, it is possible to make only one jsp, without logic in it.

The goal is : One user object to create / edit / view, one DistpachAction class, one Form class, and one jsp.

  • The form display mode is used to specify if a form in a jsp is displayed to view information (inspection mode) or to modify it (edit mode).

The form display mode is set in the Struts action by calling the FormUtils.setFormDisplayMode method. For example, FormUtils.setFormDisplayMode(request, form, FormUtils.INSPECT_MODE) will set the form mode to EDIT.

Struts-Layout also support a creation display mode, and will support user defined display mode.

  • The field display mode is used to specify if an input field should be writeable, readonly, hidden, or totally skipped in a given form display mode.

This allows a field whose value is a primary key to be editable only during the creation, or fields whose values are computed automatically to be displayed only when viewing the information, and not when modifying it.

The default behavior for an input field is to display itself as writeable (typical form field) if the form display mode is CREATE or EDIT, and to display itself readonly (the field value in plain text, and in an hidden input field) if the form display mode is INSPECT.

This can be altered by using the attribute "mode" of the different Struts-Layout input field tags. The value of this attribute must respect the following simple pattern: X,Y,Z where X is the behaviour in creation mode, Y in edit mode, and Z in inspection mode. Valid behaviours are

  • E: editable (read-write)
  • I: inspect, with an hidden input field (read-only)
  • S: show, as I but without hidden field
  • N: not displayed at all
  • P: present (as S if the field value is not null, as N if the field value is null)
  • H: hidden (not displayed, but a hidden input field is generated)
  • R : readonly (an input field is generated with the readonly attribute set)
  • D : disabled (an input field is generated with the disabled attribute set)

Like a form, the field display mode may be set in the Struts action by calling the FormUtils.setFieldDisplayMode method. For example, FormUtils.setFieldDisplayMode(request, form, fieldName, AbstractModeFieldTag.EDIT_INSPECT) will set the field mode to EDIT.

Important note: The <layout:field> tag can not be used with form and field display modes. The <layout:text>, <layout:password>, <layout:textarea>, <layout:file> tags must be use instead.

Here is the code of a page using those attributes. There are no logic tags in it. A different display mode is set in the Struts action when the form is submited.

 1<%@taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
10<layout:form align="center" action="/registrationExample.do" reqCode="create" styleClass="FORM" width="50%">
11    <layout:text key="prompt.username" property="username" styleClass="LABEL" mode="E,I,N"/>
12    <layout:password key="prompt.password" property="password" styleClass="LABEL" mode="E,I,N"/>
13    <layout:select key="prompt.password2" property="password2" styleClass="LABEL" mode="E,I,N">
14        <layout:options name="usernames"/>
15    </layout:select>
16    <layout:radios key="prompt.fullName" property="fullName" styleClass="LABEL" mode="E,I,N">
17        <layout:options name="usernames"/>
18    </layout:radios>
19    <layout:checkboxes key="prompt.fullName" property="array" styleClass="LABEL" mode="E,I,N">
20        <layout:options name="greetings"/>
21    </layout:checkboxes>
23    <layout:formActions>
24        <layout:submit mode="N,D,D" reqCode="testCreate">Edit</layout:submit>
25        <layout:submit mode="D,N,D" reqCode="testEdit">Inspect</layout:submit>
26        <layout:submit mode="D,D,N" reqCode="testInspect">Not displayed</layout:submit>
27    </layout:formActions>

Result with the fields in edit mode
Result with the fields in inspect mode
Result with the fields in not displayed mode

Now here is a very simple application, which allows the user to list, modifiy and create user registrations. There is only one jsp, and one dispatch action. The dispatch action has 3 display methods; inspect (choose and display registrations), edit (modify a registration) and create (create a registration) and 2 save method (save and saveNew)

The very simple application

next: how to dynamically change the field display mode in function of the user role