@RequestMapping请求路径映射,假设标注在某个controller的类级别上,则代表訪问此类路径下的方法都要加上其配置的路径。最常用是标注在方法上。代表哪一个详细的方法来接受处理某次请求。html
下面两种方式都可以从url中传參数,但是另一种方式的适用性更高一些,当參数中包括中文的时候,假设用第一种方式传參数,经常会出现參数还没到controller就已经通过编码了(好比:通过utf-8编码后,本来要传的參数就会以%+ab...cd这种方式出现),而后controller接受到这种请求后,根本没法解析该请求应该走那个业务方法。而后就会出现常见的404问题。java
。。web
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"; } }