简体   繁体   English

Play Framework 2.5.x始终从模板形式获取“空”

[英]Play Framework 2.5.x always get 'null' from template form

I'm new to Play Framework. 我是Play框架的新手。 I trying to develop a simple application which someone can enter his name and a number and this should be saved in a Hashtable. 我试图开发一个简单的应用程序,有人可以输入他的姓名和数字,并将其保存在哈希表中。

But when I click on the submit button I always get "null" as value. 但是,当我单击“提交”按钮时,我总是得到“ null”作为值。

Does someone has an Idea? 有人有想法吗? I go through so many tutorials and stuff but I can't gt this to work. 我经历了太多的教程和内容,但是我无法使其正常工作。 Even a exact copy of the form from the play java tutorial won't work. 即使使用Play Java教程中的表格的精确副本也无法使用。

This is my Code: 这是我的代码:

HomeController.java HomeController.java

package controllers;

import play.mvc.*;
import java.util.*;
import models.User;
import play.data.Form;


public class HomeController extends Controller {

    Form<User> filledForm = Form.form(User.class);
    public Hashtable<Integer, String> hmap = new Hashtable<>();

    public Result index() {

        return ok(views.html.index.render());
    }

    public Result saveData() {


        User user = filledForm.bindFromRequest().get();

        if (filledForm.hasErrors()) {
            // process
        } else {
            // contactForm.get().firstName should be filled with the correct data
            System.out.println("Nothing to show here.");
        }


        System.out.println(user.name);
        System.out.println(user.tableNr);

        return ok(views.html.index.render());

    }


}

models.java models.java

package models;

public class User {

    public String name;
    public String tableNr;

}

template.html template.html

@(labels: Hashtable[Integer, String])

...
...
...

<form action="@routes.HomeController.saveData()">
      <input type="text" name="name" />
      <input type="text" name="tableNr" />
      <button>Submit</button>
</form>

Does someone know what I'm missing? 有人知道我在想什么吗?

Best regards 最好的祝福

In template define form:Form[HomeController:User] 在模板中定义以下形式:Form [HomeController:User]

or try @(labels: Hashtable[String, String]) 或尝试@(labels:Hashtable [String,String])

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

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