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

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;

@Controller
public 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异常时,找到同一请求路径映射到两个方法的地方,修改即可~



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值