由于SEOer要求,一般我们的参数都是写在URI里面的,而不是用参数的形式传递的。
如:http://search.aiyibang.com/search?key=内衣
转换为:
http://search.aiyibang.com/search/内衣
但是
- package com.aiyibang.search.controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.servlet.ModelAndView;
- public class SearchController {
- @RequestMapping(value = "/search/{keyword}", method = { RequestMethod.GET, RequestMethod.POST })
- public ModelAndView customerLook(@PathVariable String keyword) {
- ModelAndView mav=new ModelAndView();
- return mav;
- }
- }
package com.aiyibang.search.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
public class SearchController {
@RequestMapping(value = "/search/{keyword}", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView customerLook(@PathVariable String keyword) {
ModelAndView mav=new ModelAndView();
return mav;
}
}
发现 是无法找到这个请求的,也就是404,后然网上搜索,发现是tomcat 默认URI不支持中文的缘故。
解决方案:
到 TOMCAT/conf下找到server.xml,添加URIEncoding="UTF-8"进行URL编码设置就可以解决中文问题。
另外我们经常遇到路径中有点".",而点是特殊字符,比如.html, .do等等,所以Spring MVC默认是把点后面的信息当作文件后缀,这时候我们就要修改这个默认值。
- <bean
- class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
- <property name="interceptors" ref="localeChangeInterceptor" />
- <property name="useDefaultSuffixPattern" value="false" />
- </bean>
另外,这时候如果只设置这个,请求可以传递到对于的controller,但传过去的数据会有问题,只会传最后一个点前面的数据,除非你在最后加上 “/”,比如/search/内衣.服装/ 这样就会把“内衣.服装”当作整体,不然只会得到“内衣”。这时候我们可以这样设置@RequestMapping("/search/{keyword:.*}")