• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 2.1.8 form validation and setter type mismatch

 
Patrick Kok
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I am new in struts2, but I do coding for many years.
I have an action form with model driven named item.id, item.name etc. in struts2 OGNL expression.
The setter of setId(Integer, id) working good with its associated validation xml, but I have a problem when I submit the value with string.

- Error setting expression 'item.id' with value '[Ljava.lang.String;@2b2cf8'
ognl.MethodFailedException: Method "setId" failed for object com.xxx.xxxxx.xxxxx.Item@11a6631 [java.lang.NoSuchMethodException: com.xxx.xxxxx.xxxxx.Item.setId([Ljava.lang.String;)]
...
...


It seems struts2 looks for an overload method of setId(String id) instead.
As I found the defaultStack of Interceptor, the params and conversionError interceptors comes before validation interceptor,
does it imply that I put validation before conversionError interceptor will solve this issue?

This Action is integrated with String 2.5.6 in session scope with no DAO.

Thanks

 
Patrick Kok
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, after some tests, I found some very interesting things (bugs?).

I keep the defaultStack interceptor, and I add two overload methods:

public class Item {
private Integer id;
private Integer qty;

setId(Integer,id){this.id=id;}
setQty(Integer,qty){this.qty=qty;}

setId(String, id){this.id=0;}
setQty(String, qty){this.qty=0;}

...
}

The struts2 form contains two textfields of item.id and item.qty, which are all declared as int in validation xml file.

I found that the first item.id return me 0 without exception,
but struts2 cannot perform setQty(String,qty)!! and it still prompts me the exception:

java.lang.NoSuchMethodException: com.xxx.xxxxx.xxxxx.Item.setQty([Ljava.lang.String;)


What happen here? a bug?
thanks
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patrick there isn't enough information available to be able to help you. Provide us with the relevant excerpts from your JSP page where the form is. Also show us the Items class. In the error, setQty([Ljava.lang.String;) means that its looking for a method setQty which takes a String array, are there more than one text fields in your JSP which have the name Qty??
 
Patrick Kok
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Patrick there isn't enough information available to be able to help you. Provide us with the relevant excerpts from your JSP page where the form is. Also show us the Items class. In the error, setQty([Ljava.lang.String;) means that its looking for a method setQty which takes a String array, are there more than one text fields in your JSP which have the name Qty??


Thanks Garg,

I added two more overload method before your post:

setId(String[] id){...}
setQty(String[] qty){...}

and I added logger inside each method call on Item.class.
The result really surprises me. The setId(String[] id) and setQty(String,qty) are used when I submit string against these fields.
This ugly hack works without exception, but I don't understand why.

I cannot post the code since there are a lot of dependencies, but I used sitemesh to organize different form templates.
This is one of them. Poor enough, I renamed the form name, form id... and result the same.
There is no textfield with same name.

However, this test is not the main point.

My Main Point is:
How can I prevent the type mismatch problem from the user data being submitted into the interceptor to produce exception.
I am looking for this on internet but it seems nobody cares about this.
What should I do to such validation?

thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic