spring mvc獲取路徑參數的幾種方式 - 淺夏的個人空間 - 開源中國社區


SpringMVC是一個基於DispatcherServlet的MVC框架,每一個請求最先訪問的都是 DispatcherServlet,DispatcherServlet負責轉發每一個Request請求給相應的Handler,Handler處理 以后再返回相應的視圖(View)和模型(Model),返回的視圖和模型都可以不指定,即可以只返回Model或只返回View或都不返回。在使用注解 的SpringMVC中,處理器Handler是基於@Controller和@RequestMapping這兩個注解的,@Controller聲明 一個處理器類,@RequestMapping聲明對應請求的映射關系,這樣就可以提供一個非常靈活的匹配和處理方式。

 

DispatcherServlet是繼承自HttpServlet的,既然SpringMVC是基於DispatcherServlet的,那么 我們先來配置一下DispatcherServlet,好讓它能夠管理我們希望它管理的內容。HttpServlet是在web.xml文件中聲明的。


一、從視圖向controller傳遞值,  controller <--- 視圖

1、通過@PathVariabl注解獲取路徑中傳遞參數 

?
1
2
3
4
5
6
7
1      @RequestMapping (value =  "/{id}/{str}"
2      public  ModelAndView helloWorld( @PathVariable  String id, 
3              @PathVariable  String str) { 
4          System.out.println(id); 
5          System.out.println(str); 
6          return  new  ModelAndView( "/helloWorld" ); 
7      }

2、
 
1)簡單類型,如int, String, 應在變量名前加@RequestParam注解,
例如:

?
1
2
3
4
5
6
7
        @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)對象類型:

?
1
2
3
4
5
6
        @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請求參數名=  命令/表單對象的屬性名”
    的規則自動綁定請求數據,支持“級聯屬性名”,自動進行基本類型數據轉換。
此外,還可以限定提交方法為POST,即修改方法的@RequestMapping注解為
@RequestMapping(value="/hello4",method=RequestMethod.POST)
 
最后,注意,如果這里提交過來的字符出現亂碼,應該在web.xml里加入如下filter:
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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>



返回數據到頁面幾種方式:      

  1. ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
         //返回頁面參數的第二種方式,在形參中放入一個Model  
         @RequestMapping (value =  "/hello2.htm" )  
         public  String hello2( int  id,Model model){  
             System.out.println( "hello2 action:" +id);  
             model.addAttribute( "name" "huangjie" );  
             //這個只有值沒有鍵的情況下,使用Object的類型作為key,String-->string  
             model.addAttribute( "ok" );  
             return  "hello" ;  
         }
  1. ?
    1
    2
    3
    4
    5
    6
    7
        //返回頁面參數的第一種方式,在形參中放入一個map  
         @RequestMapping (value =  "/hello1.htm" )  
         public  String hello( int  id,Map<String,Object> map){  
             System.out.println( "hello1 action:" +id);  
             map.put( "name" "huangjie" );  
             return  "hello" ;  
         }






免責聲明!

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



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