我有一个HTML表单,想要通过我的Controller处理它。但是,如果我想访问一个字段,例如记录或打印出来,它总是给我一个空值
有人知道为什么吗?
控制器:
public static Result register() {
DynamicForm users = Form.form().bindFromRequest();
Logger.info(users.get("vorname"));
return ok("hallo " + users.get("vorname"));
}
查看:
<form role="form" class="form-horizontal" action="@routes.Signup.register()" method="POST">
<div class="form-group">
<label for="vorname" class="col-sm-2 control-label">Vorname</label>
<div class="col-sm-10">
<div class="col-xs-4">
<input type="text" class="form-control" id="vorname" placeholder="Vorname">
</div>
</div>
</div>
<div class="form-group">
<label for="nachname" class="col-sm-2 control-label">Nachname</label>
<div class="col-sm-10">
<div class="col-xs-4">
<input type="text" class="form-control" id="nachname" placeholder="Nachname">
</div>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email-Adresse</label>
<div class="col-sm-10">
<div class="col-xs-4">
<input type="email" class="form-control" id="email" placeholder="Email-Adresse">
</div>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Passwort</label>
<div class="col-sm-10">
<div class="col-xs-4">
<input type="password" class="form-control" id="password" placeholder="Passwort">
</div>
</div>
</div>
<div class="form-group">
<label for="rpassword" class="col-sm-2 control-label">Passwort wiederholen</label>
<div class="col-sm-10">
<div class="col-xs-4">
<input type="password" class="form-control" id="rpassword" placeholder="Passwort wiederholen">
</div>
</div>
</div>
<div class="form-group">
<label for="geschlecht" class="col-sm-2 control-label">Geschlecht</label>
<div class="col-sm-10">
<div class="col-xs-4">
<select class="form-control" id="geschlecht">
<option>Männlich</option>
<option>Weiblich</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="col-xs-4">
<button type="submit" class="btn btn-default">Registrieren</button>
</div>
</div>
</div>
</form>
答案 0 :(得分:1)
我认为你应该使用:
DynamicForm form = new DynamicForm().bindFromRequest();
我还鼓励您使用表单助手,以确保正确配置所有内容。 https://playframework.com/documentation/2.3.x/JavaFormHelpers
编辑。是的,你应该使用肯定使用助手。你忘了在你的vorname输入中加上“name”属性。在处理表单时,此属性(而非id)至关重要。