如何将 java 对象从 jsp 发送到 Spring MVC 3 控制器
How to send java object from jsp to Spring MVC 3 Controller
大家好我是Spring环境的新手。
我在 jsp 页面中这样做。
<form action="add">
Name : <input type="text" name="name"/>
Contact : <input type="text" name="contact"/>
Age : <input type="text" name="age"/>
<input type="submit" name="Submit"/> </form>
我不想将此数据作为 Employee 对象发送到控制器方法中
作为单独的字段。
我正在控制器中做类似的事情
@RequestMapping(value="/add" Method=RequestMethod,POST)
public String addEmployee(@ModelAttribute("employee") Employee
employee, Model model) {
...
...
}
我认为知道如何在控制器方法中处理对象但不知道
如何将对象从 jsp 发送到控制器。
请指导。
试试这个:
首先将您的代码更改为以下,
@RequestMapping(value="/add", method=RequestMethod.POST)
public String addEmployee(@ModelAttribute("employee") Employee
employee, Model model) {
//instead of Method=RequestMethod.POST your code were
//Method=RequestMethod,POST
..
}
在那之后,假设你有一个员工 class 带有 setter 和 getter,那么你表单输入标签中的名称属性必须与你的员工 class 的 属性(归档)名称。
如果您执行上述步骤,Spring 将为您执行隐式数据绑定,您可以使用 @ModelAttribute 注释
大家好我是Spring环境的新手。
我在 jsp 页面中这样做。
<form action="add">
Name : <input type="text" name="name"/>
Contact : <input type="text" name="contact"/>
Age : <input type="text" name="age"/>
<input type="submit" name="Submit"/> </form>
我不想将此数据作为 Employee 对象发送到控制器方法中 作为单独的字段。
我正在控制器中做类似的事情
@RequestMapping(value="/add" Method=RequestMethod,POST)
public String addEmployee(@ModelAttribute("employee") Employee
employee, Model model) {
...
...
}
我认为知道如何在控制器方法中处理对象但不知道 如何将对象从 jsp 发送到控制器。 请指导。
试试这个: 首先将您的代码更改为以下,
@RequestMapping(value="/add", method=RequestMethod.POST)
public String addEmployee(@ModelAttribute("employee") Employee
employee, Model model) {
//instead of Method=RequestMethod.POST your code were
//Method=RequestMethod,POST
..
}
在那之后,假设你有一个员工 class 带有 setter 和 getter,那么你表单输入标签中的名称属性必须与你的员工 class 的 属性(归档)名称。
如果您执行上述步骤,Spring 将为您执行隐式数据绑定,您可以使用 @ModelAttribute 注释