[轉]SpringMVC Controller&View數據傳遞

Spring MVC3在controller和視圖之間傳遞參數的方法:web

 
一, 從controller往視圖傳遞值, controller---->視圖
 
1)簡單類型,如int, String,直接寫在controller方法的參數裏,是沒法傳遞到視圖頁面上的(經測試)。
 
(而用@RequestParam("name")註解,能夠從視圖上,經過url的方式?name=***傳遞到controller方法裏)
 
2)能夠用Map<String, Object>,其鍵值能夠在頁面上用EL表達式${鍵值名}獲得,
 
3)也能夠用Model類對象來傳遞,有addAttribute(key, value)方法,其鍵值能夠在頁面上用EL表達式${鍵值名}獲得,
 
若是用addAttribute(value)這個方法,會將類型名的首字母改爲小寫後,做爲鍵值名傳遞過去,例如"ok"在頁面上用${string}獲得,而一個複合類對象,如User類對象,頁面上用${user}獲得該對象,用${user.propertyName}獲得其屬性,這是用Model的一大優點。
例如,model.addAttribute( new User("my姓名","個人愛好有游泳打球"));
這樣頁面上就能用${user.name}和${user.hobby}打印對應屬性
@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";
     }
 
二,從視圖向controller傳遞值,  controller <--- 視圖
 
1)簡單類型,如int, String, 應在變量名前加@RequestParam註解,
例如:
@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" ;
      }
但這樣就要求輸入裏面必須有這兩個參數了,能夠用required=false來取消,例如:
@RequestParam(value="name",required= false) String name
但經測試也能夠徹底不寫這些註解,即方法的參數寫String name,效果與上面相同。
 
2)對象類型:
@RequestMapping("/hello4" )
       public String hello4(User user){
            System.out.println("user.getName()=" +user.getName());
            System.out.println("user.getHobby()=" +user.getHobby());
            return "hello";
      }
Spring MVC會按: 「HTTP請求參數名 =  命令/表單對象的屬性名」的規則自動綁定請求數據,支持「級聯屬性名」,自動進行基本類型數據轉換。
即有一個User類,以下
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

 

此外,還能夠限定提交方法爲POST,即修改方法的@RequestMapping註解爲
@RequestMapping(value="/hello4",method=RequestMethod. POST)
最後,注意,若是這裏提交過來的字符出現亂碼,應該在web.xml里加入以下filter:
<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

相關文章
相關標籤/搜索