播放框架的输入文本值始终传递 0
play framework's input text value always passes 0
几天来我一直在研究一种游戏形式,感到非常沮丧。我浏览了文档并使用了 "Play with Java" 书,并创建了一个 userWeights 模型 class:
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
字段将是 blank/zero/whatever (在这种情况下我无论如何都不会使用它)。反之亦然。
问题 - 当我选中复选框并填写 sample
文本输入时,它会将值 0 绑定到表单。
我试过将 input
标记中的 value
设置为 null,将其完全删除并使用模板助手(我宁愿避免使用它,因为它不那么灵活)。我不明白为什么我在文本字段中输入的数字被忽略,我的程序认为它是 0。
所以问题 1:如何停止填充 0 值?
问题 2 如何为其中一个文本字段(sampleSize
或 misSampleSize
)发送值,而将另一个留空,没有收到表格错误?
谢谢!
我在 Scala 工作,而不是 Java。但是你我认为你需要输入 id 和 name 字段来匹配你的表单字段名称。它使用那些来绑定。你没有显示你的 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" 以防(可能是错误)没有值。
几天来我一直在研究一种游戏形式,感到非常沮丧。我浏览了文档并使用了 "Play with Java" 书,并创建了一个 userWeights 模型 class:
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
字段将是 blank/zero/whatever (在这种情况下我无论如何都不会使用它)。反之亦然。
问题 - 当我选中复选框并填写 sample
文本输入时,它会将值 0 绑定到表单。
我试过将 input
标记中的 value
设置为 null,将其完全删除并使用模板助手(我宁愿避免使用它,因为它不那么灵活)。我不明白为什么我在文本字段中输入的数字被忽略,我的程序认为它是 0。
所以问题 1:如何停止填充 0 值?
问题 2 如何为其中一个文本字段(sampleSize
或 misSampleSize
)发送值,而将另一个留空,没有收到表格错误?
谢谢!
我在 Scala 工作,而不是 Java。但是你我认为你需要输入 id 和 name 字段来匹配你的表单字段名称。它使用那些来绑定。你没有显示你的 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" 以防(可能是错误)没有值。