Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-3357

ognl.MethodFailedException when you do not enter a value for a field mapped to an int.

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Not A Bug
    • Affects Version/s: 2.1.8
    • Fix Version/s: 2.5.12
    • Component/s: Core Actions
    • Labels:
      None
    • Environment:

      Websphere Application Server 6.1

      Description

      I'm getting:

      ognl.MethodFailedException: Method "setMyIntVariable" failed for object org.kevin.struts2.test.actions.TestAction@79fa79fa [java.lang.NoSuchMethodException: org.kevin.struts2.test.actions.TestAction.setMyIntVariable([Ljava.lang.String;)]
      

      ...when I try to submit a form with a <s:textfield> that is mapped to an integer in my action. Previously, there was an issue where this happened if the value was <= 0, but this happens blank values. Should it not default to zero?

      Here is a snippet from my .jsp:

      <s:form action="validatoraction">
      	<s:textfield name="myIntVariable" label="Integer variable: "/>
      	<s:textfield name="myStringVariable" label="String value: " />
      	<s:submit />
      </s:form>
      

      Here is my struts.xml

      <struts>
      	<constant name="struts.devMode" value="false" />
          <constant name="struts.action.extension" value="action" />
          <package name="struts2testerdefault" namespace="" extends="struts-default">
      	    <action name="foo">
      	    	<result>/bar.jsp</result>
      	    </action>
      	    <action name="validatoraction" class="org.kevin.struts2.test.actions.TestAction">
      	    	<result>/maintesterresult.jsp</result>
      	    	<result name="input">/maintesterpage.jsp</result>
      	    </action>
      	    <action name="maintesterpage">
      			<result>/maintesterpage.jsp</result>
      		</action>
          </package>
      </struts>
      

      Here is my action:

      package org.kevin.struts2.test.actions;
      
      import java.sql.Timestamp;
      import java.util.Date;
      
      import com.opensymphony.xwork2.ActionSupport;
      
      public class TestAction extends ActionSupport {
      	
      	/**
      	 * 
      	 */
      	private static final long serialVersionUID = 1L;
      	private int myIntVariable;
      	private String myStringVariable;
      	private Timestamp myTimeStamp;
      	private Date myDate;
      	
      	@Override
      	public String execute() throws Exception {
      		myTimeStamp = new Timestamp(new java.util.Date().getTime());
      		myDate = new java.util.Date();
      		return SUCCESS;
      	}
      
      	public int getMyIntVariable() {
      		return myIntVariable;
      	}
      
      	public void setMyIntVariable(int myIntVariable) {
      		this.myIntVariable = myIntVariable;
      	}
      
      	public String getMyStringVariable() {
      		return myStringVariable;
      	}
      
      	public void setMyStringVariable(String myStringVariable) {
      		this.myStringVariable = myStringVariable;
      	}
      
      	public Timestamp getMyTimeStamp() {
      		return myTimeStamp;
      	}
      
      	public void setMyTimeStamp(Timestamp myTimeStamp) {
      		this.myTimeStamp = myTimeStamp;
      	}
      
      	public Date getMyDate() {
      		return myDate;
      	}
      
      	public void setMyDate(Date myDate) {
      		this.myDate = myDate;
      	}
      
      }
      

        Activity

        Hide
        lukaszlenart Lukasz Lenart added a comment -

        I think it's related to the same problem with primitives (and wrappers) convertion

        Show
        lukaszlenart Lukasz Lenart added a comment - I think it's related to the same problem with primitives (and wrappers) convertion
        Hide
        mrswadge Stuart Stephen added a comment -

        I am also having the same issue with a java.sql.Timestamp based attribute. The date I entered into the field was "not a date". Of course, XWorkBasicConverter fails to convert successfully and throws an XWorkException, then it is downhill from there on. When attempting to find the method to call in OgnlRuntime it appears to look for a String parameter, when it should be looking for the java.sql.Timestamp parameter.

        Show
        mrswadge Stuart Stephen added a comment - I am also having the same issue with a java.sql.Timestamp based attribute. The date I entered into the field was "not a date". Of course, XWorkBasicConverter fails to convert successfully and throws an XWorkException, then it is downhill from there on. When attempting to find the method to call in OgnlRuntime it appears to look for a String parameter, when it should be looking for the java.sql.Timestamp parameter.
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        No, it won't default to 0 as this isn't the same as "" (an empty string). If you allow submitting empty values (no a required field validator is present) then you must use Integer instead of int

        Show
        lukaszlenart Lukasz Lenart added a comment - No, it won't default to 0 as this isn't the same as "" (an empty string). If you allow submitting empty values (no a required field validator is present) then you must use Integer instead of int
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Stuart Stephen Struts doesn't support conversion of the java.sql.Timestamp class, you must implement your own converter and register it - here you have an example app https://github.com/apache/struts-examples/tree/master/type-conversion

        Show
        lukaszlenart Lukasz Lenart added a comment - Stuart Stephen Struts doesn't support conversion of the java.sql.Timestamp class, you must implement your own converter and register it - here you have an example app https://github.com/apache/struts-examples/tree/master/type-conversion

          People

          • Assignee:
            lukaszlenart Lukasz Lenart
            Reporter:
            kadams Kevin Adams
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development