3

我正在尝试基于播放框架示例“zentask”创建一个登录页面。

当我尝试在登录页面中提交表单时,user_name 和 user_pass 为空。

命令提示符输出:

[info] play - database [default] connected at jdbc:mysql://localhost/smgts2
[info] play - Application started (Dev)
null null

应用程序.java:

package controllers;

import play.*;
import play.mvc.*;
import play.data.*;
import static play.data.Form.*;

import models.*;
import views.html.*;

public class Application extends Controller {

    // -- Authentication

    public static class Login {

        public String user_name;
        public String user_pass;

        public String validate() {
            if(Accounts.authenticate(user_name, user_pass) == null) {
                return "Invalid user or password";
            }
            return null;
        }

    }

    /**
     * Login page.
     */
    public static Result login() {
        return ok(
            login.render(form(Login.class))
        );
    }

    /**
     * Handle login form submission.
     */

    public static Result authenticate() {
        Form<Login> loginForm = form(Login.class).bindFromRequest();
        if(loginForm.hasErrors()) {
            return badRequest(login.render(loginForm));
        } else {
            session("user_name", loginForm.get().user_name);
            return redirect(
                routes.Home.index()
            );
        }
    }

    /**
     * Logout and clean the session.
     */

    public static Result logout() {
        session().clear();
        flash("success", "You've been logged out");
        return redirect(
            routes.Application.login()
        );
    }
}

login.scala.html

@(form: Form[Application.Login])

@main(Html("School Management System")) {
    <style type="text/css">
    body {
            padding-top: 60px;
            padding-bottom: 40px;
            background-color: #f5f5f5;
    }
    </style>

    @helper.form(routes.Application.authenticate, 'class -> "form-signin") {

            <h2 class="form-signin-heading">Sign-in to SMGTS</h2>

            @if(form.hasGlobalErrors) { 
                <p class="text-error">
                    @form.globalError.message
                </p>
            }

            @if(flash.contains("success")) {
                <p class="text-success">
                    @flash.get("success")
                </p>
            }

            <p>
                <input type="text" class="input-block-level" name="user_name" placeholder="Username" value='@form("user_name").value'>
            </p>
            <p>
                <input type="password" class="input-block-level" name="user_pass" placeholder="Password">
            </p>
            <p>
                <button class="btn btn-large btn-primary" type="submit">Sign in</button>
            </p>


}
}

路线文件:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Home.index()

# Authentication
GET     /login                      controllers.Application.login()
POST    /login                      controllers.Application.authenticate()
GET     /logout                     controllers.Application.logout()

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

Accounts.java(模型类):

package models;

import java.util.*;
import javax.persistence.*;

import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;

import com.avaje.ebean.*;

@Entity
@Table(name="user_account")
public class Accounts extends Model {

    @Id
    @Constraints.Required
    public Long acc_id;

    @Constraints.Required
    @Formats.NonEmpty
    public String user_name;

    @Constraints.Required
    @Formats.NonEmpty
    public String user_pass;

    @Constraints.Required
    public int group_id;

    // Queries

    public static Finder<Long,Accounts> find = new Finder<Long,Accounts>(Long.class, Accounts.class);

    public static Accounts authenticate(String user_name, String user_pass) {

        System.out.println(user_name + " " + user_pass); // Check if form data is passed.

        return find.where()
            .eq("user_name", user_name)
            .eq("user_pass", user_pass)
            .findUnique();

    }

}

I've been checking if the variable names in Application.java and they're the same with the input fields in login.scala.html.
4

2 回答 2

15

尝试运行 play clean。它应该神奇地修复它!

于 2013-06-07T17:17:40.637 回答
0

在阅读“Play for Java”一书时,我偶然发现了同样的问题。我使用的是 Play 2.5.x,示例使用的是 2.2.x。经过几个小时的搜索解决方案,与其他示例进行比较,我终于找到了解决方案:

模型类中缺少 setter 和 getter。

在我添加了 getter 和 setter 之后,表单数据终于通过了。

替代解决方案:PlayEnhancer

于 2017-05-28T19:39:35.583 回答