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";
	}
	
	
}


原文地址:https://www.cnblogs.com/gavanwanggw/p/7102150.html