spring mvc中文uri问题解决

由于SEOer要求,一般我们的参数都是写在URI里面的,而不是用参数的形式传递的。

如:http://search.aiyibang.com/search?key=内衣

转换为:

http://search.aiyibang.com/search/内衣

 

但是

Java代码 复制代码  收藏代码
  1. package com.aiyibang.search.controller;  
  2.   
  3. import org.springframework.web.bind.annotation.PathVariable;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestMethod;  
  6. import org.springframework.web.servlet.ModelAndView;  
  7.   
  8. public class SearchController {  
  9.   
  10.     @RequestMapping(value = "/search/{keyword}", method = { RequestMethod.GET, RequestMethod.POST })  
  11.     public ModelAndView customerLook(@PathVariable String keyword) {  
  12.         ModelAndView mav=new ModelAndView();  
  13.           
  14.         return mav;  
  15.     }  
  16. }  
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默认是把点后面的信息当作文件后缀,这时候我们就要修改这个默认值。

 

Xml代码 复制代码  收藏代码
  1. <bean  
  2.     class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
  3.     <property name="interceptors" ref="localeChangeInterceptor" />  
  4.     <property name="useDefaultSuffixPattern" value="false" />  
  5. </bean>   

       另外,这时候如果只设置这个,请求可以传递到对于的controller,但传过去的数据会有问题,只会传最后一个点前面的数据,除非你在最后加上 “/”,比如/search/内衣.服装/  这样就会把“内衣.服装”当作整体,不然只会得到“内衣”。这时候我们可以这样设置@RequestMapping("/search/{keyword:.*}")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值