February 2005
Intermediate to advanced
528 pages
12h 53m
English
You have a form with a
property
defined as a Map, and you want to access a value
in that Map using a key.
Define getter and setter methods that use the following pattern:
public Object getFoo(String key) {...}
public void setFoo(String key, Object value) {...}In Example 5-6, the skill
property is a Map-backed property.
Example 5-6. Map-backed form
package com.oreilly.strutsckbk.ch05;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts.action.ActionForm;
public class MapForm extends ActionForm {
private static String[] skillLevels =
new String[] {"Beginner","Intermediate","Advanced"};
private Map skills = new HashMap( );
public Object getSkill(String key) {
return skills.get(key);
}
public void setSkill(String key, Object value) {
skills.put(key, value);
}
public Map getSkills( ) {
return skills;
}
public String[] getSkillLevels( ) {
return skillLevels;
}
}The Map-backed property value is accessed from the
Struts tags on a JSP page
(map_form_test.jsp) using the
property="value(key)" syntax as shown Example 5-7.
Example 5-7. Accessing Map-backed form properties
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix= "html" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head> <title>Struts Cookbook - Chapter 5 : Map-backed Form</title> </head> <body> <h2>Map Form Test</h2> <html:form action="/ProcessMapForm"> Java ...