标签:首字母 ring key 通过 val cti type .property view
Spring MVC3在controller和视图之间传递参数的方法:
@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方法略 
则页面上可以用
<form name="form1" action="hello4" method="post">
     <input type="text" name="name"/>
     <input type="text" name="hobby"/>
...
提交后,把值直接绑定到user对象上。
<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>
标签:首字母 ring key 通过 val cti type .property view
原文地址:http://www.cnblogs.com/lovefendi/p/7777371.html