關於springMVC的請求參數如何處理(@RequestMapping 來映射請求參數)

來源:互聯網
上載者:User

標籤:

用 @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   通過瀏覽器進行測試

關於springMVC的請求參數如何處理(@RequestMapping 來映射請求參數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.