简体   繁体   English

玩! 2.1-RC2 JavaForms validate()没有静态引用

[英]Play! 2.1-RC2 JavaForms validate() without static references

I've been going through the Play! 我一直在玩游戏! 2.1 example to setup a basic login system following the ZenTasks example. 按照ZenTasks示例设置基本登录系统的2.1示例。 Where I get stuck is the JavaForms part. 卡住的地方是JavaForms部分。 I want to validate the login request using an instance of an auth service that is provided via Guice DI. 我想使用通过Guice DI提供的auth服务的实例来验证登录请求。

I'm following Play20 Sample . 我正在关注Play20 Sample This example uses a static authenticate() method to run the authentication when form validation is requested after form submission. 在提交表单后请求表单验证时,此示例使用静态authenticate()方法运行身份验证。 Any thoughts on how to perform this validation step in a non-static scope? 关于如何在非静态范围内执行此验证步骤的任何想法?

Note: I have looked at the Play! 注意:我已经看过Play! Authenticate plugin as well as the SecureSocial plugin, however those projects are overkill for what I want to do right now. 对插件以及SecureSocial插件进行身份验证,但是这些项目对于我现在想做的事情来说是过大的。 Also, I am interested in a general solution for allowing non-static validation in JavaForms. 另外,我对允许在JavaForms中进行非静态验证的通用解决方案感兴趣。

Edit: It seems there is some confusion about what I am asking for here. 编辑:似乎有一些关于我在这里要求的困惑。 What I am hoping to find is an alternate way to perform the validation step of the form submission that is sent by a Play! 我希望找到一种替代方法来执行Play发送的表单提交的验证步骤! framework Form.form() generated form. 框架Form.form()生成的表单。 Currently it requires that a validate() method be called on an instance of a POJO which is not created through the DI framework. 当前,它要求在未通过DI框架创建的POJO实例上调用validate()方法。 This results in static references being required to access authorization services etc... 这导致访问授权服务等需要静态引用。

Edit 2: The current solution I am working with is this: 编辑2:我正在使用的当前解决方案是这样的:

public static class AuthServiceFormReference {
    @Inject
    public static Provider<AuthService> authService;        
}

// In my auth module configure()
//...
    requestStaticInjection(AuthController.AuthServiceFormReference.class);
//...

public static class Login {
    @Required
    public String email;
    @Required
    public String password;

    public String validate(){
        if(AuthServiceFormReference.authService.get().authenticateAdmin(email, password) == null) {
            return "Invalid user or password";
        }
        return null;
    }
}

It's an okay workaround, but it still relies on static injection :( 这是一个好的解决方法,但是它仍然依赖于静态注入:(

Play Framework does not offer Dependency Injection out of the box. Play Framework不提供开箱即用的Dependency Injection However you can integrate it with Guice or Spring. 但是,您可以将其与Guice或Spring集成。 As a lazy developer you could also create a Singleton for the service, or make it a plugin (as it probably needs to prepare work on application startup anyways). 作为一个懒惰的开发人员,您还可以为该服务创建一个Singleton或将其制成插件(因为它可能仍然需要为应用程序启动做准备工作)。 Then you can get a reference to your plugin -- Play.application().plugin(AuthPlugin.class) . 然后,您可以获得对插件的引用Play.application().plugin(AuthPlugin.class)

In this particular case you can do a database lookup in the validate methods, eg 在这种情况下,您可以使用validate方法进行数据库查找,例如

User u = User.find.where().eq("username", username).eq("password`,password).findUnique();
if (u == null)
 return "Error.";
else
 return null;

So this isn't so much about Play Framework, but Java programming in general. 因此,这与Play Framework无关,而是Java编程。

Assuming you use Spring , you can do it like in any other Java program: 假设您使用Spring ,则可以像在其他任何Java程序中一样进行操作:

@Configurable
public class MyModel {
    @Autowired
    transient MyService myService;

    public String validate() { ... }
}

The @Configurable annotation makes the class capable of dependency injection. @Configurable批注使该类能够进行依赖项注入。 The transient qualifier makes sure the field doesn't get picked up as a field to save into a database. 瞬态限定符确保该字段不会被选择为要保存到数据库中的字段。

Spring works fine with Play 2.0/2.1 in one of my projects. 在我的一个项目中,Spring在Play 2.0 / 2.1中运行良好。 I don't consider dependency injection in model objects aesthetically pleasing, but if you need it you can use it. 我不认为在美观的模型对象中使用依赖项注入,但是如果需要,可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM