This is what my action form looks like. You can ignore the LinkedHashMap and
use a regular HashMap. The only difference really is that I use the 'this.'
notation to access the local Map.

Basically, I have a single Map that has accessor methods to get/set
individual properties and I have accessor methods to get/set the whole Map.


ValidatorActionForm
...

protected LinkedHashMap fields = new LinkedHashMap();

...

        /**
         * Returns the field.
         * @return Object
         */
        public Object getField(String key) {
                return fields.get(key);
        }

        /**
         * Method setField.
         * @param key
         * @param value
         */
        public void setField(String key, Object value) {
                this.fields.put(key,value);
        }


        /**
         * Method getFields.
         * @return Map
         */
        public LinkedHashMap getFields() {
                return fields;
        }

        /**
         * Sets the fields.
         * @param fields The fields to set
         */
        public void setFields(LinkedHashMap fields) {
                this.fields = fields;
        }

...



My jsp tag property looks like the following:

<html:text property="fields(phoneAreaCode)" size="24"/>



My validation.xml looks like the following:

<form-validation>
<formset>
<form name="/contact/send">
...
<field property="fields(phoneAreaCode)"
    depends="integer,minlength">
    <arg0 key="${var:field}"
        resource="false"/>
    <arg1 name="minlength"
        key="${var:minlength}"
        resource="false"/>
    <var>
        <var-name>field</var-name>
        <var-value>Area Code</var-value>
    </var>
    <var>
        <var-name>minlength</var-name>
        <var-value>3</var-value>
    </var>
</field>
...
</form>
</formset>
</form-validation>

It works like a champ.


Brandon Goodin
Phase Web and Multimedia
PO Box 85
Whitefish MT 59937
P (406) 862-2245
F (406) 862-0354
[EMAIL PROTECTED]
http://www.phase.ws


-----Original Message-----
From: Ken Boss [mailto:[EMAIL PROTECTED]
Sent: Saturday, February 22, 2003 5:39 PM
To: [EMAIL PROTECTED]
Subject: Using Map in ActionForm (ValidatorForm)


The book forum for "Struts in Action" appears to be down, so thought I'd
try here.  Apologies if you don't have the book.

I am attempting to use a map in my form bean to pass data back and forth
from my business tier, as described on pp. 172-173 in "Struts in
Action".  The code within the form bean is exactly as shown on p. 172,
excepting that I added a couple of lines to the setValue method so that
it reads:

        public void setValue(String key, Object value) {
                if (key != null && value != null) {
                        getMap().put(key,value);
                }
        }


When retrieving data from the database, the map comes in from the action
and populates the form widgets on my jsp page without difficulty.
However, when I try to submit the page to send the data back, I get a
NullPointerException from the setValue method:

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at
org.apache.commons.beanutils.PropertyUtils.setMappedProperty(PropertyUtils.j
ava:1583)

... >< snip >< ...

Caused by: java.lang.NullPointerException
        at
us.mn.state.dnr.fire.FireReportForm.setValue(FireReportForm.java:270)
        ... 44 more

The line referred to (FireReportForm.java:270) is the
"getMap().put(key,value)" statement.

I've tried another reader's suggestion of changing

        private Map map = null;

to

        private Map map = new HashMap();

which gets past the NullPointerException, but then triggers a validator
error for a simple "required" field, which actually shows data in the
form.

Can anyone clue me in as to what I might be doing wrong?

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to