NoSuchMethodException in Struts2

前端 未结 1 1783
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 14:30

I have textfield for birthDate. When a user enter invalid date, let say for example a String, error message successfully displayed as fielderror. But in my console, I got this e

相关标签:
1条回答
  • 2021-01-25 14:41

    In your Action class you dont have a method called setBirthDate(String birthDate), add it your issue should be resolved.

    Note check to see that you have placed all getter and setter in your action class for all properties.

    I think in your JSP you have :

    <s:textfield name="birthDate" />
    

    Struts will try to map this to setBirthDate(String string), since this method is missing in your action hence the NoSuchMethodException

    Update:

    To convert String to Date:

    public class MyStringToDateConverter extends StrutsTypeConverter {
        public Object convertFromString(Map context, String[] values, Class toClass) {
           //Parse String to get a date object
        }
    
        public String convertToString(Map context, Object o) {
           // Get the string from object o
        }
     }
    

    If you are using Annotation in your action class then add @Conversion() to your action

    @Conversion()
    public class MyAction extends ActionSupport{
        public Date myDate = null;
    
        @TypeConversion(converter="MyStringToDateConverter") //Fully qualified name so if this class is in mypackage then converter will be "myPackage.MyStringToDateConverter"
        public void setMyDate(Date date) { 
            this.myDate = date;
        }
    }
    

    If you dont want to use Annotation then you can look at the official documentation for example.

    0 讨论(0)
提交回复
热议问题