1、web.xml和核心配置文件的編輯見(jiàn)上篇http://blog.csdn.net/u010101142/article/details/55005330;
2、還是HelloController類:
package com.slz.hello.controller;import org.sPRingframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/hello")public class HelloController { @RequestMapping(path={"/hello","/slz.html","/*/benboba.php","/**/baboben.aspx","/ab*.htm"}) public ModelAndView hello(){ System.out.println("Hello World!"); ModelAndView mav = new ModelAndView(); mav.setViewName("hello");///WEB-INF/hello.jsp return mav; } @RequestMapping(path={"index"}) public ModelAndView index(){ System.out.println("Hello Index!"); ModelAndView mav = new ModelAndView(); mav.setViewName("index");///WEB-INF/index.jsp return mav; } @RequestMapping(path={"index","indexAmbiguous"})//調(diào)用時(shí)報(bào)錯(cuò),Ambiguous mapping,模棱兩可的映射 public ModelAndView indexAmbiguous(){ System.out.println("Hello Index!"); ModelAndView mav = new ModelAndView(); mav.setViewName("index");///WEB-INF/index.jsp return mav; } // @RequestMapping(path={"index"}) //啟動(dòng)時(shí)報(bào)錯(cuò),Ambiguous mapping,模棱兩可的映射// public ModelAndView indexAmbiguous02(){// System.out.println("Hello Index!");// ModelAndView mav = new ModelAndView();// mav.setViewName("index");///WEB-INF/index.jsp// return mav;// }}3、注意如果方法(不一定在同一個(gè)類)的RequestMapping路徑中存在相同的path值(默認(rèn)訪問(wèn)方法為get,比如index()和indexAmbiguous02()),啟動(dòng)時(shí)會(huì)報(bào)Ambiguous Mapping異常;也會(huì)有調(diào)用時(shí)才報(bào)Ambiguous Mapping異常,比如index()和indexAmbiguous();4、新建MethodAmbiguousController:
package com.slz.hello.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class MethodAmbiguousController { //啟動(dòng)沒(méi)問(wèn)題, //testMethod01和testMethod02方法共存時(shí),則:執(zhí)行g(shù)etMethod方法,根據(jù)方法類型選擇執(zhí)行的方法 //testMethod03方法與其他兩個(gè)方法共存時(shí),則:執(zhí)行g(shù)etMethod方法,根據(jù)請(qǐng)求類型報(bào)錯(cuò)//(Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/mvc/getMethodAmbiguous') @RequestMapping(path="getMethodAmbiguous",method=RequestMethod.GET) public ModelAndView testMethod01(){ System.out.println("getMethodAmbiguous"); return null; } @RequestMapping(path="getMethodAmbiguous",method=RequestMethod.POST) public ModelAndView testMethod02(){ System.out.println("getMethodAmbiguous"); return null; } @RequestMapping(path="getMethodAmbiguous",method={RequestMethod.GET,RequestMethod.POST}) public ModelAndView testMethod03(){ System.out.println("getMethodAmbiguous"); return null; }}
總結(jié):出現(xiàn)Ambiguous Mapping異常時(shí),找到同一請(qǐng)求路徑映射到兩個(gè)方法的地方,修改即可~
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注