java Playframework JSR-303 验证的“字段”没有用于数据绑定的相应访问器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16860046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:11:32  来源:igfitidea点击:

Playframework JSR-303 validated "field" does not have a corresponding accessor for data binding

javaweb-applicationsplayframework

提问by sadaf2605

When I have added this following code to my project

当我将以下代码添加到我的项目时

Form<User> filledForm2 = userSignupForm.bindFromRequest();

It has stopped working by showing an error message which states:

它已通过显示一条错误消息停止工作,其中指出:

Execution exception
[IllegalStateException: JSR-303 validated property 'Password' does not have a corresponding accessor for data binding - check your DataBinder's configuration (bean property versus direct field access)]

My Userclass was like that:

我的User课是这样的:

class User{
String username;
String Password;
}

Now how can to check/modify DataBinder's configuration in java play framework?

现在如何在 java play 框架中检查/修改 DataBinder 的配置?

回答by a darren

Actually this shouldn't be happening, as Play automatically generates getters and setters, see Guillaume's comment.

实际上这不应该发生,因为 Play 会自动生成 getter 和 setter,请参阅 Guillaume 的评论

Therefore it's possible that your IDE is causing issues e.g. Guillaume's comment re Eclipse. Or that your sbt cache is corrupt and needs cleaning, which you can do with play clean-all(read about it here)

因此,您的 IDE 可能导致问题,例如 Guillaume 对 Eclipse 的评论。或者您的 sbt 缓存已损坏并需要清理,您可以这样做play clean-all在此处阅读有关内容

By the way, changing your Passwordattribute to passwordmay have caused the cache to be re-generated and therefore fixed the issue.

顺便说一句,将您的Password属性更改为password可能会导致重新生成缓存并因此解决了该问题。

Update:

更新:

For more recent versions of Play that use activator, it seems the following are the the up-to-date equivalents:

对于使用 的较新版本的 Play,activator以下似乎是最新的等效项:

activator cleanand activator clean-files

activator cleanactivator clean-files

回答by Ataru

My environment is Mac OS X 10.9.5 and run CentOS 7 with Vagrant. On CentOS the same problem happended, but on Mac OS didn't. Play Framework version is 2.3.8.

我的环境是 Mac OS X 10.9.5 并使用 Vagrant 运行 CentOS 7。在 CentOS 上发生了同样的问题,但在 Mac OS 上没有。Play 框架版本为 2.3.8。

The commnad below didn't work in my case:

下面的命令在我的情况下不起作用:

  activator clean
  activator clean-files

Though it's so weird, I had to define getter and setter to work Play Framework on CentOS7 successfully as following:

虽然很奇怪,但我必须定义 getter 和 setter 才能在 CentOS7 上成功运行 Play Framework,如下所示:

@Entity
public class Message extends Model{

    @Id
    public long id;

    @Constraints.Required
    public String name;


    //Add getter and setter to work successfully...
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

回答by Casey Murray

I've fixed this issue by using activator clean && activator clean-filesbefore doing activator dist

activator clean && activator clean-files在做之前通过使用解决了这个问题activator dist

回答by iboz

I got the same error trying to validate intfield in POJO using @Min(value = 0).

我在尝试int使用 @Min(value = 0).

Solved by putting the annotation on getter in combination with @Validin my bean.

通过将注释放在 getter 上并结合@Valid在我的 bean 中来解决。

回答by Mike

Had the same issue...

有同样的问题...

To remove this error, you should define your variables as public.

要消除此错误,您应该将变量定义为 public。

class User{
   public String username;
   public String password;
}