struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Li Ying <liying.cn.2...@gmail.com>
Subject Re: javax.servlet.ServletException: BeanUtils.populate
Date Sat, 27 Nov 2010 08:27:40 GMT
I think the reason of the Exception maybe:

(1)the data you post is named like "liste[0].nom"

(2)Struts1 accept this http param, and try to translate it to a form
property, by using a lib named Apache Commons BeanUtils
See: http://commons.apache.org/beanutils/

(3)BeanUtils treats nested property access likes
"liste[0].nom"
as some chained java method invokes, like:
targetBean.getListe[0].setNome(someValue)

(4-1)If you did not initialize the array liste,
it will be a null, so of cause "getListe" will get a null,
and then, "getListe[0]" should throw a NullPointException

(4-2)If you initialized the array, but there is no item in it,
then "getListe[0]" should throw an IndexOutOfBoundsException

(4-3)If you initialized the array, but the items in it is null,
then "getListe[0]" will get a null,
and then "setNome(someValue)" should  throw a NullPointException

So, you should initialize the array, and items in it, before you can
capture data posted from client side.

Years ago, I meet the same problem. But I used List instead of array.
So I created a new class named "AutoList". When you call the
"get(index)" method on it, it will create a new item instance if the
item dose not exist yet.
By using this AutoList, when BeanUtils try to invoke method likes
"getListe(0).setNome(someValue)", the item instance will be created
automatically if need.

In your case, my suggest is:
(1)use AutoList, instead of Array

Or

(2)Since you have done the array/item initialization in the the method
"setNom(int index , Personne nom)", so you can use this method instead
of "getListe[0].setNome(someValue)".
Which mean, in html side, the data name should be
"nom[0]" instead of "liste[0].nom"



How ever, I recommend AutoList solution strongly, because I believe
this is a very common problem in Struts1.
You may meet it somewhere else, so AutoList class will be very convenient.
And also, if you use AutoList, you don't have to change the data name
on the html side, which means, you can use struts tag lib as normal.

If you need this AutoList class, I can send it to you.

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


Mime
View raw message