您的位置:首页 > 编程语言 > Java开发

第二节:springmvc传值方式

2015-08-23 08:45 537 查看
示例代码:

student.jsp
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>

<h2>User Information</h2>
<form method="POST" action="/HelloWeb/addStudent">
name:<input type="text" name="name"/>
<input type="submit" value="click"/>
</form>

</body>
</html>
result.jsp
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Submitted User Information</h2>
<table>
<tr>
<td>Name</td>
<td>${name}</td>
</tr>
</table>
</body>
</html>
User.java
public class User {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
@Controller
public class UserController {
<span style="white-space:pre">	</span>@RequestMapping(value = "/user", method = RequestMethod.GET)
<span style="white-space:pre">	</span>public String toUser() {
<span style="white-space:pre">	</span>      return "user";
<span style="white-space:pre">	</span>}
   @RequestMapping(value = "/add1", method = RequestMethod.POST)
   public String addStudent(User user, 
   ModelMap model) {
      model.addAttribute("name", user.getName());
      return "result";
   }
   @RequestMapping(value = "/add2", method = RequestMethod.POST)
   public String addStudent(@RequestParam(value="name")String username,ModelMap model){
<span style="white-space:pre">	</span>   model.addAttribute("name", username);
<span style="white-space:pre">	</span>   return "result";
   }
   @RequestMapping(value = "/add3", method = RequestMethod.POST)
   public ModelAndView addStudent(String name){
<span style="white-space:pre">	</span>   ModelAndView model=new ModelAndView("result");
<span style="white-space:pre">	</span>   model.addObject("name", name);
<span style="white-space:pre">	</span>   return model;
   }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: