Java 将形式与对象关联

Java 将形式与对象关联,java,html,spring,jsp,jakarta-ee,Java,Html,Spring,Jsp,Jakarta Ee,@modeldattribute注释(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>

@modeldattribute
注释(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>
它在这里工作

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

不了解如何使用checkbox传递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:


我知道这条路。我想使用@modeldattribute注释
@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";
}
<form action="${home}/test" method="POST">
    <input type="checkbox" value="1" name="check" />
    <input type="submit" />
</form>