关于springMVC的请求参数如何处理(@RequestMapping 来映射请求参数)

用 @RequestMapping来解析http请求中的参数。

1   通过浏览器发送一个带参数的get请求来访问服务器中的服务,

http://localhost:8080/springmvc/RequestParam/testRequestParam2?username=xiaoming&age=11

url描述了一个get请求 域名为springmvc  ,后端是一个控制器的映射RequestParam,testRequestParam2是发布的服务名的映射,问号后面对应的请求参数和参数值

 

2   服务端的接口

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/RequestParam")
public class RequestParamsService {

    public final static String SUCCEEDD="show";
    
    /**    RequestParam
     *            映射的请求参数  可以是多个参数
     *     required  
     *            表示参数是否是必须的 默认是true(表示是必须的)       
     *     defaultValue  
     *                    请求参数的默认值
     *    
     * @param username
     * @param age
     * @return
     */
    @RequestMapping(value="/testRequestParam1",method=RequestMethod.GET)
    public String testRequestParam1(@RequestParam(value="username") String username , @RequestParam(value="age",required=false,defaultValue="0") int age){
        System.out.println("testRequestParam1   username="+username +" ,age="+age);
        return SUCCEEDD;
    }
    /**
     *   url :
     * @param username
     * @param age
     * @return
     */
    @RequestMapping(value="/testRequestParam2",method=RequestMethod.GET)
    public String testRequestParam2(@RequestParam(value="username") String username , @RequestParam(value="age",required=false) Integer age){
        System.out.println("testRequestParam2   username="+username +" ,age="+age);
        return SUCCEEDD;
    }
    
    
    
}

3   通过浏览器进行测试

posted @ 2016-06-18 09:38  德玛西亚宝宝  阅读(641)  评论(0编辑  收藏  举报