You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "MoorthyHome.com" <mo...@sas.com> on 2008/02/22 20:46:19 UTC

Re: Map backed Struts Action Form and multiple select values issue

More details to my previous posting...

I am building an app with Struts 1.x, which is dynamically rendered at
runtime to create html form elements. Because of the applications dynamic
nature, I am using map backed struts action form to dynamically capture the
values in a HashMap.

Unfortunately, for multiple select box, the object value being set to the
map was always a String than a String[]. The same case applies to checkbox.

Am I using map backed struts action form properly. Is there any other
approach to build a dynamic form and actions!! Your help is much
appreciated.

I am pasting a sample test form and action form below.  

form.jsp

***********
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MyTestForm</title>
</head>
<body>
<html:form action="/myTestAction.do">
   <html:select property="value(myTestProperty)" multiple="true">
      <html:option value="option1">option1</html:option>
      <html:option value="option2">option2</html:option>
      <html:option value="option3">option3</html:option>
      <html:option value="option4">option4</html:option>
      <html:option value="option5">option5</html:option>
   </html:select>
   <p>&nbsp;</p>
   <html:submit property="submit"></html:submit>
</html:form>
</body>
</html>
************************************
formResults.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Results</title>
</head>
<body>
Selected values are<br>
<bean:write name="myTestForm" property="value(myTestProperty)" />
</body>
</html>

****************************

MyTestForm is

package test;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts.action.ActionForm;

public class MyTestForm extends ActionForm
{
   private static final long serialVersionUID = 1L;
   private static final Map values = new HashMap();
   
   public void setValue(String key, Object value)
   {
      if (value instanceof String)
      {
         System.out.println("Doesnt work!!! valueof a multiple select is a
string");
      }
      else if (value instanceof String[])
      {
         System.out.println("cool. It works!!! value is a string array");
      }
      else
      {
         System.out.println("Some other object");
      }
      
      values.put(key, value);
   }
   
   public Object getValue(String key)
   {
      return values.get(key);
   }
}

*************************
MyTestAction

package test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class MyTestAction extends Action
{
   public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
   {

      String[] myTestProperty = new String[0];
      if (request.getParameterValues("value(myTestProperty)") != null)
      {
         myTestProperty =
request.getParameterValues("value(myTestProperty)");
      }
      for (int i = 0; i < myTestProperty.length; i++)
      {
         System.out.println(myTestProperty[i]);
      }
      ActionForward forward = mapping.findForward("success");
      return forward;
   }
}
****************************

Struts-config defintion
form bean defintion
<form-bean name="myTestForm" type="test.MyTestForm"></form-bean>
action defintion
<action path="/myTestAction" name="myTestForm" type="test.MyTestAction"
scope="session" validate="false">
       <forward name="success" path="/formResults.jsp"></forward>
</action>



-- 
View this message in context: http://www.nabble.com/Map-backed-Struts-Action-Form-and-multiple-select-values-issue-tp15623494p15641300.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org