Thursday, January 22, 2009

"More" Dynamic Forms with Struts

The goal behind this pot-pourri of code snippets is that I had a
spreadsheet-like form whose purpose was to collect information that was
relative to the time/date when the form was being viewed (i.e. "dynamic").
Hopefully, someday I will get the time to cohesively explain the relationship
between the code snippets but they should prove of some value in the meantime.


On Sun, 1 Dec 2002, Martin Cooper wrote:

Date: Sun, 1 Dec 2002 22:18:05 -0800 (PST)
From: Martin Cooper Reply-To: Struts Developers List
Subject: Using a property of type Map in a DynaActionForm

As far as I can determine, it is not possible to create a DynaActionForm which
has a property of type Map. It is also not possible to extend it to be able
to accept a property of type Map. This seems like an unfortunate omission,
so I'd like to fix it.

First, though, I'd like to get some feedback on whether there is a reason
it's not currently supported, and on the best way of resolving it.

It's *supposed* to work if you declare the property type to be java.util.Map
instead of a concrete implementation of Map. You'll still need to create
your own Map instance at the moment -- I do mine in a custom reset() method,
but we probably also need a better solution based on an initial expression.


Custom Struts Action Form

package mypackage;

import java.util.HashMap;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

public class ActionFormRoles extends DynaActionForm {

public void reset(ActionMapping mapping, HttpServletRequest request) {
this.getMap().put("roleMap", new HashMap());
this.getMap().put("assignMap", new HashMap());

super.reset(mapping, request);

public void reset(ActionMapping mapping, ServletRequest request) {
super.reset(mapping, request);

Use of custom action form in struts-config.xml:

<form-bean name="roleForm" type="com.kpe.resourcePlanning.webapp.forms.ActionFormRoles">
<form-property type="java.util.Map" name="roleMap"/>
<form-property type="java.util.Map" name="assignMap"/>

JSP snippet showing use of map:
    <c:forEach var="headerPeriod" items="${rHeaderPeriods}" varStatus="column">
<td style="text-align: center;">
<c:set var="hash" value="${headerPeriod.year}.${headerPeriod.index}"/>

<c:if test="${column.index lt 2}">
<c:if test="${column.index gt 1}">
<html:text property="roleMap(${}.${headerPeriod.year}.${headerPeriod.index})" value="${role.timehash[hash].value}" size="3"/>


Struts Action which processes the form:

public ActionForward saveManageRoles(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
final ApplicationContext ac = this.getAppContext(request);
final ActionFormRoles f = (ActionFormRoles)form;

this.getApplicationController().saveRolesData(ac, (Map)f.get("roleMap"), (Map)f.get("assignMap"));
return mapping.findForward("manage");

Thanks to the following website for providing a converter for my html and xml text:

No comments: