如何将java对象从jsp发送到Spring MVC 3 Controller

时间:2017-04-20 06:25:19

标签: java jsp spring-mvc controllers

Hello All我是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发送对象到控制器。  请指导。

1 个答案:

答案 0 :(得分:1)

试试这个: 首先将代码更改为以下,

@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
 .. 
 }

之后,如果您有一个包含setter和getter的Employe类,那么表单输入标记中的name属性必须与您的employee类的属性(已归档)名称具有相同的名称。

如果您将执行上述步骤,Spring将为您使用@ModelAttribute注释可以使用隐式DataBinding