Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 播放框架';s输入文本值始终通过0_Java_Playframework_Playframework 2.0_Playframework 2.3 - Fatal编程技术网

Java 播放框架';s输入文本值始终通过0

Java 播放框架';s输入文本值始终通过0,java,playframework,playframework-2.0,playframework-2.3,Java,Playframework,Playframework 2.0,Playframework 2.3,几天来,我一直在研究一种游戏形式,我非常沮丧。我阅读了文档并使用了“玩Java”一书,创建了一个userWeights模型类: public class UserWeights { public HashMap<ServicesWeights, Float> userWeights = new HashMap<>(); @Constraints.Required public String user; @Co

几天来,我一直在研究一种游戏形式,我非常沮丧。我阅读了文档并使用了“玩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 HashMap userWeights=new HashMap();
@约束条件。必需
公共字符串用户;
@约束条件。必需
公共字符串密码;
公共int样本化;
公共int抽样错误;
} 
我的控制器:

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.Form(UserWeights.class);
公共静态结果索引(){
返回ok(index.render(“新应用程序准备就绪”);
}
公共静态结果查找器(){
返回ok(finder.render(userWeightsForm));
}
公共静态结果runWithUserInput(){
Form boundForm=userWeightsForm.bindFromRequest();
如果(boundForm.hasErrors()){
返回错误请求(index.render(“失败”);
}
UserWeights=boundForm.get();
if(weights.checkboxChoices.get(0)!=null&&weights.checkboxChoices.get(0).等于(“1”)){
runMethodA();
}else if(weights.checkboxChoices.get(1)!=null&&weights.checkboxChoices.get(1).equals(“2”)){
runMethodB();
} 
返回待办事项;
}
以及以下观点:

@(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>
@(UserWeightsForm:Form[UserWeights])
@导入帮助程序。\u
@导入helper.twitterBootstrap.\u
@主(“查找器”){
请填写必填字段
@helper.form(action=routes.Application.runWithUserInput(),'enctype->“多部分/表单数据”){
发现者
@helper.inputText(UserWeightsForm(“用户”),“\u标签->用户”)
@helper.inputPassword(UserWeightsForm(“密码”),“\u标签->密码”)

输入类型:
一般样品
抽样错误
我想要的-如果选中第一个复选框,用户将填写
sampleSize
文本输入字段,该值将绑定到表单,而
misSampleSize
字段将为空/zero/无论什么(在这种情况下,我不会使用它)。反之亦然

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

我尝试将
输入
标记中的
值设置为null,将其完全删除,并使用模板帮助器(我更喜欢避免它,因为它没有那么灵活)。我不明白为什么我在文本字段中输入的数字被忽略,我的程序认为它是0

因此问题1:如何停止填充0值

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


谢谢!

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

在Java中,输入名称应该与表单字段名称匹配。因此它应该是
name=“sampleSize”
感谢您的回答,但它不起作用。它不会用getOrElse()编译,当我像你建议的那样只输入字段名时,我得到一个运行时异常-[IllegalStateException:No value]。嗯,不知道。除了getOrElse,它应该可以工作。你检查过区分大小写的打字错误吗?两个建议:看看。也许生成一个文本字段并转换它?看看生成的html(inspect element)查看它是否能让您了解问题的症结所在。另请参阅以下问题。找到了解决方案。您提供的方法正是我想要的,只是field.value.getOrElse()的Java版本是field.valueOr。非常感谢您的帮助。