【问题标题】:associate form with Object将表单与对象关联
【发布时间】:2013-08-15 11:33:43
【问题描述】:

@ModelAttribute annotation(Spring) 允许 html 创建对象。

比如有一个类

class Vasya{
int id;
String name;
//set get 
}

和html表单

<form action='path'>
<input type='text' name = 'id'/>
<input type='text' name = 'name'/>
<input type='submit'/>
</form>

@controller方法:

@RequestMapping("/path")
    public String processSkill( @ModelAttribute Vasya vasya) {...}

在这里有效。

问题: 如何使用 *checkbox*es 为我的控制器方法工作的 id 和名称编写 html 表单?

【问题讨论】:

    标签: java html spring jsp jakarta-ee


    【解决方案1】:

    不明白如何使用复选框传递 id,但您可以通过这种方式传递复选框值:

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String form(@RequestParam(required = false) Integer check) {
        if(check != null) { // if checkbox is not selected it is null
            System.out.println(check);
        }
        return "your-view";
    }
    

    jsp:

    <form action="${home}/test" method="POST">
        <input type="checkbox" value="1" name="check" />
        <input type="submit" />
    </form>
    

    【讨论】:

    • 我知道这种方式。我想使用@ModelAttribute 注释
    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多