Recipe5.4.Using a Map-Backed Form Property


Recipe 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 Skill:          <html:select property="skill(java)">             <html:options property="skillLevels"/>         </html:select><br />         JSP Skill:          <html:select property="skill(jsp)">             <html:options property="skillLevels"/>         </html:select><br />         Struts Skill:          <html:select property="skill(struts)">             <html:options property="skillLevels"/>         </html:select><br />         <html:submit/>     </html:form>     <hr />     <c:if test="${not empty MapForm.skills}">         Java Skill: <c:out value="${MapForm.skills.java}"/><br />         JSP Skill: <c:out value="${MapForm.skills.jsp}"/><br />         Struts Skill: <c:out value="${MapForm.skills.struts}"/><br />     </c:if> </body> </html>

Discussion

The value of a Map-backed form property is accessed using its key. The key and value are used to add or replace a value in the map. Map-backed form properties provide a good way of creating a property that can hold an indeterminate set of values.

In the Solution above, the MapForm action form defines the property skill. The getters and setters use the following pattern:

public Value getValue(String key); public void setValue(String key, Object value);

Using this pattern allows the value to be accessed on a JSP page from most Struts tags using the property attribute syntax of value(key). value corresponds to the property name, and key corresponds to the actual key into the map. For Map-backed properties, the keys should always be Strings. A Map-backed property can be accessed in this way by any Struts tag where the property attribute is used to access JavaBean properties:

<html:text property="skill(java)"/> <bean:write name="MapForm" property="skill(java)"/>

JSTL doesn't support accessing properties that use the getValue(String key) pattern. Map-backed properties can, however, be accessed using a JSTL expression provided the actual Map is exposed via a public getter method. The Solution shows how the JSTL c:out tag is used to render the data from the map:

<c:out value="${MapForm.skills.java}"/>

JSTL accesses the Map directly and extracts values by the specified key. If you don't need to use JSTL to access Map-backed properties, then exposing the Map in a public method is not required.

See Also

Recipe 5.3 shows how to use lists to back a form property.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net