HTML Link
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:
{{{<%
HashMap params = new HashMap(); params.put("param1", param1Value); params.put("param2","param2Value"); pageContext.setAttribute("linkParams", params);
%> <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:param name="param1" value="${user.fullName}"/> <c:param name="param2" value="${'parameter 2'}"/>
</c:url> <a href='<c:out value="${url}"/>'>Click Me</a>}}}
Dropdown box
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:
name is the name of plain java bean or an action form; optional
property is the property in the java bean that holds current value
<html:optionsCollection> specifies the collection that contains selectable items. The above sample uses List:
name is the collection name, can be defined in any scope;
value specifies the property containing item value;
label specifies the property containing item label; this is what is shown to a user.
When the widget is being displayed, it positions to an element that has the same value, as the property specified in <html:select> tag.
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:
name is the name of plain java bean or an action form; optional
property is the property in the java bean that holds current value
value is the static value assigned to a particular radio button
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>
<logic:iterate> tag uses following attributes:
name is the name of a collection that holds possible values and labels of radio buttons
id is the name to use for current collection element while iterating over the collection
<html:radio> tag uses following attributes:
name is the name of plain java bean or an action form; optional
property is the property in the java bean that holds current value
idName corresponds to id attribute from <logic:iterate> tag; id identifies a collection element that is being processed
value is the name of collection property that contains radio button value
<bean:write> tag uses following attributes:
name identifies a collection element that is being processed
property is the name of collection property that contains radio button label
In the above example a radio button is checked if addressForm.currentStateCode.equals(choice.stateCode).