<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/filter-locale-httpservletrequest/</link>
<title><![CDATA[如何在Filter中改变request的locale设置]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Mon, 20 Sep 2010 18:22:33 +0000</pubDate> 
<guid>https://www.heckjj.com/filter-locale-httpservletrequest/</guid> 
<description>
<![CDATA[ 
	<span style="font-family: 微软雅黑;">我们在HttpServletRequest中我们可以通过getLocale()得到用户请求的locale，但是如果你希望手动改变用户的locale设置的话却没有setLocale方法，那么有什么办法来改变request的locale设置吗呢？<br/><br/>答案是可以写一个HttpServletRequestWrapper，通过其中的getLocale()方法和getLocales()方法来改变用户请求的locale设置。首先我们来写一个这样的类：</span><br/><textarea name="code" class="java" rows="15" cols="100">
public class LocaleRequestWrapper extends 
/**
* http://www.hecks.tk/filter-locale-httpservletrequest/
*/
HttpServletRequestWrapper &#123;

&nbsp;&nbsp;&nbsp;&nbsp;public LocaleRequestWrapper(HttpServletRequest req) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(req);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;public Enumeration getLocales() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Vector v = new Vector(1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v.add(getLocale());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return v.elements();
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;public Locale getLocale() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String localeStr = getRequest().getParameter("locale");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Locale locale =&nbsp;&nbsp;new Locale(localeStr);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return locale;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;
</textarea><br/><span style="font-family: 微软雅黑;">我们可以看到在getLocale()方法中我们得到request参数中的locale的值（想想用户请求的地址是http://www.hecks.tk?locale=zh_CN）。之后我们需要写一个filter，并使用LocaleRequestWrapper：</span><br/><textarea name="code" class="java" rows="15" cols="100">
/**
 * http://www.hecks.tk/filter-locale-httpservletrequest/
 */
public class LocaleFilter implements Filter &#123;

&nbsp;&nbsp;
&nbsp;&nbsp;public void doFilter(ServletRequest request, ServletResponse response,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FilterChain chain) throws IOException, ServletException &#123;
&nbsp;&nbsp;&nbsp;&nbsp; if (request instanceof HttpServletRequest && request.getParameter("locale") != null) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletRequest req = (HttpServletRequest)request;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LocaleRequestWrapper wrapper =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new LocaleRequestWrapper(req);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chain.doFilter(wrapper, response);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chain.doFilter(request, response);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;

&#125;
</textarea><br/><span style="font-family: 微软雅黑;">可以看到，在其中的doFilter()方法中，如果请求的参数中含有locale的值，那么我们就会使用这个刚才设定的LocaleRequestWrapper。<br/><br/>写好Java代码之后，为了让我们的Filter能够生效，还需要在web.xml中配置对应的filter和filter-mapping：</span><br/><textarea name="code" class="xml" rows="15" cols="100">
<?xml version = "1.0"?>
<web-app>

<!-- Define the filters within the Web Application 
http://www.hecks.tk/filter-locale-httpservletrequest/
-->

&nbsp;&nbsp;<filter>
&nbsp;&nbsp;&nbsp;&nbsp;<filter-name>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ondev.net locale filter
&nbsp;&nbsp;&nbsp;&nbsp;</filter-name>
&nbsp;&nbsp;&nbsp;&nbsp;<filter-class>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;net.ondev.demo.LocaleFilter
&nbsp;&nbsp;&nbsp;&nbsp;</filter-class>
&nbsp;&nbsp;</filter>

&nbsp;&nbsp;<!-- Map the filter to a Servlet or URL -->
&nbsp;&nbsp;<filter-mapping>
&nbsp;&nbsp;&nbsp;&nbsp;<filter-name>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ondev.net locale filter
&nbsp;&nbsp;&nbsp;&nbsp;</filter-name>
&nbsp;&nbsp;&nbsp;&nbsp;<url-pattern>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*
&nbsp;&nbsp;&nbsp;&nbsp;</url-pattern>
&nbsp;&nbsp;</filter-mapping>
</web-app></textarea><br/>Tags - <a href="https://www.heckjj.com/tags/filter/" rel="tag">filter</a> , <a href="https://www.heckjj.com/tags/locale/" rel="tag">locale</a> , <a href="https://www.heckjj.com/tags/httpservletrequest/" rel="tag">httpservletrequest</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/filter-locale-httpservletrequest/#blogcomment</link>
<title><![CDATA[[评论] 如何在Filter中改变request的locale设置]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/filter-locale-httpservletrequest/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>