播放 java 应用程序时出现以下错误。请建议我在这里做错了什么。我不明白是否需要启用 CSRF 如果是这样我已经添加了 application.conf 下面的行
play.filters.enabled += "play.filters.csrf.CSRFFilter"
Cannot find any HTTP Request Header here
In /application/app/views/add_saving.scala.html:11
7 <div class="login">
8 <h1>Add saving account module </h1>
9 </div>
10 form(action = routes.HomeController.addSavingSubmit, 'class -> "form-horizontal", 'role->"form") {
11 @CSRF.formField
12 @inputText(addSavingForm("AccountNo"), '_label -> "AccountNo",'class -> "form-control" )
13 @inputText(addSavingForm("FirstName"), '_label -> "First Name",'class -> "form-control")
14 @inputText(addSavingForm("LastName"), '_label -> "Last Name",'class -> "form-control")
15 <div class="actions">
这是 add_saving.scala.html
@(addSavingForm: Form[models.Account])
@import models._
@import helper._
@main("Adding saving account") {
<!-- this css class is not part of bootstrap but is defined in /css/style.css -->
<section id="content">
<div class="login">
<h1>Add saving account module </h1>
</div>
form(action = routes.HomeController.addSavingSubmit, 'class -> "form-horizontal", 'role->"form") {
@CSRF.formField
@inputText(addSavingForm("AccountNo"), '_label -> "AccountNo",'class -> "form-control" )
@inputText(addSavingForm("FirstName"), '_label -> "First Name",'class -> "form-control")
@inputText(addSavingForm("LastName"), '_label -> "Last Name",'class -> "form-control")
<div class="actions">
<input type="submit" value="Create Account" class="btn btn-primary">
<a href="@routes.HomeController.view_accounts">
<button class="btn btn-warning">Cancel</button>
</a>
</div>
}
</section>
}
这是 HomeController.java
public class HomeController extends Controller {
private FormFactory formFactory;
/** http://stackoverflow.com/a/37024198 **/
//private Environment env;
/** http://stackoverflow.com/a/10159220/6322856 **/
@Inject
public HomeController(FormFactory f) {
this.formFactory = f;
}
/**
* An action that renders an HTML page with a welcome message.
* The configuration in the <code>routes</code> file means that
* this method will be called when the application receives a
* <code>GET</code> request with a path of <code>/</code>.
*/
public Result index() {
return ok(index.render());
}
public Result manage() {
return ok(manage.render());
}
public Result statement() {
return ok(statement.render());
}
@Transactional
public Result view_accounts() {
List<Account> AccountsList = Account.find.all();
return ok(view_accounts.render(AccountsList));
}
public Result add_accounts() {
return ok(add_accounts.render());
}
public Result close_accounts() {
return ok(close_accounts.render());
}
@Transactional
public Result add_saving() {
Form<Account> addSavingForm = formFactory.form(Account.class);
// Render the Add Product View, passing the form object
return ok(add_saving.render(addSavingForm));
}
@Transactional
public Result addSavingSubmit() {
// Create a product form object (to hold submitted data)
// 'Bind' the object to the submitted form (this copies the filled form)
Form<Account> newSavingForm = formFactory.form(Account.class).bindFromRequest();
// Check for errors (based on Product class annotations)
if(newSavingForm.hasErrors()) {
// Display the form again
return badRequest(add_saving.render(newSavingForm));
}
// Save if new (no id) otherwise update product
Account p = newSavingForm.get();
if (p.getId() != null) {
p.update();
}
else {
p.save();
}
// Set a flash message
//flash("success", "Account " + newSavingForm.get().getFirstName() + " has been created or updated");
// Redirect to the products page
return redirect(controllers.routes.HomeController.View_accounts());
}
这是 Accounts.java
package models;
import io.ebean.Model;
import play.data.validation.Constraints;
import javax.persistence.Table;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import java.util.*;
import javax.persistence.*;
import io.ebean.*;
import play.data.format.*;
import play.data.validation.*;
@Entity
public class Account extends Model {
@Id
private Long AccountNo;
@Constraints.Required
private String FirstName;
private String LastName;
public Account(){}
public Account(Long id, String fn, String ln){
this.AccountNo=id;
this.FirstName=fn;
this.LastName=ln;
}
public static Finder<Long,Account> find = new Finder<>(Account.class);
public Long getId()
{
return this.AccountNo;
}
public String getFirstName()
{
return this.FirstName;
}
public String getLastName() {
return this.LastName;
}
public void setAccountNo(Long i)
{
this.AccountNo=i;
}
public void setFirstName(String firstName)
{
this.FirstName=firstName;
}
public void setLastName(String lastName){
this.LastName=lastName;
}
}