Differences between revisions 16 and 17
Revision 16 as of 2006-07-06 21:10:19
Size: 7042
Comment: Added <a href ...> to JSTL sample
Revision 17 as of 2009-09-20 23:11:47
Size: 7070
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 63: Line 63:
inline:selectwidget.gif {{attachment:selectwidget.gif}}
Line 135: Line 135:
inline:radiobuttonwidget.gif {{attachment:radiobuttonwidget.gif}}
Line 153: Line 153:
inline:radiogroup.gif {{attachment:radiogroup.gif}}
Line 173: Line 173:
[http://javaboutique.internet.com/tutorials/strutsform/index.html Advanced Forms Handling in Struts 1.1] [[http://javaboutique.internet.com/tutorials/strutsform/index.html|Advanced Forms Handling in Struts 1.1]]
Line 175: Line 175:
[http://www.jguru.com/faq/view.jsp?EID=877089 jGuru Q&A] [[http://www.jguru.com/faq/view.jsp?EID=877089|jGuru Q&A]]

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>}}}

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.

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:

  • 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

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:

  • 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).

References

Advanced Forms Handling in Struts 1.1

jGuru Q&A

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