2

This happens not all the time, but frequently, and it's annoying. When I try to get a value from <s:form /> other than String (declared in Action), it gives me an exception like java.lang.NoSuchMethodException.

Nov 23, 2011 4:13:20 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn WARNING: Error setting expression 'passengers' with value '[Ljava.lang.String;@157b2d' ognl.MethodFailedException: Method "setPassengers" failed for object com.deveto.struts.actions.BookFlightAction@14928cc [java.lang.NoSuchMethodException: com.deveto.struts.actions.BookFlightAction.setPassengers([Ljava.lang.String;)]

The code from Action:

private Integer passengers;

public Integer getPassengers() {
    return passengers;
}

public void setPassengers(Integer passengers) {
    this.passengers = passengers;
}

The jsp:

<s:form action="book-flight" theme="simple">
        <s:textfield name="passengers" value=""/>
</s:form>

And it is strange that it happens at random all the time, not at some specific point. Do you guys have some advices?

Update: I found this:

1) https://bugs.java.com/bugdatabase/view_bug?bug_id=6434149

2) http://dev.bostone.us/2009/02/23/javaxfacesfacesexception-javalangclassnotfoundexception-ljavalangstring/#awp::2009/02/23/javaxfacesfacesexception-javalangclassnotfoundexception-ljavalangstring/ (but it is for Eclipse only)

11
  • 1
    shouldn't the value be an integer like value=0 because "" should be a String and you only have setPassanders(Integer) and not setPassanders(String). Also you probably have to set the type somewhere Commented Nov 23, 2011 at 14:27
  • This is a trick for autocomplete-off, but when I submit the form, there is a value, entered by the user. Commented Nov 23, 2011 at 14:28
  • Give an example of the value that throws the exception. Your method is expecting an Integer, make sure it is one and not a String instead. Commented Nov 23, 2011 at 14:29
  • It is an Integer, I am 200% convinced, an it is 7 for example. Commented Nov 23, 2011 at 14:29
  • I'n not sure, but it may need an int field instead of Integer. Commented Nov 23, 2011 at 14:33

2 Answers 2

2

This case can only happen when incoming string values does not represent a valid instance of primitive or types.

since as per your statement it is happening only few time which indicates that the input is not what expected

OGNL provides out of the box type conversion for all of primitive and its wrapper types. Just debug this to find out what exactly is being passed in passengers.

 There is no need to do any type conversion in struts2 until unless it is 
 being not provided by OGNL else you are not using 
 framework build in capabilities
Sign up to request clarification or add additional context in comments.

8 Comments

Can you post your struts config file and jsp some what all data inside form field?? Just eager what can be an issue as i never faced it.
Look in the first post, I updated it. Now I need somehow to fix it. :)
@DenisHoss: i can't see struts config file
There is no config file, because the problem is not in config file, there is 2 links, that redirects you to a bugs page
I already gone through them.your log is being thrown by the OGNL and it indicates that some how it is trying to set value for the attributes whose setter method is not defined. that is not a exception but as per my understanding there is going some triggering of some action and struts2 trying to set "passengers" params and it is unable to find matching setter method.Can only be debugged more if u able to pin point what are those "few" cases when this is happening
|
2

Like the name Textfield suggests this element only deals with text. You can force it to accept nothing but numbers by using a validator as described here: Field Validators

You will however still receive a String like "7" instead of an integer. You have to do the parsing manually by using Integer.parseInt(String).

edited out the wrong part.

3 Comments

I understand, but it is not all the time, sometimes the value comes correct and without exceptions :)
Well, that's weird. Could you overload setPassengers(String) and print the String that gets transmitted and whether it has whitespaces at the beginning or end?
@PeterT Everything that comes from a browser is a string. Type conversion happens at the framework level, and is automatic in Struts 2. You do not need to "parse manually".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.