Spring注解@RequestMapping请求路径映射问题

@RequestMapping请求路径映射,如果标注在某个controller的类级别上,则表明访问此类路径下的方法都要加上其配置的路径;最常用是标注在方法上,表明哪个具体的方法来接受处理某次请求。

以下两种方式都可以从url中传参数,但是第二种方式的适用性更高一些,当参数中包含中文的时候,如果用第一种方式传参数,经常会出现参数还没到controller就已经经过编码了(例如:经过utf-8编码后,原本要传的参数就会以%+ab...cd这样的方式出现),然后controller接受到这样的请求后,根本无法解析该请求应该走那个业务方法。然后就会出现常见的404问题。。。

package com.test.jeofey.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/path")
public class TestController {

	// 第一种传参数的方式 访问地址例如:http:域名/path/method1/keyWord.html
	@RequestMapping("method1/{keyWord}")
	public String getZhiShiDetailData(@PathVariable("keyWord") String keyWord,
			HttpServletRequest request, HttpServletResponse response){
		System.out.println(keyWord);
		return "v1/detail";
	}
	
	// 第二种传参数的方式 访问地址例如:http:域名/path/method2.html?key=keyWord
	@RequestMapping("method2")
	public String getCommonData(HttpServletRequest request, 
			HttpServletResponse response){
		String keyWord= request.getParameter("key");
		System.out.println(keyWord);
		return "v1/common";
	}
	
	
}


posted @ 2015-08-21 13:54  小鲜肉成长记  阅读(3031)  评论(1编辑  收藏  举报