Skip to Main Content
Jakarta Struts Cookbook
book

Jakarta Struts Cookbook

by Bill Siggelkow
February 2005
Intermediate to advanced content levelIntermediate to advanced
528 pages
12h 53m
English
O'Reilly Media, Inc.
Content preview from Jakarta Struts Cookbook

5.4. Using a Map-Backed Form Property

Problem

You have a form with a property defined as a Map, and you want to access a value in that Map using a key.

Solution

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Jakarta Struts

Programming Jakarta Struts

Chuck Cavaness
Beginning Spring Framework 2

Beginning Spring Framework 2

Bruce Snyder, Sing Li, Anne Horton, Thomas Van de Velde, Naveen Balani, Christian Dupuis
Java Cookbook

Java Cookbook

Ian F. Darwin
Struts 2 in Action

Struts 2 in Action

J. Scott Stanlick, Chad Michael Davis, Donald J. Brown

Publisher Resources

ISBN: 059600771XSupplemental ContentErrata Page