Struts-Layout 0.4 introduces 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 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.
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
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.
<%@taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %> <html> <head> <layout:skin/> </head> <body> <layout:form align="center" action="/registrationExample.do" reqCode="create" styleClass="FORM" width="50%"> <layout:text key="prompt.username" property="username" styleClass="LABEL" mode="E,I,N"/> <layout:password key="prompt.password" property="password" styleClass="LABEL" mode="E,I,N"/> <layout:select key="prompt.password2" property="password2" styleClass="LABEL" mode="E,I,N"> <layout:options name="usernames"/> </layout:select> <layout:radios key="prompt.fullName" property="fullName" styleClass="LABEL" mode="E,I,N"> <layout:options name="usernames"/> </layout:radios> <layout:checkboxes key="prompt.fullName" property="array" styleClass="LABEL" mode="E,I,N"> <layout:options name="greetings"/> </layout:checkboxes> <layout:formActions> <layout:submit mode="N,D,D" reqCode="testCreate">Edit</layout:submit> <layout:submit mode="D,N,D" reqCode="testEdit">Inspect</layout:submit> <layout:submit mode="D,D,N" reqCode="testInspect">Not displayed</layout:submit> </layout:formActions> </layout:form> </body> </html>
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)