我正在尝试根据播放框架示例'zentask'创建一个登录页面。
当我尝试在登录页面中提交表单时,user_name和user_pass为null。
命令提示输出:
[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
# 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.
答案 0 :(得分:14)
尝试运行播放干净。它应该神奇地修复它!
答案 1 :(得分:0)
在使用“Play for Java”这本书时,我偶然发现了同样的问题。我正在使用Play 2.5.x,这些示例适用于2.2.x.经过几个小时的搜索解决方案,与其他例子相比,我终于找到了解决方案:
在模型类中缺少setter和getter。
在我添加了getter和setter之后,Form数据终于通过了。
替代解决方案:PlayEnhancer