You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Neil Mendoza <nm...@prytaniagroup.com> on 2005/11/09 19:58:49 UTC
Map-backed ActionForms PLEASE HELP!!!!
OK, I'm climbing up the walls trying to make Struts populate a map-backed action form. This should be a simple task but I cannot get it to f*&king work and it is driving me insane... The reset method is called before the form is first displayed and the resulting jsp is populated with the value set in the reset method, i.e. it's accessing the map correctly. However, when the form is submitted the reset is called again but the form bean is not populated from the request before it gets to the action...
AAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaa....
----------------------------------------------------
Action Form class
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class StrutsTestForm extends ActionForm {
private Map things = new HashMap();
public void setThing(String key, String value) {
things.put(key, value);
}
public Object getThing(String key) {
return things.get(key);
}
public void reset(
ActionMapping arg0,
HttpServletRequest arg1) {
setThing("name", "test");
System.out.println("reset called");
}
}
------------------------------------------------------------
form jsp
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html>
<head>
<title>Employee Form</title>
</head>
<body>
<h1>Employee Form</h1>
<html:form action="sta" focus="name" method="get">
<table>
<tr>
<td >Name:</td>
<td><html:text property="thing(name)"/></td>
</tr>
<tr>
<td>Age:</td>
<td><html:text property="thing(age)"/></td>
</tr>
</table>
<html:submit>Submit</html:submit>
</html:form>
</body>
</html>
-----------------------------------------------------------
Action class
package com.prytaniagroup.www;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
//import org.apache.commons.
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class StrutsTestAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
StrutsTestForm strutsTestForm = (StrutsTestForm)form;
System.out.println((String)strutsTestForm.getThing("name"));
System.out.println((String)strutsTestForm.getThing("age"));
return (mapping.findForward("success"));
}
}
----------------------------------------------------------------------------------
extracts from strusts-config.xml
<form-beans>
<form-bean
name="StrutsTestForm"
type="com.prytaniagroup.www.StrutsTestForm"/>
</form-beans>
<action-mappings>
<action
path="/setupstrutstestform"
forward="/strutstestform.jsp"/>
<action
path="/sta"
type="com.prytaniagroup.www.StrutsTestAction"
name="StrutsTestForm"
scope="request"
validate="false">
<forward name="success" path="/confirmation.jsp"/>
</action>
</action-mappings>
---
This e-mail may contain confidential and/or privileged information. If you are not the
intended recipient (or have received this e-mail in error) please notify the sender
immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution
of the material in this e-mail is strictly forbidden. The Prytania Group has taken every
reasonable precaution to ensure that any attachment to this e-mail has been swept for
viruses. However, we cannot accept liability for any damage sustained as a result of
software viruses and would advise that you carry out your own virus checks before
opening any attachment.
Re: Map-backed ActionForms PLEASE HELP!!!!
Posted by Michael Jouravlev <jm...@gmail.com>.
>From what you posted it is not obvious that your ActionForm belongs to
com.prytaniagroup.www package. Does it?
Michael.
On 11/9/05, Neil Mendoza <nm...@prytaniagroup.com> wrote:
> OK, I'm climbing up the walls trying to make Struts populate a map-backed action form. This should be a simple task but I cannot get it to f*&king work and it is driving me insane... The reset method is called before the form is first displayed and the resulting jsp is populated with the value set in the reset method, i.e. it's accessing the map correctly. However, when the form is submitted the reset is called again but the form bean is not populated from the request before it gets to the action...
>
> AAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaa....
>
> ----------------------------------------------------
> Action Form class
>
> import java.util.HashMap;
> import java.util.Map;
>
> import javax.servlet.http.HttpServletRequest;
>
> import org.apache.struts.action.ActionForm;
> import org.apache.struts.action.ActionMapping;
>
> public class StrutsTestForm extends ActionForm {
>
> private Map things = new HashMap();
>
> public void setThing(String key, String value) {
> things.put(key, value);
> }
> public Object getThing(String key) {
> return things.get(key);
> }
>
> public void reset(
> ActionMapping arg0,
> HttpServletRequest arg1) {
> setThing("name", "test");
> System.out.println("reset called");
>
> }
> }
> ------------------------------------------------------------
> form jsp
>
> <%@ taglib uri="/tags/struts-bean" prefix="bean" %>
> <%@ taglib uri="/tags/struts-html" prefix="html" %>
> <html>
> <head>
> <title>Employee Form</title>
> </head>
> <body>
> <h1>Employee Form</h1>
> <html:form action="sta" focus="name" method="get">
> <table>
> <tr>
> <td >Name:</td>
> <td><html:text property="thing(name)"/></td>
> </tr>
> <tr>
> <td>Age:</td>
> <td><html:text property="thing(age)"/></td>
> </tr>
> </table>
> <html:submit>Submit</html:submit>
> </html:form>
> </body>
> </html>
>
> -----------------------------------------------------------
> Action class
>
>
> package com.prytaniagroup.www;
>
> import org.apache.struts.action.Action;
> import org.apache.struts.action.ActionForward;
> import org.apache.struts.action.ActionMapping;
> import org.apache.struts.action.ActionForm;
> //import org.apache.commons.
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
>
> public final class StrutsTestAction extends Action {
>
> public ActionForward execute(ActionMapping mapping,
> ActionForm form,
> HttpServletRequest request,
> HttpServletResponse response)
> throws Exception {
> StrutsTestForm strutsTestForm = (StrutsTestForm)form;
> System.out.println((String)strutsTestForm.getThing("name"));
> System.out.println((String)strutsTestForm.getThing("age"));
> return (mapping.findForward("success"));
> }
> }
>
> ----------------------------------------------------------------------------------
> extracts from strusts-config.xml
>
> <form-beans>
>
> <form-bean
> name="StrutsTestForm"
> type="com.prytaniagroup.www.StrutsTestForm"/>
> </form-beans>
>
> <action-mappings>
>
> <action
> path="/setupstrutstestform"
> forward="/strutstestform.jsp"/>
>
> <action
> path="/sta"
> type="com.prytaniagroup.www.StrutsTestAction"
> name="StrutsTestForm"
> scope="request"
> validate="false">
>
> <forward name="success" path="/confirmation.jsp"/>
> </action>
>
> </action-mappings>
>
> ---
> This e-mail may contain confidential and/or privileged information. If you are not the
> intended recipient (or have received this e-mail in error) please notify the sender
> immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution
> of the material in this e-mail is strictly forbidden. The Prytania Group has taken every
> reasonable precaution to ensure that any attachment to this e-mail has been swept for
> viruses. However, we cannot accept liability for any damage sustained as a result of
> software viruses and would advise that you carry out your own virus checks before
> opening any attachment.
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Map-backed ActionForms PLEASE HELP!!!!
Posted by Laurie Harper <la...@holoweb.net>.
Your 'setThing' and 'getThing' methods have inconsistent signatures.
They need to comply with the JavaBeans specification. In particular,
they both need to deal in the same value types -- so either change the
type of the 'value' parameter to Object in setThing, or change the
return type of getThing to String. I think that's your problem.
L.
Neil Mendoza wrote:
> OK, I'm climbing up the walls trying to make Struts populate a map-backed action form. This should be a simple task but I cannot get it to f*&king work and it is driving me insane... The reset method is called before the form is first displayed and the resulting jsp is populated with the value set in the reset method, i.e. it's accessing the map correctly. However, when the form is submitted the reset is called again but the form bean is not populated from the request before it gets to the action...
>
> AAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaa....
>
> ----------------------------------------------------
> Action Form class
>
> import java.util.HashMap;
> import java.util.Map;
>
> import javax.servlet.http.HttpServletRequest;
>
> import org.apache.struts.action.ActionForm;
> import org.apache.struts.action.ActionMapping;
>
> public class StrutsTestForm extends ActionForm {
>
> private Map things = new HashMap();
>
> public void setThing(String key, String value) {
> things.put(key, value);
> }
> public Object getThing(String key) {
> return things.get(key);
> }
>
> public void reset(
> ActionMapping arg0,
> HttpServletRequest arg1) {
> setThing("name", "test");
> System.out.println("reset called");
>
> }
> }
> ------------------------------------------------------------
> form jsp
>
> <%@ taglib uri="/tags/struts-bean" prefix="bean" %>
> <%@ taglib uri="/tags/struts-html" prefix="html" %>
> <html>
> <head>
> <title>Employee Form</title>
> </head>
> <body>
> <h1>Employee Form</h1>
> <html:form action="sta" focus="name" method="get">
> <table>
> <tr>
> <td >Name:</td>
> <td><html:text property="thing(name)"/></td>
> </tr>
> <tr>
> <td>Age:</td>
> <td><html:text property="thing(age)"/></td>
> </tr>
> </table>
> <html:submit>Submit</html:submit>
> </html:form>
> </body>
> </html>
>
> -----------------------------------------------------------
> Action class
>
>
> package com.prytaniagroup.www;
>
> import org.apache.struts.action.Action;
> import org.apache.struts.action.ActionForward;
> import org.apache.struts.action.ActionMapping;
> import org.apache.struts.action.ActionForm;
> //import org.apache.commons.
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
>
> public final class StrutsTestAction extends Action {
>
> public ActionForward execute(ActionMapping mapping,
> ActionForm form,
> HttpServletRequest request,
> HttpServletResponse response)
> throws Exception {
> StrutsTestForm strutsTestForm = (StrutsTestForm)form;
> System.out.println((String)strutsTestForm.getThing("name"));
> System.out.println((String)strutsTestForm.getThing("age"));
> return (mapping.findForward("success"));
> }
> }
>
> ----------------------------------------------------------------------------------
> extracts from strusts-config.xml
>
> <form-beans>
>
> <form-bean
> name="StrutsTestForm"
> type="com.prytaniagroup.www.StrutsTestForm"/>
> </form-beans>
>
> <action-mappings>
>
> <action
> path="/setupstrutstestform"
> forward="/strutstestform.jsp"/>
>
> <action
> path="/sta"
> type="com.prytaniagroup.www.StrutsTestAction"
> name="StrutsTestForm"
> scope="request"
> validate="false">
>
> <forward name="success" path="/confirmation.jsp"/>
> </action>
>
> </action-mappings>
>
> ---
> This e-mail may contain confidential and/or privileged information. If you are not the
> intended recipient (or have received this e-mail in error) please notify the sender
> immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution
> of the material in this e-mail is strictly forbidden. The Prytania Group has taken every
> reasonable precaution to ensure that any attachment to this e-mail has been swept for
> viruses. However, we cannot accept liability for any damage sustained as a result of
> software viruses and would advise that you carry out your own virus checks before
> opening any attachment.
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org