Use <html:select> tag to create HTML A element.

To specify more than one query parameters you need to create a Map, where map key corresponds to request parameter key, and map value corresponds to request parameter value.

You can specify parameter map with scriptlet like this:

{{{<%

%> <html:link page="/myAction.do" name="linkParams" scope="page" >Click Me</html:link>}}}

Instead of using Struts <html:link> tag you can try JSTL to build link's URL:

{{{<c:url value="/myAction.do" var="url">

</c:url> <a href='<c:out value="${url}"/>'>Click Me</a>}}}

Use <html:select> tag to create HTML SELECT element that can be rendered either as dropdown box or a listbox. To create a dropdown box set size less than 2 or do not specify size at all.

<html:select name="addressForm" property="currentStateCode" size="1">
 <html:optionsCollection name="stateList" value="stateCode" label="stateName"/>
</html:select>

The stateList is a list of beans with stateCode and stateName properties:

public class State {
  private String stateCode;
  public String getStateCode() {return stateCode;}

  private String stateName;
  public String getStateName() {return stateName;}

  ...
}

HTML SELECT can be rendered either as dropdown box or as a listbox. To create a listbox specify size larger than 1.

<html:select> tag specifies current value:

<html:optionsCollection> specifies the collection that contains selectable items. The above sample uses List:

When the widget is being displayed, it positions to an element that has the same value, as the property specified in <html:select> tag.

selectwidget.gif

If you want the dropdown box to contain an arbitraty expression you can define a read-only property for that (a calculated field in SQL terminology or decorator in DisplayTag parlance). For example, stateCodeAndName calculated property returns both state code and state name in one string:

public class State {
  private String stateCode;
  public String getStateCode() {return stateCode;}

  private String stateName;
  public String getStateName() {return stateName;}

  public String getStateCodeAndName() {
    return stateCode + "(" + stateName + ")";
  }

  ...
}

Then use new property as label in <html:optionsCollection> tag:

<html:select name="addressForm" property="currentStateCode" size="1">
 <html:optionsCollection name="stateList" value="stateCode" label="stateCodeAndName"/>
</html:select>

Using Map is slightly different from using List. When a Map is iterated, it exposes each entry as a Map.Entry record with attributes key and value. Selectable item is stored in value attribute of Map.Entry:

<html:select name="addressForm" property="currentStateCode" size="1">
 <html:optionsCollection name="stateMap" value="value.stateCode" label="value.stateName"/>
</html:select>

If you want a listbox to always position to a certain hardcoded value instead of value submitted by user, use value attribute to <html:select> tag. For example, this code always displays "California" despite of currently chosen value:

<html:select name="addressForm" property="currentStateCode" value="CA" size="1">
 <html:optionsCollection name="stateList" value="stateCode" label="stateName"/>
</html:select>

You still need to use property attribute to store value submitted by user.

Listbox

Use <html:select> tag to create HTML SELECT element. Specify number of rows in the lisbox using size attribute. You must set size larger than 1, otherwise SELECT element will be rendered as a dropdownbox.

<html:select name="addressForm" property="currentStateCode" size="3">
 <html:optionsCollection name="stateList" value="stateCode" label="stateName"/>
</html:select>

See previous section (dropdown box) for usage details.

Radio Button

Use <html:radio> tag to create HTML INPUT element having type "radio".

<html:radio name="addressForm" property="currentStateCode" value="CA"/> California

<html:radio> tag uses following attributes:

radiobuttonwidget.gif

Radio button is checked if actual value stored of the property equals to value attribute. In the above example, the checkbox is checked if addressForm.currentStateCode.equals("CA").

Radio button does not have a label. You need to print it yourself beside the input element, like "California" is printed above.

Radio Group

Use <logic:iterate> and <html:radio> tags to create series of HTML INPUT elements having type "radio".

<logic:iterate id="choice" name="stateList">
  <html:radio name="addressForm" property="currentStateCode" idName="choice" value="stateCode"/>
  <bean:write name="choice" property="stateName"/>
  <br>
</logic:iterate>

radiogroup.gif

<logic:iterate> tag uses following attributes:

<html:radio> tag uses following attributes:

<bean:write> tag uses following attributes:

In the above example a radio button is checked if addressForm.currentStateCode.equals(choice.stateCode).

References

Advanced Forms Handling in Struts 1.1

jGuru Q&A

StrutsWidgets (last edited 2009-09-20 23:11:47 by localhost)