码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-07-01 14:26:52      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:问题   span   track   mic   font   pop   port   div   spring注解   

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


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

标签:问题   span   track   mic   font   pop   port   div   spring注解   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!