Spring MVC3在controller和視圖之間傳遞參數的方法:web
@RequestMapping(value={"/","/hello"}) public String hello(int id,Map<String,Object> map) { System.out.println(id); System.out.println("hello"); map.put("hello", "world"); return "hello"; }
@RequestMapping(value="/say") public String say(@RequestParam int id,Model model) { System.out.println("say"); model.addAttribute("hello", "value"); //使用Object的類型做爲key,String-->string model.addAttribute("ok"); return "hello"; }
@RequestMapping("hello3") public String hello3( @RequestParam("name" ) String name, @RequestParam("hobby" ) String hobby){ System. out.println("name=" +name); System. out.println("hobby=" +hobby); return "hello" ; }
@RequestMapping("/hello4" ) public String hello4(User user){ System.out.println("user.getName()=" +user.getName()); System.out.println("user.getHobby()=" +user.getHobby()); return "hello"; }
public class User { private String name ; private String hobby ; public User(){ } public User(String name, String hobby) { this.name = name; this.hobby = hobby; } //...get/set方法略
則頁面上能夠用spring
<form name="form1" action="hello4" method="post"> <input type="text" name="name"/> <input type="text" name="hobby"/> ...
提交後,把值直接綁定到user對象上。app
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name > <url-pattern>/*</url-pattern> </filter-mapping>
內容轉載自:http://blog.csdn.net/yanqlv/article/details/7655645post