Struts2 中的 NoSuchMethodException [英] NoSuchMethodException in Struts2

查看:29
本文介绍了Struts2 中的 NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用于birthDate 的文本字段.当用户输入无效日期时,例如一个字符串,错误消息成功显示为 fielderror.但是在我的控制台中,我收到了这个错误:

java.lang.NoSuchMethodException: Profile.setBirthDate([Ljava.lang.String;)

我是否遗漏了什么导致我遇到错误的原因?

解决方案

在你的 Action 类中,你没有名为 setBirthDate(StringbirthDate) 的方法,添加它你的问题应该得到解决.

注意检查您是否已将所有 getter 和 setter 放置在所有属性的操作类中.

我认为在你的 JSP 中你有:

Struts 将尝试将其映射到 setBirthDate(String string),因为您的操作中缺少此方法,因此 NoSuchMethodException

更新:

将字符串转换为日期:

public class MyStringToDateConverter extends StrutsTypeConverter {public Object convertFromString(Map context, String[] values, Class toClass) {//解析字符串以获取日期对象}公共字符串 convertToString(地图上下文,对象 o){//从对象 o 中获取字符串}}

如果您在操作类中使用注解,则将 @Conversion() 添加到您的操作

@Conversion()公共类 MyAction 扩展了 ActionSupport{公共日期 myDate = null;@TypeConversion(converter="MyStringToDateConverter")//完全限定名称,所以如果这个类在mypackage中,那么转换器将是myPackage.MyStringToDateConverter"public void setMyDate(Date date) {this.myDate = 日期;}}

如果不想用Annotation那可以看官方文档例如.

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 error:

java.lang.NoSuchMethodException: Profile.setBirthDate([Ljava.lang.String;)

Have I missed something that's why I encountered the error?

解决方案

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.

这篇关于Struts2 中的 NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆