Fields FAQ

How can I display a date pick up calendar ?

The <layout:date> tag displays a popup calendar. Example:

 1<%@taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
 2<html>
 3
 4<head>
 5<layout:skin/>
 6<script src="/struts-layout/config/javascript.js"></script>
 7</head>
 8
 9<body>
10
11<layout:form align="center" action="/registrationExample.do" reqCode="create" styleClass="FORM" width="50%">
12    <layout:date key="Date" property="username" styleClass="LABEL" mode="E,E,E"/>
13</layout:form>
14</body>
15</html>

Result

How can I display a popup window to help the user to fill in a field ?

The following javascript functions are available to display a popup window that can filled in an input field when closed.

  • openpopup(form, popup, width, height, e)
  • closepopup(form, openerField, popupField)
    The openpopup function opens a popup window, and submits the form in this new window. This allows to generate a response specific to the form content. The closepopup funtion sets the specified field in the main window with the value of the selected field of the popup window and closes the popup window.

This example allows to pick up a car model when a brand is selected.

How can I set the default value of a DispatchAction parameter ?

The struts-layout form tag adds html formatting to the struts-html form tag but also allows the author to specify the name of the method to invoke if the form is posted to a DispathAction. This can be done by using the reqCode attribute.

If there are several submit buttons, it is possible to invoke different methods by using the reqCode attribute of the struts-layout submit button:

 1<%@taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
 2<html>
 3<header>
 4<layout:skin>
 5<header>
 6<body>
 7
 8<layout:form align="center" action="registration.do" reqCode="performOne" styleClass="FORM">
 9    <layout:field key="prompt.username" property="username"/>
10    <layout:field key="prompt.fullName" property="fullName"/>
11    <layout:field key="prompt.fromAddress" property="fromAddress"/>
12    <layout:formActions>
13        <layout:submit>Post to default method</layout:submit>
14        <layout:submit reqCode="performTwo">Post to method2</layout:submit>
15    </layout:formActions>
16</layout:form>
17
18</body>
19</html>

Result

How can I create a list of radio buttons or a drop down list ?

It is possible to create radio buttons very easily with the <layout:radios> tag. Here is how is works:

 1<@page import="java.util.Vector"%>
 2<%@taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
 3<html>
 4<head>
 5<layout:skin/>
 6<% Vector v = new Vector(); v.add("Edit"); v.add("Create"); v.add("Delete");
 7   pageContext.setAttribute("v", v); %>
 8</head>
 9<body>
10
11<layout:form align="center" action="registration.do" styleClass="FORM">
12    <layout:radios key="action" property="action">
13        <layout:options name="v"/>
14        <layout:option value="Do something else"/>
15    </layout:radios>
16    <layout:select key="action" property="action">
17        <layout:options name="v"/>
18        <layout:option value="Do something else"/>
19    </layout:select>
20</layout:form>
21
22</body>
23</html>

The form associated with the registration action, RegistrationForm has its properties action initially set to Create so Create will be selected

Select boxes are created the same way than radio buttons.

As with the <html:options> tag, it is possible to specify different values for the value to display and the value to send, using the labelName and labelProperty attributes.

Result

How can I put more than one field on a line ?

The <layout:line> tag allows to put several input fields or buttons on the same line. See also the layout FAQs.

 1<%@taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
 2<html>
 3<head>
 4<layout:skin/>
 5</head>
 6<body>
 7
 8<layout:form align="center" action="registration.do" reqCode="performOne" styleClass="FORM" focus="username">
 9    <layout:line>
10        <layout:text key="prompt.username" property="username"/>
11        <layout:text key="prompt.fullName" property="fullName"/>
12    </layout:line>
13    <layout:text key="prompt.fromAddress" property="fromAddress"/>
14    <layout:submit>Action 3</layout:submit>
15    <layout:line>
16        <layout:submit>Action 1</layout:submit>
17        <layout:submit>Action 2</layout:submit>
18    </layout:line>
19</layout:form>
20
21</body>
22</html>

Result

How can I set the styleClass of a field at runtime ?

In some case it will be needed to set a field styleClass at run time (for exemple in function of its value). This can be achived by using the FormUtils.setFieldStyleClass method in the execute method of an Action class. For example FormUtils.setFieldStyleClass(request, "login", "RED"); will set the field whose property is "login" to "RED"

Please note that this can't be used with the deprecated <layout:field> tag.

How can I warn the user he is going to lost data if he clicks on a link after typing data in a form ?

If a user changes form values and then chooses to navigate in the application by clicking on an hyperlink, its changes will be lost. Struts-layout allows to warn the user when he is about to loose its changes. This is done by setting the follow.change property in the Layout properties file to false. The required javascript code is included in the layout tags. To use this feature with usual HTML or struts tags, the href attribute should be modified from url to javascript:checkFormChange('url', 'warning message')

The treeview example uses a skin configured with this feature.

How can I create two interdependent combo box ?

Interdependent combo box can be created with the <layout:optionsDependent> tag. The following example allows to pick up a city after picking up a country:

1<layout:select key="Country" property="country" styleClass="FIELD">                
2    <layout:option value=""/>
3    <layout:options collection="countries" property="name" sourceOf="city"/>
4</layout:select>                
5
6<layout:select key="City" property="city" styleClass="FIELD">
7    <layout:optionsDependent collection="cities" dependsFrom="country"/>
8</layout:select>

The model object is as follow:

 1public class Country {
 2    private String name;
 3    private List cities = new ArrayList();
 4
 5    public Country(String in_name) {
 6        name = in_name;
 7    }
 8
 9    public String getName() {
10        return name;
11    }
12
13    public List getCities() {
14        return cities;
15    }
16
17    public void addCity(String in_city) {
18        cities.add(in_city);
19    }
20}        

A list of Country object is put directly in the request scope under the key "countries". The Struts form has 2 properties country and city.