Spring mvc 3 在controller和視圖之間傳遞參數


Spring MVC3在controller和視圖之間傳遞參數的方法:
 
一, 從controller往視圖傳遞值,controller---->視圖
 
1)簡單類型,如int, String,直接寫在controller方法的參數里,是無法傳遞到視圖頁面上的(經測試)。
 
(而用@RequestParam("name")注解,可以從視圖上,或地址中加?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";
     } www.2cto.com
 
二,從視圖向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類,如下
package model;
 
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對象上。
 
此外,還可以限定提交方法為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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM