<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PawaStation 能猫环岛 &#187; java</title>
	<atom:link href="http://blog.pawa.ca/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pawa.ca</link>
	<description>An Apple a Day Keeps the Doctor Away</description>
	<lastBuildDate>Sun, 08 Mar 2009 04:39:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>无尽欢乐 JasperReport</title>
		<link>http://blog.pawa.ca/2008/01/06/jasperreport_tips/</link>
		<comments>http://blog.pawa.ca/2008/01/06/jasperreport_tips/#comments</comments>
		<pubDate>Sun, 06 Jan 2008 06:48:04 +0000</pubDate>
		<dc:creator>pawaca</dc:creator>
				<category><![CDATA[tips]]></category>
		<category><![CDATA[jasperreport]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.pawa.ca/?p=43</guid>
		<description><![CDATA[上个月很磕了一下 JasperReport，再次亲身印证了 80/20 法则。。。今天抽空列出几个“看上去”都用不了多少时间就可以搞定的问题。 空白 pdf 报表 我用的是 Spring 中的 JasperReportsPdfView 来帮助整合生成 pdf，这本来应该是一个非常简单的过程：Controller 返回一个标识 JasperReportsPdfView 的 ModelAndView，然后 Spring 自动生成报表。我最开始尝试的时候这样写的 Controller： 12345protected ModelAndView handleRequestInternal&#40;HttpServletRequest request, &#160; &#160; &#160; &#160; &#160; &#160; HttpServletResponse response&#41; throws Exception &#123; &#160; &#160; return new ModelAndView&#40;&#34;pdfReport&#34;&#41;; &#125; pdfReport 对应一个 JasperReportsPdfView，并配置指向了一个不需要任何动态数据的 report.jrxml 模板。 打开浏览器访问 controller ，会返回生成后的 pdf，不过内容是空的。。。改来改去，折腾了半天，最后发现原来 即使模板不需要任何动态数据，还是要传递一个 dummy 过去，才可以 render 出结果 [...]]]></description>
			<content:encoded><![CDATA[<p>上个月很磕了一下 JasperReport，再次亲身印证了 80/20 法则。。。今天抽空列出几个“看上去”都用不了多少时间就可以搞定的问题。</p>

<h3>空白 pdf 报表</h3>

<p>我用的是 Spring 中的 JasperReportsPdfView 来帮助整合生成 pdf，这本来应该是一个非常简单的过程：Controller 返回一个标识 JasperReportsPdfView 的 ModelAndView，然后 Spring 自动生成报表。我最开始尝试的时候这样写的 Controller：</p>

<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">protected</span> ModelAndView handleRequestInternal<span style="color: #009900;">&#40;</span>HttpServletRequest request,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pdfReport&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>pdfReport 对应一个 JasperReportsPdfView，并配置指向了一个不需要任何动态数据的 report.jrxml 模板。 打开浏览器访问 controller ，会返回生成后的 pdf，不过内容是空的。。。改来改去，折腾了半天，最后发现原来 <strong>即使模板不需要任何动态数据，还是要传递一个 dummy 过去，才可以 render 出结果</strong> 。</p>

<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">protected</span> ModelAndView handleRequestInternal<span style="color: #009900;">&#40;</span>HttpServletRequest request,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> model <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Spring 配置的默认 jasper datasource 在 map 中的名称是 'datasource'，这个名字可以配置。</span><br />
&nbsp; &nbsp; model.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;datasource&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> JRBeanCollectionDataSource<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pdfReport&quot;</span>,model<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<h3>文字消失</h3>

<p>开始在 dev 环境下生成好好的 pdf，放到 product 环境下变得无法完全显示所有文字。推测是嵌入字体问题，换来换去都不行。最后想起  product 环境是 windows，而 dev 环境是 linux，digg 了许久文档，终于找出原因。原来 JasperReport 在生成 pdf 的时候 text 的 layout 是以 fontName 这个属性为基准，用 Java 的 <span class="caps">AWT </span>里算出字的高度，而最终显示在 Adobe Reader 中的事根据 pdfFontName 这个属性。很显然， Linux 和 Windows 下面算出的文字高度不同。Linux下面可以显示，但是 Windows 下面高度就不够了，所以出现了缺字的现象。解决办法就是把 TextField 的高度加 4 px 左右。后来还想出一个方法是设置 stretch with overflow 那个属性，不过没有具体测试。</p>

<h3>isStretchWithOverflow 失灵</h3>

<p>用户输入的多行文字不能完全显示，只显示了在 iReport 里面设计时候的那部分，仿佛 isStretchWithOverflow 设置了跟没设置一样。又是昏天黑地的 digg，最后发现原因在客户端录入，Flex 的文本域 line separator 竟然是 \r 。Java 这边则是 \n，JasperReport 的文本 stretch 很可能是数文本有多少个 line separator 然后算出总高度，结果当然是算不出实际的行数。解决的办法是在把文本发到 JasperReport 之前，先 correct line separator。</p>

<h3>总结</h3>

<p>是福不是祸，是祸躲不过。上面的问题每一个都耗掉 2 hours+ 的时间来 debug，记在这里，希望可以造福后人。另外有一点比较奇怪的是，JasperReport 的论坛好像完全没有被 google index，出了问题一定要到 <a href="http://www.jasperforge.org/jaspersoft/opensource/business_intelligence/jasperreports/faq.html">这里</a> 和 <a href="http://www.jasperforge.org/index.php?option=com_joomlaboard&amp;Itemid=215&amp;func=showcat&amp;catid=8">这里</a> 才可以啊。囧</p>


<p>我佛慈悲。。。</p>]]></content:encoded>
			<wfw:commentRss>http://blog.pawa.ca/2008/01/06/jasperreport_tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AMF3 Integer/Number 与 GDS</title>
		<link>http://blog.pawa.ca/2007/11/24/amf3-integernumber-gds/</link>
		<comments>http://blog.pawa.ca/2007/11/24/amf3-integernumber-gds/#comments</comments>
		<pubDate>Sat, 24 Nov 2007 07:42:24 +0000</pubDate>
		<dc:creator>pawaca</dc:creator>
				<category><![CDATA[tips]]></category>
		<category><![CDATA[amf]]></category>
		<category><![CDATA[gds]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.pawa.ca/?p=49</guid>
		<description><![CDATA[AMF3 里面的 integer 类型使用 4 bytes 来传输，但是是以一种诡异的形式，摘 osflash 上的一段描述： Integer-data is probably the single most used item in AMF3. To save space it is an integer that can be 1-4 bytes long. The first bit of the first three bytes determine if the next byte is included (1) in this integer-data or not (0). The [...]]]></description>
			<content:encoded><![CDATA[<p><span class="caps">AMF3 </span>里面的 integer 类型使用 4 bytes 来传输，但是是以一种诡异的形式，摘 <a href="http://osflash.org/documentation/amf3">osflash</a> 上的一段描述：</p>

<blockquote><p>Integer-data is probably the single most used item in <span class="caps">AMF3.</span> To save space it is an integer that can be 1-4 bytes long. The first bit of the first three bytes determine if the next byte is included (1) in this integer-data or not (0). The last byte, if present, is read completely (8 bits). The first bits are then removed from the first three bytes and the remaining bits concatenated to form a big-endian integer. </p></blockquote>

<p>这里的重点是：为了省地方， amf3 的 integer 牺牲了部分范围，只能表示 -268435456(int.MIN_VALUE»3) 到 268435455(int.MAX_VALUE»3) 范围内的数。</p>

<p>这引出了一个问题，如果在 java 中一个 integer 的数值大于 amf3 可允许的范围传输的时候怎么办？ survey 了一些 implement 代码，发现这种时候会用范围更大的 8 bytes number 类型来传输。</p>

<p>[分隔线若干条...]</p>

<p>最近程序 (Flex + <a href="http://www.graniteds.org"><span class="caps">GDS</span></a> + Hibernate + 其他) 被 User 用出一个神秘的 exception： </p>

<blockquote><p>javax.persistence.PersistenceException: org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.Integer, got class java.lang.Double </p></blockquote>

<p>由于无法 reproduce，我就把他当成了灵异事件，没有理会。没想到这个不着调的 exception 竟然在昨天出现在朕的面前！经过 3.765 秒的分析和 4.132 秒的 survey 终于发现了问题的根源。（深吸一口气）原来是有一个 proxy entity 的 id 超过了 amf integer 可允许的最大范围，这个从 java 传到 flex 还没什么问题，但是回传会来的时候由于 <span class="caps">GDS </span>将 amf number 读成 double 所以造成 id 类型为 double，但实际上这个 id 被声明为 integer，所以在就出现了上面那个 exception （吐气&#8230;）</p>

<p>有趣的是，只有当 entity 是 hibernate proxy 的时候才会出现这个问题。看来是 <span class="caps">GDS </span>某个地方写的有问题，在代码里游啊游，发现了这段：</p>

<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// /graniteds/hibernate/org/granite/hibernate/HibernateProxyInstanciator.java</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> readId<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobjectinput+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ObjectInput</span></a> in<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aclassnotfoundexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ClassNotFoundException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Serializable</span></a><span style="color: #009900;">&#41;</span>in.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>上面的这个代码是从 svn 的 trunk 上粘下来的，看来这个问题还没改&#8230;（没被发现？）</p>

<p>总之，如果有谁不幸中招，可以试试 <a href="http://blog.pawa.ca/wp-content/uploads/2007/11/hibernate_patch.txt">apply 这个 patch</a> 到 /graniteds/hibernate/org/granite/hibernate/HibernateProxyInstanciator.java 。</p>

<p>Over</p>]]></content:encoded>
			<wfw:commentRss>http://blog.pawa.ca/2007/11/24/amf3-integernumber-gds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring MVC 中的中文编码问题小说一蛤</title>
		<link>http://blog.pawa.ca/2007/07/26/spring-mvc-encoding/</link>
		<comments>http://blog.pawa.ca/2007/07/26/spring-mvc-encoding/#comments</comments>
		<pubDate>Thu, 26 Jul 2007 04:17:02 +0000</pubDate>
		<dc:creator>pawaca</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.pawa.ca/?p=57</guid>
		<description><![CDATA[原来以为 Java 中文支持的会很好，没想到一用 Spring 就出乱码啦，看来人家默认情况下都不吊你第三世界国家的。。。 anyway 切入正题，我股沟了一下相关的内容，发现大多数解决方法都是加个 servlet filter 来转换编码。觉得这方法不好，好像虽然有用但是比较糊弄事，不彻底明白始终不爽。所以我就在源代码里游啊游，整理出大概的思路，如下： 页面显示中文 这部分以 jsp 为例， Spring MVC 在 resolve 页面显示的时候是这么一个流程。 InternalResourceViewResolver 利用 InternalResourceView 来 build view。 InternalResourceView 则会将最终页面生成的请求转交给对应的 jsp 来通过 container 实现。 如果页面显示有乱码，问题貌似会出在 InternalResourceView 上，实际上 InternalResourceViewResolver 的父类中有一个叫 UrlBasedViewResolver 的东东，里面有个属性叫做 contentType，并且会在 buid view 的时候最终把这个属性赋值给 InternalResourceView 。貌似找到了问题的原因。我试着这样设置并窃喜： 12345&#60;bean id=&#34;jspViewResolver&#34; &#160; &#160; &#160; &#160;class=&#34;org.springframework.web.servlet.view.InternalResourceViewResolver&#34;&#62; &#160; &#160; &#60;property name=&#34;contentType&#34; value=&#34;utf-8&#34;&#62;&#60;/property&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>原来以为 Java 中文支持的会很好，没想到一用 Spring 就出乱码啦，看来人家默认情况下都不吊你第三世界国家的。。。</p>

<p>anyway 切入正题，我股沟了一下相关的内容，发现大多数解决方法都是加个 servlet filter 来转换编码。觉得这方法不好，好像虽然有用但是比较糊弄事，不彻底明白始终不爽。所以我就在源代码里游啊游，整理出大概的思路，如下：</p>

<h3>页面显示中文</h3>

<p>这部分以 jsp 为例， Spring <span class="caps">MVC </span>在 resolve 页面显示的时候是这么一个流程。</p>

<p>InternalResourceViewResolver 利用 InternalResourceView 来 build view。 InternalResourceView 则会将最终页面生成的请求转交给对应的 jsp 来通过 container 实现。</p>

<p>如果页面显示有乱码，问题貌似会出在 InternalResourceView 上，实际上 InternalResourceViewResolver 的父类中有一个叫 UrlBasedViewResolver 的东东，里面有个属性叫做 contentType，并且会在 buid view 的时候最终把这个属性赋值给 InternalResourceView 。貌似找到了问题的原因。我试着这样设置并窃喜：</p>

<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jspViewResolver&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;contentType&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; ...<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>

<p>页面刷新后乱码依旧。。。回到源文件中，游啊游，发现其实 InternalResourceViewResolver 虽然有给 InternalResourceView 赋值 contentType，但是 InternalResourceView 忽视了这个属性。。。然后直接转交给对应的 jsp 处理。看来问题实际上出在 jsp 上。到对应的 jsp 页面，加上下面的代码：</p>

<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;%@ page <span style="color: #000066;">pageEncoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000066;">contentType</span>=<span style="color: #ff0000;">&quot;text/html;charset=UTF-8&quot;</span>%<span style="color: #000000; font-weight: bold;">&gt;</span></span></div></td></tr></tbody></table></div>

<p>然后在编辑器里确保源文件也存成 utf-8，然后刷新页面，好了。<br />
	<br />
h3. 提交中文参数<br />
	<br />
原以为这样就 OK 了，后来发现还没完。。。在表单提交的时候，中文依然乱码，sun。再游！ 发现要改的地方有两点，第一个是针对表单的 <span class="caps">POST</span>，因为默认的 request body 处理的编码不是 utf-8 所以在 decode body 的时候会解出乱码来。这需要写个Interceptor 来设置 request 的 encoding ，比如：</p>

<div class="codecolorer-container java5 mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="java5 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RequestEncodingInterceptor <span style="color: #000000; font-weight: bold;">implements</span> HandlerInterceptor<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; @<a href="http://java.sun.com/j2se/1%2E5%2E0/docs/api/java/lang/Override.html"><span style="color: #003399; font-weight: bold;">Override</span></a><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">boolean</span> preHandle<span style="color: #009900;">&#40;</span>HttpServletRequest request,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse response, <a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+Object"><span style="color: #003399; font-weight: bold;">Object</span></a> handler<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://java.sun.com/j2se/1%2E5%2E0/docs/api/java/lang/Exception.html"><span style="color: #003399; font-weight: bold;">Exception</span></a><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; request.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #006600; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; ...<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>然后挂在对应的 handlerMapping 上面，比如：</p>

<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;handlerMapping&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; class=&quot;org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping&quot;&gt;<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;interceptors&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;RequestEncodingInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; ...<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>

<p>另一方面，如果 url get 中有 encode 之后的中文参数，还需要确保 url decode 也用 utf-8 来解。由于 BeanNameUrlHandlerMapping 通过 UrlPathHelper 来处理 url，所以需要这样配置：</p>

<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;handlerMapping&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; ....<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;urlPathHelper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.util.UrlPathHelper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;urlDecode&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;defaultEncoding&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>

<h3>国际化 i18n</h3>

<p>国际化的配置文件如果用 java 的 properties 文件是不支持 utf-8 滴，要换成 Spring 的 ReloadableResourceBundleMessageSource ，酱紫配置：</p>

<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;messageSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; class=&quot;org.springframework.context.support.ReloadableResourceBundleMessageSource&quot;&gt;<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;basename&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;WEB-INF/i18n/messages&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;defaultEncoding&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>

<p>搞定！（貌似？）</p>]]></content:encoded>
			<wfw:commentRss>http://blog.pawa.ca/2007/07/26/spring-mvc-encoding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
