简体   繁体   中英

Play Framework: Form data not passed

I am trying to create a login page based on the play framework sample 'zentask'.

When I try submitting the form in the login page, user_name and user_pass are null.

Command Prompt Output:

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

Application.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 File:

# 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 (Model Class):

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.

Try running play clean. It should magically fix it!

I stumbled over the same issue while working trough the book "Play for Java". I was working with Play 2.5.x and the examples worked with 2.2.x. After hours of searching for the solution, comparing with other examples I finally found the solution:

Missing setters and getters in the model class.

After I added the getters and setters the Form data finally got passed.

Alternative solution: PlayEnhancer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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