首页 > 学院 > 开发设计 > 正文

SpringMVC之Ambiguous mapping(模棱两可的映射)

2019-11-08 02:14:25
字体:
来源:转载
供稿:网友

1、web.xml和核心配置文件的编辑见上篇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"})//调用时报错,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"})	//启动时报错,Ambiguous mapping,模棱两可的映射//	public ModelAndView indexAmbiguous02(){//		System.out.println("Hello Index!");//		ModelAndView mav = new ModelAndView();//		mav.setViewName("index");///WEB-INF/index.jsp//		return mav;//	}}3、注意如果方法(不一定在同一个类)的RequestMapping路径中存在相同的path值(默认访问方法为get,比如index()和indexAmbiguous02()),启动时会报Ambiguous Mapping异常;也会有调用时才报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 {	//启动没问题,	//testMethod01和testMethod02方法共存时,则:执行getMethod方法,根据方法类型选择执行的方法	//testMethod03方法与其他两个方法共存时,则:执行getMethod方法,根据请求类型报错偷笑	//(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;	}}

总结:出现Ambiguous Mapping异常时,找到同一请求路径映射到两个方法的地方,修改即可~


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表