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.


Reference

http://markmail.org/message/557ico53ydq2gvsp#query:using%20a%20map%20in%20a%20dynaactionform+page:1+mid:557ico53ydq2gvsp+state:results

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
To: stru...@jakarta.apache.org
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.

Craig




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;

@SuppressWarnings("serial")
public class ActionFormRoles extends DynaActionForm {

@SuppressWarnings("unchecked")
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"/>
</form-bean>




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}">
${role.timehash[hash].value}
</c:if>
<c:if test="${column.index gt 1}">
<html:text property="roleMap(${role.id}.${headerPeriod.year}.${headerPeriod.index})" value="${role.timehash[hash].value}" size="3"/>
</c:if>

</td>
</c:forEach>




Struts Action which processes the form:

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

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




Thanks to the following website for providing a converter for my html and xml text: http://www.felgall.com/htmlt47.htm

No comments: