简体   繁体   中英

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

I'm new to Play Framework. 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.

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.

This is my Code:

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

package models;

public class User {

    public String name;
    public String tableNr;

}

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]

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

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