spring mvc获取路径参数的几种方式 - 浅夏的个人空间 - 开源中国社区

时间:2023-03-09 09:33:57
spring mvc获取路径参数的几种方式 - 浅夏的个人空间 - 开源中国社区

body
{
font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif;
font-size: 10.5pt;
line-height: 1.5;
}
html, body
{

}
h1 {
font-size:1.5em;
font-weight:bold;
}
h2 {
font-size:1.4em;
font-weight:bold;
}
h3 {
font-size:1.3em;
font-weight:bold;
}
h4 {
font-size:1.2em;
font-weight:bold;
}
h5 {
font-size:1.1em;
font-weight:bold;
}
h6 {
font-size:1.0em;
font-weight:bold;
}
img {
border:0;
max-width: 100%;
}
blockquote {
margin-top:0px;
margin-bottom:0px;
}
table {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
td {
border-collapse:collapse;
border:1px solid #bbbbbb;
}

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";  
        }