【问题标题】:play framework's input text value always passes 0播放框架的输入文本值总是传递 0
【发布时间】:2015-01-30 00:22:34
【问题描述】:

这几天我一直在研究一种游戏形式,非常沮丧。我浏览了文档并使用了“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 字段将为空白/零/随便(在这种情况下,无论如何我都没有使用它)。反之亦然。

问题 - 当我选中复选框并填写 sample 文本输入时,它会将值 0 绑定到表单。

我尝试将input 标记中的value 设置为null,将其完全删除并使用模板助手(我宁愿避免使用它,因为它不够灵活)。我不明白为什么我在文本字段中输入的数字被忽略了,而我的程序认为它是 0。

所以问题 1:如何停止填充 0 值?

问题 2如何为其中一个文本字段(sampleSizemisSampleSize)发送一个值,而将另一个留空,而不会出现表单错误?

谢谢!

【问题讨论】:

    标签: java playframework playframework-2.0 playframework-2.3


    【解决方案1】:

    我在 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”,以防(可能是错误)没有值。

    【讨论】:

    • 在 Java 中,输入名称应与表单字段名称匹配。所以应该是name="sampleSize"
    • 感谢您的回答,但它不起作用。它不会用 getOrElse() 编译,当我只输入你建议的字段名称时,我得到一个运行时异常 - [IllegalStateException: No value]。
    • 嗯,不知道。除了 getOrElse,它应该可以工作。你检查过区分大小写的错别字吗?几个建议:看看computer-database-java sample app。也许生成一个文本字段并转换它?查看生成的 html(检查元素),看看它是否让您了解出了什么问题。另请参阅this 等问题。
    • 找到了解决方案。您提供的方法是我正在寻找的,只是 field.value.getOrElse() 的 Java 版本是 field.valueOr。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多