我已经在一个游戏表格上工作了几天而且非常沮丧。我浏览了文档并使用了“Play with Java”一书,并创建了一个userWeights模型类:
public class UserWeights {
public HashMap<ServicesWeights, Float> userWeights = new HashMap<>();
@Constraints.Required
public String user;
@Constraints.Required
public String password;
public int sampleSize;
public int misSampleSize;
}
我的控制器:
public class Application extends Controller {
private static final Form<UserWeights> userWeightsForm = Form.form(UserWeights.class);
public static Result index() {
return ok(index.render("Your new application is ready."));
}
public static Result finder () {
return ok(finder.render(userWeightsForm));
}
public static Result runWithUserInput () {
Form<UserWeights> boundForm = userWeightsForm.bindFromRequest();
if (boundForm.hasErrors()) {
return badRequest(index.render("FAIL"));
}
UserWeights weights = boundForm.get();
if (weights.checkboxChoices.get(0) != null && weights.checkboxChoices.get(0).equals("1")) {
runMethodA();
} else if (weights.checkboxChoices.get(1) != null && weights.checkboxChoices.get(1).equals("2")) {
runMethodB();
}
return TODO;
}
观点:
@(UserWeightsForm: Form[UserWeights])
@import helper._
@import helper.twitterBootstrap._
@main("finder") {
<h1>Please fill the required fields</h1>
@helper.form(action = routes.Application.runWithUserInput(), 'enctype -> "multipart/form-data") {
<fieldset>
<legend>Finder</legend>
@helper.inputText(UserWeightsForm("user"),'_label -> "User")
@helper.inputPassword(UserWeightsForm("password"), '_label -> "Password")
<br/>
<label for="checkboxInput">Input Type:</label><br/>
<span class="buttonSet" id="checkboxInput">
<input type = "checkbox" id = "genericCheckbox" name = 'checkboxChoices[0]' value = "1">
<label for = "genericCheckbox">Generic Sample</label>
<input type = "number" name = 'UserWeightsForm("sampleSize")' id = "genericInput" value = "@UserWeightsForm("sampleSize").value"><br/>
<input type = "checkbox" id = "misCheckbox" name = 'checkboxChoices[1]' value = "2">
<label for = "misCheckbox">MisSample</label>
<input type = "number" name = 'UserWeightsForm("misSampleSize")' id = "misInput" value = "@UserWeightsForm("misSampleSize").value"><br/>
</span>
我想要的 - 如果选中第一个复选框,则用户将填写sampleSize
文本输入字段,该值将限制在表单中,而misSampleSize
字段将为空/ zero / what(在这种情况下,我还没有使用它)。反之亦然。
问题 - 当我选中复选框并填写sample
文本输入时,它会将值0绑定到表单。
我已尝试将value
标记中的input
设置为null,将其完全删除并使用模板帮助程序(我更喜欢避免它,因为它不够灵活)。我无法理解为什么我输入文本字段的数字被忽略,我的程序认为它是0。
所以问题1 :如何停止填充0值?
问题2 如何为其中一个文本字段(sampleSize
或misSampleSize
)发送值,并将另一个留空,而不会出现表单错误?
谢谢!
答案 0 :(得分:1)
我在Scala工作,而不是Java。但是我认为您需要输入ID和名称字段以匹配表单字段名称。它使用那些绑定。您没有显示您的UserWeightsForm,但您可以尝试类似:
<input type="number" name='sampleSize' id="sampleSize" value="@UserWeightsForm("sampleSize").value.getOrElse("0")"><br/>
<input type="number" name='misSampleSize' id="misSampleSize" value="@UserWeightsForm("misSampleSize").value.getOrElse("0")"><br/>
我也在值上使用“getOrElse”(也许是错误)没有值。