Url萬用字元映射

來源:互聯網
上載者:User

標籤:測試   system   文字框   xxxx   user   detail   ppi   技術分享   app   

原文:http://www.cnblogs.com/liukemng/p/3726897.ht

1.URL路徑映射

1.1.對一個action配置多個URL映射:

我們把上一篇中的HelloWorldController的index() action方法的@RequestMapping更改為@RequestMapping(value={"/index", "/hello"}, method = {RequestMethod.GET}),這表示對該action配置了/index和/hello兩個映射。運行測試,如下:

可以看到/helloworld/hello請求也成功匹配。

1.2.URL請求參數映射:

這在查詢的時候經常用到,比如我們根據id或編號來擷取某一條記錄。

在HelloWorldController添加一個getDetail的action,代碼如下:

@RequestMapping(value="/detail/{id}", method = {RequestMethod.GET})public ModelAndView getDetail(@PathVariable(value="id") Integer id){        ModelAndView modelAndView = new ModelAndView();      modelAndView.addObject("id", id);      modelAndView.setViewName("detail");      return modelAndView;}

其中value="/detail/{id}",中的{id}為預留位置表示可以映射請求為/detail/xxxx 的URL如:/detail/123等。

方法的參數@PathVariable(value="id") Integer id 用於將URL中預留位置所對應變數映射到參數id上,@PathVariable(value="id") 中value的值要和預留位置/{id}大括弧中的值一致。

在views中添加detail.jsp視圖,用於將擷取到的id值展示出來。視圖內容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>    ${id}</body></html>

運行測試,請求URL地址 http://localhost:8080/SpringMVCLesson/helloworld/detail/123 ,結果如下:

可以看到已經正確的顯示了我們請求的id。

1.3.URL萬用字元映射:

我們還可以通過萬用字元對URL映射進行配置,萬用字元有“?”和“*”兩個字元。其中“?”表示1個字元,“*”表示匹配多個字元,“**”表示匹配0個或多個路徑。

例如:

“/helloworld/index?”可以匹配“/helloworld/indexA”、“/helloworld/indexB”,但不能匹配“/helloworld/index”也不能匹配“/helloworld/indexAA”;

“/helloworld/index*”可以匹配“/helloworld/index”、“/helloworld/indexA”、“/helloworld/indexAA”但不能匹配“/helloworld/index/A”;

“/helloworld/index/*”可以匹配“/helloworld/index/”、“/helloworld/index/A”、“/helloworld/index/AA”、“/helloworld/index/AB”但不能匹配    “/helloworld/index”、“/helloworld/index/A/B”;

“/helloworld/index/**”可以匹配“/helloworld/index/”下的多有子路徑,比如:“/helloworld/index/A/B/C/D”;

如果現在有“/helloworld/index”和“/helloworld/*”,如果請求地址為“/helloworld/index”那麼將如何匹配?Spring MVC會按照最長相符優先原則(即和映射配置中哪個匹配的最多)來匹配,所以會匹配“/helloworld/index”,下面來做測試:

在HelloWorldController添加一個urlTest的action,內容如下:

@RequestMapping(value="/*", method = {RequestMethod.GET})public ModelAndView urlTest(){        ModelAndView modelAndView = new ModelAndView();       modelAndView.setViewName("urltest");      return modelAndView;}

在views檔案夾中新加一個視圖urltest.jsp,為了和index.jsp做區別urltest.jsp的內容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>    urlTest!</body></html>

請求http://localhost:8080/SpringMVCLesson/helloworld/index查看結果:

可以看出映射的是index對應的action。

請求http://localhost:8080/SpringMVCLesson/helloworld/AAA查看結果:

可以看出映射的是urlTest對應的action。

 

本人親測代碼:

@Controller@RequestMapping("/helloworld")public class HelloWorldContrloller {    //一個action匹配多個URL路徑    @RequestMapping(value={"/index","/hello"},method=RequestMethod.GET)    public String hello(String username,Model model){        System.out.println("//////////"+username);        model.addAttribute("hello111", "Hello:"+username);//username匹配jsp頁input文字框的name值        return "success";    }    //萬用字元    @RequestMapping(value="/detail/{id}",method=RequestMethod.GET)    public String getDetail(@PathVariable(value="id") Integer id,Model model){                model.addAttribute("id", id);        return "detail";            }}

 

Url萬用字元映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.