﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Rory's Blog-随笔分类-Spring</title><link>http://www.blogjava.net/rory/category/9611.html</link><description>Happy study,Happy work,Happy life</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 12:36:04 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 12:36:04 GMT</pubDate><ttl>60</ttl><item><title>SpringXmlrpcServiceExporter for xmlrpc3.x</title><link>http://www.blogjava.net/rory/archive/2006/10/22/spring_xmlrpc3_service_exporter.html</link><dc:creator>莫多</dc:creator><author>莫多</author><pubDate>Sun, 22 Oct 2006 08:12:00 GMT</pubDate><guid>http://www.blogjava.net/rory/archive/2006/10/22/spring_xmlrpc3_service_exporter.html</guid><wfw:comment>http://www.blogjava.net/rory/comments/76628.html</wfw:comment><comments>http://www.blogjava.net/rory/archive/2006/10/22/spring_xmlrpc3_service_exporter.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/rory/comments/commentRss/76628.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rory/services/trackbacks/76628.html</trackback:ping><description><![CDATA[<p>      我们的项目用到了xmlrpc,不过还是用的2.x版本的。由于xmlrpc3.x地推出。提供了NULL,Serializable等的支持，将原来的Hashtable改成了Map,Vector改成了List。都是不错的进步。所以我们决定从xmlrpc2.x升级到xmlrpc3.x.<br />      在spring里面有几个ServiceExporter，org.springframework.remoting.rmi.RmiServiceExporter、org.springframework.remoting.caucho.HessianServiceExporter、org.springframework.remoting.caucho.BurlapServiceExporter。不过没有xmlrpc的serviceExporter，原来我们是自己封装的XmlRpcServer,用servlet提供服务。(eg:http://localhost:8080/community/service/xmlrpc)没有和spring集成虽然用了spring。<br />    考虑到spring的便利以及配置的同意我决定将xmlrpcService放入spring中。xmlrpc3.x和xmlrpc2.x的代码基本上没有一样的。改了很多东西。除了类型变化之外，还添加了对异常的支持。详细信息请参照xmlrpc3.x源代码。<br />XmlRpcServiceExporter.java</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">package</span>
				<span style="COLOR: #000000"> com.jdkcn.xmlrpc;<br /><br /></span>
				<span style="COLOR: #0000ff">import</span>
				<span style="COLOR: #000000"> javax.servlet.ServletException;<br /><br /></span>
				<span style="COLOR: #008000">/**</span>
				<span style="COLOR: #008000">
						<br /> * </span>
				<span style="COLOR: #808080">@author</span>
				<span style="COLOR: #008000"> &lt;a href="mailto:rory.cn@gmail.com"&gt;somebody&lt;/a&gt;<br /> * </span>
				<span style="COLOR: #808080">@since</span>
				<span style="COLOR: #008000"> 2006-9-27 03:59:22 pm<br /> * </span>
				<span style="COLOR: #808080">@version</span>
				<span style="COLOR: #008000"> $Id XmlRpcServiceExporter.java$<br /> </span>
				<span style="COLOR: #008000">*/</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">class</span>
				<span style="COLOR: #000000"> XmlRpcServiceExporter </span>
				<span style="COLOR: #0000ff">extends</span>
				<span style="COLOR: #000000"> RemoteExporter </span>
				<span style="COLOR: #0000ff">implements</span>
				<span style="COLOR: #000000">
						<br />        Controller, InitializingBean {<br />    <br />    </span>
				<span style="COLOR: #0000ff">private</span>
				<span style="COLOR: #000000"> XmlRpcServletServer server;<br />    <br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> String serviceName;<br />    <br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> Resource configFile;<br />    <br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> Boolean enabledForExtensions;<br />    <br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> setEnabledForExtensions(Boolean enabledForExtensions) {<br />        </span>
				<span style="COLOR: #0000ff">this</span>
				<span style="COLOR: #000000">.enabledForExtensions </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> enabledForExtensions;<br />    }<br /><br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> setConfigFile(Resource configFile) {<br />        </span>
				<span style="COLOR: #0000ff">this</span>
				<span style="COLOR: #000000">.configFile </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> configFile;<br />    }<br /><br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> String getServiceName() {<br />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> serviceName;<br />    }<br /><br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> setServiceName(String serviceName) {<br />        </span>
				<span style="COLOR: #0000ff">this</span>
				<span style="COLOR: #000000">.serviceName </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> serviceName;<br />    }<br /><br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> XmlRpcServletServer getXmlRpcServletServer() {<br />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> server;<br />    }<br />    <br />    </span>
				<span style="COLOR: #008000">/*</span>
				<span style="COLOR: #008000"> (non-Javadoc)<br />     * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)<br />     </span>
				<span style="COLOR: #008000">*/</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> ModelAndView handleRequest(HttpServletRequest request,<br />            HttpServletResponse response) </span>
				<span style="COLOR: #0000ff">throws</span>
				<span style="COLOR: #000000"> Exception {<br />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (</span>
				<span style="COLOR: #000000">!</span>
				<span style="COLOR: #000000">WebContentGenerator.METHOD_POST.equals(request.getMethod())) {<br />            </span>
				<span style="COLOR: #0000ff">throw</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> ServletException(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">XmlRpcServiceExporter only supports POST requests</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">);<br />        }<br />        server.execute(request, response);<br />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">;<br />    }<br /><br />    </span>
				<span style="COLOR: #008000">/*</span>
				<span style="COLOR: #008000"> (non-Javadoc)<br />     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()<br />     </span>
				<span style="COLOR: #008000">*/</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">public</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> afterPropertiesSet() </span>
				<span style="COLOR: #0000ff">throws</span>
				<span style="COLOR: #000000"> Exception {<br />        server </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> XmlRpcServletServer();<br />        server.setHandlerMapping(newXmlRpcHandlerMapping());<br />        </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (enabledForExtensions</span>
				<span style="COLOR: #000000">!=</span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">) {<br />            ((XmlRpcServerConfigImpl) server.getConfig()).setEnabledForExtensions(enabledForExtensions.booleanValue());<br />        }<br />        <br />    }<br /><br />    </span>
				<span style="COLOR: #008000">/**</span>
				<span style="COLOR: #008000"> Creates a new handler mapping. The default implementation loads<br />     * a property file from the resource<br />     * <br />     </span>
				<span style="COLOR: #008000">*/</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">protected</span>
				<span style="COLOR: #000000"> XmlRpcHandlerMapping newXmlRpcHandlerMapping() </span>
				<span style="COLOR: #0000ff">throws</span>
				<span style="COLOR: #000000"> XmlRpcException {<br />        <br />        SpringHandlerMapping mapping </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> SpringHandlerMapping(getServiceInterface());<br />        mapping.addHandler(getServiceName(), getServiceInterface());<br />        mapping.setTagetObject(getProxyForService());<br />        </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> mapping;<br />    }<br />    <br />}<br /></span>
		</div>
		<br />spring配置文件<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">bean </span><span style="COLOR: #ff0000">id</span><span style="COLOR: #0000ff">="accountService"</span><span style="COLOR: #ff0000">  class</span><span style="COLOR: #0000ff">="com.jdkcn.service.impl.AccountServiceImpl"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">bean</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">bean </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="rpcAccountService"</span><span style="COLOR: #ff0000"> class</span><span style="COLOR: #0000ff">="com.jdkcn.xmlrpc.XmlRpcServiceExporter"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="service"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">ref </span><span style="COLOR: #ff0000">bean</span><span style="COLOR: #0000ff">="accountService"</span><span style="COLOR: #0000ff">/&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="serviceName"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">jdkcn.accountService</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="enabledForExtensions"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">true</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="serviceInterface"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">com.jdkcn.service.AccountService</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">bean</span><span style="COLOR: #0000ff">&gt;</span></div>然后映射一个地址就可以通过xmlrpc访问服务了<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">bean </span><span style="COLOR: #ff0000">id</span><span style="COLOR: #0000ff">="urlMapping"</span><span style="COLOR: #ff0000"> class</span><span style="COLOR: #0000ff">="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="mappings"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">props</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"> <br />                </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">prop </span><span style="COLOR: #ff0000">key</span><span style="COLOR: #0000ff">="/account"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">rpcAccountService</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">prop</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">props</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">bean</span><span style="COLOR: #0000ff">&gt;</span></div>web.xml<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">context-param</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">param-name</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">contextConfigLocation</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">param-name</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">param-value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />            classpath:spring/global.xml<br />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">param-value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">context-param</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    <br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">listener</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">listener-class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">org.springframework.web.context.ContextLoaderListener</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">listener-class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">listener</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">servlet</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">servlet-name</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">service</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">servlet-name</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">servlet-class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">org.springframework.web.servlet.DispatcherServlet</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">servlet-class</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">servlet</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">servlet-mapping</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">servlet-name</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">service</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">servlet-name</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">url-pattern</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">/service/xmlrpc3/*</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">url-pattern</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">servlet-mapping</span><span style="COLOR: #0000ff">&gt;</span></div>然后我们的service地址就是这样的http://localhost:8080/service/xmlrpc3/account<br /><br />希望对大家有用，这里提供project下载。包含一个client程序。com.jdkcn.xmlrpc.Client<br /><br /><img style="MARGIN: 0px 2px -4px 0px" alt="" src="http://jdkcn.com/images/download.gif" border="0" /><a title="点击下载完整代码" href="http://jdkcn.com/download.asp" target="_blank">点击下载完整代码</a><br /><br /><a href="http://www.creativecommons.cn/licenses/by-nc-sa/1.0/" target="_blank"><img height="31" alt="创造共用协议:署名,非商业,保持一致" src="http://leaf.jdk.cn/rights.gif" width="88" border="0" /></a>   除经特别注明外,本文章版权归<strong><a class="" href="http://jdkcn.com/"><font color="#6b6b52">莫多泡泡</font></a></strong>所有. <br /><font color="#ff0000">署名,非商业用途,保持一致.</font>   <a title="莫多泡泡" href="http://jdkcn.com/" target="_blank">somebody(莫多)</a><img src ="http://www.blogjava.net/rory/aggbug/76628.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rory/" target="_blank">莫多</a> 2006-10-22 16:12 <a href="http://www.blogjava.net/rory/archive/2006/10/22/spring_xmlrpc3_service_exporter.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于spring启动的优化的问题</title><link>http://www.blogjava.net/rory/archive/2006/08/10/speed_up_spring_problem.html</link><dc:creator>莫多</dc:creator><author>莫多</author><pubDate>Thu, 10 Aug 2006 02:59:00 GMT</pubDate><guid>http://www.blogjava.net/rory/archive/2006/08/10/speed_up_spring_problem.html</guid><wfw:comment>http://www.blogjava.net/rory/comments/62740.html</wfw:comment><comments>http://www.blogjava.net/rory/archive/2006/08/10/speed_up_spring_problem.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/rory/comments/commentRss/62740.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rory/services/trackbacks/62740.html</trackback:ping><description><![CDATA[<p>  <span style="FONT-WEIGHT: bold">jspark</span> 的这篇文章《<a class="postTitle2" id="viewpost1_TitleUrl" href="/jspark/archive/2006/07/29/60712.html">开发阶段eclipse下面的spring容器的启动优化</a> 》讲到如何加快spring的启动速度。非常感谢<span style="FONT-WEIGHT: bold">jspark. </span>一下是引用的原文：<br /><br />  <span style="COLOR: rgb(51,51,153); FONT-STYLE: italic">最近在负责一个大项目，项目组成员包括项目经理大概10个人左右。项目技术用struts+spring+hibernate实现。项目的规模相对来说是比较大的，总共有10大模块，每个大模块又分为有十几个、甚至几十个小模块。开发工具用eclipse，由于在开发阶段，项目开发成员需要频繁重启服务器。在启动服务器的时候，每次启动时间总是会超过1分钟。记得以前在做另外一个项目时，启动时间不到5秒钟，相差了10倍，而且项目规模是差不多的。</span><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><span style="COLOR: rgb(51,51,153); FONT-STYLE: italic">    从初步分析来说，应该是hibernate解释hbm.xml时花费时间，或者可能是spring容器启动并解释所有的bean配置文件。诊断了一下，发现1分钟消耗的时间主要分布在hibernate解释hbm.xml花费5秒；spring容器从启动到解释bean配置文件竟然花了58秒，真是太嚣张了。当时非常怀疑spring的效率问题。企图从网上搜索相关资料，看看有什么优化措施。</span><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><span style="COLOR: rgb(51,51,153); FONT-STYLE: italic">    首先是找到了hibernate的启动优化 </span><a style="COLOR: rgb(51,51,153); FONT-STYLE: italic" href="http://www.hibernate.org/194.html">http://www.hibernate.org/194.html</a><span style="COLOR: rgb(51,51,153); FONT-STYLE: italic">  里面的主要思想是通过将xml序列花到本地的文件里，每次读取的时候根据情况，从本地文件读取并反序列化，节省了hibernate xml的解析时间。按照这个方式测试了一下，发现hibernate的启动时间从5秒降低到3秒，但是这个优化对于整个启动过程是杯水车薪的，毫无用处。</span><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><span style="COLOR: rgb(51,51,153); FONT-STYLE: italic">    没办法，又仔细查看了spring的资料，终于发现spring的容器是提供了lazy-load的，即默认的缺省设置是bean没有lazy- load，该属性处于false状态，这样导致spring在启动过程导致在启动时候，会默认加载整个对象实例图，从初始化ACTION配置、到 service配置到dao配置、乃至到数据库连接、事务等等。这么庞大的规模，难怪spring的启动时间要花将近1分钟。尝试了一下，把beans的 default-lazy-init改为true就，再次启动，速度从原来的55秒，降到8秒钟！！Great！虽然是非常小一个改动，但是影响确实非常大。一个项目组10个人，假若每个人一天平均需要在eclipse下启动测试服务器50次。那么一天项目组需要重启500次，每次节省50秒的话，就是 25000秒，将近几个小时，差不多一个工作日，多么可观的数字！</span><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><span style="COLOR: rgb(51,51,153); FONT-STYLE: italic">   不过在运行期间第一次点页面的时候，由于spring做了lazy-load，现在就需要启动一部分需要的beans，所以稍微慢2－3秒钟，但是明显比等几十秒要快很多，值得一鉴。</span><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><br style="COLOR: rgb(51,51,153); FONT-STYLE: italic" /><span style="COLOR: rgb(51,51,153); FONT-STYLE: italic">    以上是针对开发阶段的spring容器启动优化，在部署到实际环境中，倒是没必要设置为lazy-load。毕竟部署到实际环境中不是经常的事，每次启动1分钟倒不是大问题。 </span><br /><br />我这里要提醒的是不是说有的beans都能设置default-lazy-init成为true.对于scheduler的bean不能用lazy-init<br /><br /></p>
		<div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)">
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">beans </span>
				<span style="COLOR: rgb(255,0,0)">default-lazy-init</span>
				<span style="COLOR: rgb(0,0,255)">="true"</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />    <br />    </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">bean </span>
				<span style="COLOR: rgb(255,0,0)">class</span>
				<span style="COLOR: rgb(0,0,255)">="org.springframework.scheduling.quartz.SchedulerFactoryBean"</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />        </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">property </span>
				<span style="COLOR: rgb(255,0,0)">name</span>
				<span style="COLOR: rgb(0,0,255)">="triggers"</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />            </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">list</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="buildHtmlTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="askTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="mailSenderTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="topicDetailBuildTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="forumBuildTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="topicBuildTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />            </span>
				<span style="COLOR: rgb(0,0,255)">&lt;/</span>
				<span style="COLOR: rgb(128,0,0)">list</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />        </span>
				<span style="COLOR: rgb(0,0,255)">&lt;/</span>
				<span style="COLOR: rgb(128,0,0)">property</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />    </span>
				<span style="COLOR: rgb(0,0,255)">&lt;/</span>
				<span style="COLOR: rgb(128,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />
				</span>
				<span style="COLOR: rgb(0,0,255)">&lt;/</span>
				<span style="COLOR: rgb(128,0,0)">beans</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
		</div>
		<p>
				<br />
				<br />
				<br />这样的话。所有的scheduler就都不管用了。所以请大家要注意。<br /></p>
		<div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)">
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">beans</span>
				<span style="COLOR: rgb(255,0,0)">
				</span>
				<span style="COLOR: rgb(0,0,255)">
				</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />    <br />    </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">bean </span>
				<span style="COLOR: rgb(255,0,0)">class</span>
				<span style="COLOR: rgb(0,0,255)">="org.springframework.scheduling.quartz.SchedulerFactoryBean"</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />        </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">property </span>
				<span style="COLOR: rgb(255,0,0)">name</span>
				<span style="COLOR: rgb(0,0,255)">="triggers"</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />            </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">list</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="buildHtmlTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="askTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="mailSenderTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="topicDetailBuildTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="forumBuildTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />                </span>
				<span style="COLOR: rgb(0,0,255)">&lt;</span>
				<span style="COLOR: rgb(128,0,0)">ref </span>
				<span style="COLOR: rgb(255,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">="topicBuildTrigger"</span>
				<span style="COLOR: rgb(0,0,255)">/&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />            </span>
				<span style="COLOR: rgb(0,0,255)">&lt;/</span>
				<span style="COLOR: rgb(128,0,0)">list</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />        </span>
				<span style="COLOR: rgb(0,0,255)">&lt;/</span>
				<span style="COLOR: rgb(128,0,0)">property</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />    </span>
				<span style="COLOR: rgb(0,0,255)">&lt;/</span>
				<span style="COLOR: rgb(128,0,0)">bean</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
				<span style="COLOR: rgb(0,0,0)">
						<br />
				</span>
				<span style="COLOR: rgb(0,0,255)">&lt;/</span>
				<span style="COLOR: rgb(128,0,0)">beans</span>
				<span style="COLOR: rgb(0,0,255)">&gt;</span>
		</div>
		<p>
				<br /> </p><img src ="http://www.blogjava.net/rory/aggbug/62740.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rory/" target="_blank">莫多</a> 2006-08-10 10:59 <a href="http://www.blogjava.net/rory/archive/2006/08/10/speed_up_spring_problem.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于Freemarker生成静态文件的问题</title><link>http://www.blogjava.net/rory/archive/2006/06/21/54134.html</link><dc:creator>莫多</dc:creator><author>莫多</author><pubDate>Wed, 21 Jun 2006 02:46:00 GMT</pubDate><guid>http://www.blogjava.net/rory/archive/2006/06/21/54134.html</guid><wfw:comment>http://www.blogjava.net/rory/comments/54134.html</wfw:comment><comments>http://www.blogjava.net/rory/archive/2006/06/21/54134.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/rory/comments/commentRss/54134.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rory/services/trackbacks/54134.html</trackback:ping><description><![CDATA[首先感谢JScud提供的好文章。《<a class="" title="" href="http://www.jscud.com/srun/news/viewhtml/2_2005_4/36.htm" target="">使用FreeMarker生成Html静态文件(实例)</a>》<br />      在我们的项目中也用到了Freemarker生成静态文件。不过这里我要说的是编码的问题。我们的项目使用的都是UTF-8编码，我直接使用 飞云小侠 提供的方法生成的文件在UTF-8编码下察看是乱码，而GBK正常(后来发现因为我用的中文操作系统所以用GBK查看正常)。<br />      当然我把Freemarker的配置都改成了UTF-8，我的模版文件也是UTF-8编码的。下面是原来的代码<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setTemplatePath(Resource templatePath) {<br />        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.templatePath </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> templatePath;<br />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">设置freemarker的参数</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        freemarkerCfg </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Configuration();<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"> {<br />            freemarkerCfg.setDirectoryForTemplateLoading(</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.templatePath.getFile());<br />            freemarkerCfg.setObjectWrapper(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> DefaultObjectWrapper());<br />            freemarkerCfg.setDefaultEncoding(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (IOException ex) {<br />            </span><span style="COLOR: #0000ff">throw</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> SystemException(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">No Directory found,please check you config.</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        }<br />    }</span></div><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />     * 生成静态文件<br />     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> templateFileName 模版名称eg:(biz/order.ftl)<br />     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> propMap 用于处理模板的属性Object映射 <br />     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> htmlFilePath 要生成的静态文件的路径,相对设置中的根路径,例如 "/biz/2006/5/" <br />     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> htmlFileName 要生成的文件名,例如 "123.htm" <br />     * </span><span style="COLOR: #808080">@return</span><span style="COLOR: #008000"><br />     </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">boolean</span><span style="COLOR: #000000"> buildHtml(String templateFileName,Map propMap, String htmlFilePath,String htmlFileName){<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"> {<br />            Template template </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> freemarkerCfg.getTemplate(templateFileName);<br />            template.setEncoding(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />            </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">创建生成文件目录</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">            creatDirs(buildPath.getFilename(),htmlFilePath);<br />            File htmlFile </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> File(buildPath </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> htmlFilePath </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> htmlFileName);<br />            Writer out </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> BufferedWriter(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> OutputStreamWriter(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> FileOutputStream(htmlFile)));<br />            template.process(propMap,out);<br />            out.flush();<br />            </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (TemplateException ex){<br />            log.error(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Build Error</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">templateFileName,ex);<br />            </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (IOException e) {<br />            log.error(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Build Error</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">templateFileName,e);<br />            </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br />        }<br />        <br />    }</span></div>下面是修改之后的代码<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />     * 生成静态文件<br />     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> templateFileName 模版名称eg:(biz/order.ftl)<br />     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> propMap 用于处理模板的属性Object映射 <br />     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> htmlFilePath 要生成的静态文件的路径,相对设置中的根路径,例如 "/biz/2006/5/" <br />     * </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> htmlFileName 要生成的文件名,例如 "123.htm" <br />     * </span><span style="COLOR: #808080">@return</span><span style="COLOR: #008000"><br />     </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">boolean</span><span style="COLOR: #000000"> buildHtml(String templateFileName,Map propMap, String htmlFilePath,String htmlFileName){<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"> {<br />            Template template </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> freemarkerCfg.getTemplate(templateFileName);<br />            template.setEncoding(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />            </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">创建生成文件目录</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">            creatDirs(buildPath.getFilename(),htmlFilePath);<br />            File htmlFile </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> File(buildPath </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> htmlFilePath </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> htmlFileName);<br />            Writer out </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> BufferedWriter(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> OutputStreamWriter(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> FileOutputStream(htmlFile),</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br />            template.process(propMap,out);<br />            out.flush();<br />            </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (TemplateException ex){<br />            log.error(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Build Error</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">templateFileName,ex);<br />            </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (IOException e) {<br />            log.error(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Build Error</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">templateFileName,e);<br />            </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br />        }<br />        <br />    }</span></div>原因就在于OutputStreamWriter的不同构造方法<br /><br /><a href="file:///D:/Jdk5_API_zh_CN/api/java/io/OutputStreamWriter.html#OutputStreamWriter%28java.io.OutputStream%29"><strong><font face="Courier New"><em>OutputStreamWriter</em></font></strong></a><font face="Courier New"><em>(</em></font><a title="java.io 中的类" href="file:///D:/Jdk5_API_zh_CN/api/java/io/OutputStream.html"><font face="Courier New"><em>OutputStream</em></font></a><em><font face="Courier New"> out)</font><br />          创建使用默认字符编码的 OutputStreamWriter。<br /></em><a href="file:///D:/Jdk5_API_zh_CN/api/java/io/OutputStreamWriter.html#OutputStreamWriter%28java.io.OutputStream,%20java.lang.String%29"><strong><font face="Courier New"><em>OutputStreamWriter</em></font></strong></a><font face="Courier New"><em>(</em></font><a title="java.io 中的类" href="file:///D:/Jdk5_API_zh_CN/api/java/io/OutputStream.html"><font face="Courier New"><em>OutputStream</em></font></a><font face="Courier New"><em> out, </em></font><a title="java.lang 中的类" href="file:///D:/Jdk5_API_zh_CN/api/java/lang/String.html"><font face="Courier New"><em>String</em></font></a><em><font face="Courier New"> charsetName)</font><br />          创建使用指定字符集的 OutputStreamWriter。<br /><br /> </em>这个是中文JDK的文档说明，刚开始我使用默认的构造函数，所以使用了系统默认的编码，GBK，所以在生成静态文件的时候把UTF-8内容用GBK编码写入了，所以在UTF-8下浏览就有问题。<br /><br />还有关于修改模版文件同样也要注意这个问题。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> String loadTemplate(String templateName) {<br />        StringBuffer sb </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> StringBuffer();<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"> {<br />            File file </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> File(templatePath</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">templateName);<br />            BufferedReader reader </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> BufferedReader(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> InputStreamReader(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> FileInputStream(file),</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br />            String line </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> reader.readLine();<br />            </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(line </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">)    {<br />                sb.append(line);<br />                sb.append(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />                line </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> reader.readLine();<br />            }<br />            reader.close();<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (IOException e) {<br />            </span><span style="COLOR: #0000ff">throw</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> SystemException(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Loading template Error:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,e);<br />        }<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> sb.toString();<br />    }</span></div><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> saveTemplate(String templateName, String templateContent) {<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"> {<br />            File file </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> File(templatePath </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> templateName);<br />            Writer out </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> BufferedWriter(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> OutputStreamWriter(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> FileOutputStream(file),</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br />            out.write(templateContent);<br />            out.flush();<br />            </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">扔出templatesave事件</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">            TemplateSaveEvent evt </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> TemplateSaveEvent();<br />            evt.setTemplateName(templateName);<br />            dispatchTemplateEvent(evt);<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (IOException e) {<br />            </span><span style="COLOR: #0000ff">throw</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> SystemException(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Write template Error</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,e);<br />        }<br />    }</span></div><td width="100"><br /></td><img src ="http://www.blogjava.net/rory/aggbug/54134.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rory/" target="_blank">莫多</a> 2006-06-21 10:46 <a href="http://www.blogjava.net/rory/archive/2006/06/21/54134.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring关于底层资源的抽象是如此方便</title><link>http://www.blogjava.net/rory/archive/2006/06/11/52065.html</link><dc:creator>莫多</dc:creator><author>莫多</author><pubDate>Sun, 11 Jun 2006 15:01:00 GMT</pubDate><guid>http://www.blogjava.net/rory/archive/2006/06/11/52065.html</guid><wfw:comment>http://www.blogjava.net/rory/comments/52065.html</wfw:comment><comments>http://www.blogjava.net/rory/archive/2006/06/11/52065.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/rory/comments/commentRss/52065.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rory/services/trackbacks/52065.html</trackback:ping><description><![CDATA[在以前的项目中对于一些资源的配置基本上都是通过spring的IOC注入一个目录的地址字符串。而这样的问题是，对于开发中的团队来说还是很有问题的，因为每个可能都配置一个不同的本地目录，而发布到服务器之后又有不同的目录。这样造成每个人提交了配置文件之后其他人都可能需要修改配置文件才能正确启动服务。这确实很令人烦劳。<br />     最近看《Professional Java Development with the Spring Framework》时看到了spring对底层资源的抽象，才找到了完美解决方案。<br />     原来的代码：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> String templatePath;<br />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setTemplatePath(String templatePath) {<br />        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.templatePath </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> templatePath;<br />    }<br />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> initListener() {<br />        TemplateEventListener templateListener </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> TemplateEventListener(){<br />            </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> handleTemplateEvent(TemplateEventSupport evt) {<br />                </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 添加事件到队列中</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                queue.offer(evt);<br />                </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(log.isDebugEnabled()){<br />                    log.debug(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Add Template about:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> evt.getTemplateName());<br />                }<br />            }<br />            <br />        };<br />        <br />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">注册模版监听事件</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT, templateListener,</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">);<br />        <br />        <br />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">设置freemarker的参数</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        freemarkerCfg </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Configuration();<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"> {<br />            freemarkerCfg.setDirectoryForTemplateLoading(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> File(templatePath));<br />            freemarkerCfg.setObjectWrapper(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> DefaultObjectWrapper());<br />            freemarkerCfg.setDefaultEncoding(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (IOException ex) {<br />            </span><span style="COLOR: #0000ff">throw</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> SystemException(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">No Directory found,please check you config.</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        }<br />    }</span></div>配置文件<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">bean </span><span style="COLOR: #ff0000">id</span><span style="COLOR: #0000ff">="buildHtmlService"</span><span style="COLOR: #ff0000"> class</span><span style="COLOR: #0000ff">="cn.jdk.leaf.service.impl.BuildHtmlServiceImpl"</span><span style="COLOR: #ff0000"> init-method</span><span style="COLOR: #0000ff">="initListener"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="templatePath"</span><span style="COLOR: #0000ff">&gt;&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">${templatePath}</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">bean</span><span style="COLOR: #0000ff">&gt;</span></div><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">templatePath.path</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">D:/template</span></div>使用spring对底层资源的抽象只要把templatePath改成Resource就可以了<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000"> Resource templatePath;<br />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> setTemplatePath(Resource templatePath) {<br />        </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.templatePath </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> templatePath;<br />    }<br />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> initListener() {<br />            TemplateEventListener templateListener </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> TemplateEventListener(){<br />            </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> handleTemplateEvent(TemplateEventSupport evt) {<br />                </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> 添加事件到队列中</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                queue.offer(evt);<br />                </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(log.isDebugEnabled()){<br />                    log.debug(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Add Template about:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> evt.getTemplateName());<br />                }<br />            }<br />            <br />        };    <br />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">注册模版监听事件</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT, templateListener,</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">);<br />        <br />        <br />        </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">设置freemarker的参数</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        freemarkerCfg </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Configuration();<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"> {<br />            freemarkerCfg.setDirectoryForTemplateLoading(templatePath.getFile());<br />            freemarkerCfg.setObjectWrapper(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> DefaultObjectWrapper());<br />            freemarkerCfg.setDefaultEncoding(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">UTF-8</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (IOException ex) {<br />            </span><span style="COLOR: #0000ff">throw</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> SystemException(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">No Directory found,please check you config.</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        }<br />    }</span></div>bean的配置不变，只要修改properties文件就可以了。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">bean id</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">buildHtmlService</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">cn.jdk.leaf.service.impl.BuildHtmlServiceImpl</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> init</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">method</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">initListener</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">property name</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">templatePath</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&gt;&lt;</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">${templatePath}</span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">value</span><span style="COLOR: #000000">&gt;&lt;/</span><span style="COLOR: #000000">property</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">bean</span><span style="COLOR: #000000">&gt;</span></div>把properties文件修改成<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">templatePath.path</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">template</span></div>在webcontext目录下面建立一个template目录就可以了。在部署到服务器的时候需要部署到一个特定的目录只要修改这个配置文件为<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">templatePath.path</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">file:/D:/template</span></div>这样就可以了。<br /><br /><table cellspacing="0" cellpadding="0" bgcolor="#ffffc8" border="0"><tbody><tr><td width="100"><a href="http://www.creativecommons.cn/licenses/by-nc-sa/1.0/" target="_blank"><img height="31" alt="创造共用协议:署名,非商业,保持一致" src="http://www.jscud.com/scud/somerights.gif" width="88" border="0" /></a>  </td><td>除经特别注明外,本文章版权归<strong><a class="" title="" href="http://leaf.jdk.cn" target="">莫多泡泡</a></strong>所有. <br /><font color="#ff0000">署名,非商业用途,保持一致.</font>   somebody(莫多)    </td></tr></tbody></table><img src ="http://www.blogjava.net/rory/aggbug/52065.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rory/" target="_blank">莫多</a> 2006-06-11 23:01 <a href="http://www.blogjava.net/rory/archive/2006/06/11/52065.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于Hibernate的DetachedCriteria查询的addOrder问题的解决办法</title><link>http://www.blogjava.net/rory/archive/2006/05/29/48850.html</link><dc:creator>莫多</dc:creator><author>莫多</author><pubDate>Mon, 29 May 2006 15:29:00 GMT</pubDate><guid>http://www.blogjava.net/rory/archive/2006/05/29/48850.html</guid><wfw:comment>http://www.blogjava.net/rory/comments/48850.html</wfw:comment><comments>http://www.blogjava.net/rory/archive/2006/05/29/48850.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.blogjava.net/rory/comments/commentRss/48850.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rory/services/trackbacks/48850.html</trackback:ping><description><![CDATA[上篇文章:《<a class="singleposttitle" id="viewpost1_TitleUrl" href="/rory/archive/2006/05/21/47339.html"><font color="#223355"><strong>今天发现一个hibernate的bug,或者说一个应该注意的地方比较合适</strong></font></a> 》里面我提到了Hibernate查询需要注意的一个问题。今天发现了一个最好的解决办法。如果大家现在用Hibernate，相信大家都回用到DetachedCriteria.关于DetachedCriteria查询请查看<a href="http://dev.yesky.com/241/2033241.shtml">http://dev.yesky.com/241/2033241.shtml</a>。<br />      DetachedCriteria给我们的Hibernate查询带来了很多方便，但是如果你带上排序信息就会出现我的上一篇文章里面说的那种错误，今天发现一个很好的解决方法，其实也很简单。就是先把传入的带Order信息的DetachedCriteria去掉order信息查询数据总条数，然后再把Order加回来查询满足条件的对象。通过查看Hibernate的源代码发现Criteria的实现CriteriaImpl发现其实addOrder是给private List orderEntries = new ArrayList();这个List加值。这个List里面放的是OrderEntry对象。这个OrderEntry里面放了一个criteria 和 order.<br /><br />     
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> PaginationSupport findPageByCriteria(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> DetachedCriteria detachedCriteria, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> pageSize, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> startIndex) {<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (PaginationSupport) getHibernateTemplate().execute(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> HibernateCallback() {<br />            </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object doInHibernate(Session session) </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> HibernateException {<br />                Criteria criteria </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> detachedCriteria.getExecutableCriteria(session);<br />                CriteriaImpl impl </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (CriteriaImpl) criteria;<br />                List orderEntrys </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> ArrayList();<br />                </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">{<br />                    Field field </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> CriteriaImpl.</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">.getDeclaredField(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">orderEntries</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />                    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Get orders</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                    orderEntrys </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (List) field.get(impl);<br />                    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Remove orders</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                    field.set(criteria,</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> ArrayList());<br />                }</span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000">(Exception ex){<br />                    ex.printStackTrace();<br />                    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">TODO xxxx</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                }<br />                </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> totalCount </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ((Integer) criteria.setProjection(Projections.rowCount())<br />                        .uniqueResult()).intValue();<br />                criteria.setProjection(</span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">);<br />                <br />                </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">{<br />                    Field field </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> CriteriaImpl.</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">.getDeclaredField(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">orderEntries</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />                    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Add orders return</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">orderEntrys.size(); i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">){<br />                        List innerOrderEntries </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (List) field.get(criteria);<br />                        innerOrderEntries.add(orderEntrys.get(i));<br />                    }<br />                }</span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000">(Exception ex){<br />                    ex.printStackTrace();<br />                    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">TODO cccc</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                }<br />                List items </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();<br />                PaginationSupport ps </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> PaginationSupport(items, totalCount, pageSize,<br />                        startIndex);<br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> ps;<br />            }<br />        }, </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">);<br />    }</span></div>希望大家多多交流<img src ="http://www.blogjava.net/rory/aggbug/48850.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rory/" target="_blank">莫多</a> 2006-05-29 23:29 <a href="http://www.blogjava.net/rory/archive/2006/05/29/48850.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于Hibernate3的NativeSQL查询是如此简单</title><link>http://www.blogjava.net/rory/archive/2006/05/26/48274.html</link><dc:creator>莫多</dc:creator><author>莫多</author><pubDate>Fri, 26 May 2006 02:33:00 GMT</pubDate><guid>http://www.blogjava.net/rory/archive/2006/05/26/48274.html</guid><wfw:comment>http://www.blogjava.net/rory/comments/48274.html</wfw:comment><comments>http://www.blogjava.net/rory/archive/2006/05/26/48274.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/rory/comments/commentRss/48274.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rory/services/trackbacks/48274.html</trackback:ping><description><![CDATA[现在的系统中虽然使用了。Hibernate但是没有使用Hibernate的关联关系来进行数据库操作。所有的管理操作都是单独实现的。所以也不能用Criteria.add()这种方式去查询关联的一方。所以只能用Native SQL去查询结果返回对象了。按照Hibernate3的reference里面说的<br /><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="CLEAR: both"><font color="#006400">16.1. 使用<tt class="literal">SQLQuery</tt></font></h2></div></div><div><font color="#006400"></font></div></div><p><font color="#006400">对原生SQL查询执行的控制是通过<tt class="literal">SQLQuery</tt>接口进行的，通过执行<tt class="literal">Session.createSQLQuery()</tt>获取这个接口。最简单的情况下，我们可以采用以下形式：</font></p><pre class="programlisting"><font color="#006400">List cats = sess.createSQLQuery("select * from cats")
    .addEntity(Cat.class)
    .list();</font></pre><p><font color="#006400">这个查询指定了:</font></p><div class="itemizedlist"><ul type="disc"><li><p><font color="#006400">SQL查询字符串</font></p></li><li><p><font color="#006400">查询返回的实体</font></p></li></ul></div><p><font color="#006400">这里，结果集字段名被假设为与映射文件中指明的字段名相同。对于连接了多个表的查询，这就可能造成问题，因为可能在多个表中出现同样名字的字段。下面的方法就可以避免字段名重复的问题:</font></p><pre class="programlisting"><font color="#006400">List cats = sess.createSQLQuery("select {cat.*} from cats cat")
    .addEntity("cat", Cat.class)
    .list();</font></pre><p><font color="#006400">这个查询指定了: </font></p><div class="itemizedlist"><ul type="disc"><li><p><font color="#006400">SQL查询语句，它带一个占位符，可以让Hibernate使用字段的别名. </font></p></li><li><p><font color="#006400">查询返回的实体，和它的SQL表的别名. </font></p></li></ul></div><p><font color="#006400"><tt class="literal">addEntity()</tt>方法将SQL表的别名和实体类联系起来，并且确定查询结果集的形态。 </font></p><p><font color="#006400"><tt class="literal">addJoin()</tt>方法可以被用于载入其他的实体和集合的关联. </font></p><pre class="programlisting"><font color="#006400">List cats = sess.createSQLQuery(
        "select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id"
    )
    .addEntity("cat", Cat.class)
    .addJoin("kitten", "cat.kittens")
    .list();</font></pre><p><font color="#006400">原生的SQL查询可能返回一个简单的标量值或者一个标量和实体的结合体。 </font></p><pre class="programlisting"><font color="#006400">Double max = (Double) sess.createSQLQuery("select max(cat.weight) as maxWeight from cats cat")
        .addScalar("maxWeight", Hibernate.DOUBLE);
        .uniqueResult();</font></pre><p><font color="#006400">除此之外，你还可以在你的hbm文件中描述结果集映射信息，在查询中使用。</font></p><pre class="programlisting"><font color="#006400">List cats = sess.createSQLQuery(
        "select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id"
    )
    .setResultSetMapping("catAndKitten")
    .list();<br /></font></pre></div><br />后来我使用了命名SQL查询方式。<br /><div class="titlepage"><div><div><h2 class="title" style="CLEAR: both"><font color="#006400">16.3. 命名SQL查询</font></h2></div></div><div><font color="#006400"></font></div></div><p><font color="#006400">可以在映射文档中定义查询的名字,然后就可以象调用一个命名的HQL查询一样直接调用命名SQL查询.在这种情况下,我们<span class="emphasis"><em>不</em></span> 需要调用<tt class="literal">addEntity()</tt>方法. </font></p><pre class="programlisting"><font color="#006400">&lt;sql-query name="persons"&gt;
    &lt;return alias="person" class="eg.Person"/&gt;
    SELECT person.NAME AS {person.name},
           person.AGE AS {person.age},
           person.SEX AS {person.sex}
    FROM PERSON person
    WHERE person.NAME LIKE :namePattern
&lt;/sql-query&gt;</font></pre><pre class="programlisting"><font color="#006400">List people = sess.getNamedQuery("persons")
    .setString("namePattern", namePattern)
    .setMaxResults(50)
    .list();</font><br /><br />我觉得这种发式比较好。这样写出来的sql可以很整齐。我们的数据库使用的是oracle，不过按照这上面的写法发现sql语句有错误。<br />后来拿到控制台执行也抱错。因为原来都用sqlserver，而sqlserver都是可以的。后来发现是表不能有别名改成这样就好了。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">            </span><span style="COLOR: #0000ff">SELECT</span><span style="COLOR: #000000"> T_PAY.sys_id </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.sysId},<br />                   T_PAY.sys_flag </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.sysFlag},<br />                   T_PAY.sys_domain </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.sysDomain},<br />                   T_PAY.sys_owner </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.sysOwner},<br />                   T_PAY.sys_create_date </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.sysCreateDate},<br />                   T_PAY.sys_update_date </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.sysUpdateDate},<br />                   T_PAY.pay_id </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.payId},<br />                   T_PAY.pay_name </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.payName},<br />                   T_PAY.pay_type_id </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.payTypeId},<br />                   T_PAY.pay_date </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.payDate},<br />                   T_PAY.money_type_id </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.moneyTypeId},<br />                   T_PAY.amount </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.amount},<br />                   T_PAY.payer_id </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.payerId},<br />                   T_PAY.payer_name </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.payerName},<br />                   T_PAY.accept_id </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.acceptId},<br />                   T_PAY.accept_name </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.acceptName},<br />                   T_PAY.pay_state_id </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.payStateId},<br />                   T_PAY.remark </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> {pay.remark}<br />            </span><span style="COLOR: #0000ff">FROM</span><span style="COLOR: #000000">   T_PAY<br />            </span><span style="COLOR: #0000ff">JOIN</span><span style="COLOR: #000000">   T_BIZ_PAY<br />            </span><span style="COLOR: #0000ff">ON</span><span style="COLOR: #000000">       T_PAY.pay_id </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> T_BIZ_PAY.pay_id<br />            </span><span style="COLOR: #0000ff">WHERE</span><span style="COLOR: #000000">  T_BIZ_PAY.biz_id </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> :bizId</span></div></pre>  <strong>这里要特别的提醒一下大家千万不要把主键忘了。刚开始我就忘了主键，后来调试了半天才找出原因来。<br /></strong><br />这样在sping里面用回调查询一下就ok了。真的很方便.<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> List getPaysByBizId(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> String bizId) {<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (List)getHibernateTemplate().execute(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> HibernateCallback(){<br />            </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object doInHibernate(Session session)</span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> HibernateException{<br />                Query query </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> session.getNamedQuery(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">find.pays.by.bizid</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />                query.setParameter(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">bizId</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,bizId);<br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> query.list();<br />            }<br />        },</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">);<br />    }</span></div><img src ="http://www.blogjava.net/rory/aggbug/48274.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rory/" target="_blank">莫多</a> 2006-05-26 10:33 <a href="http://www.blogjava.net/rory/archive/2006/05/26/48274.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>今天发现一个hibernate的bug,或者说一个应该注意的地方比较合适</title><link>http://www.blogjava.net/rory/archive/2006/05/21/47339.html</link><dc:creator>莫多</dc:creator><author>莫多</author><pubDate>Sun, 21 May 2006 14:49:00 GMT</pubDate><guid>http://www.blogjava.net/rory/archive/2006/05/21/47339.html</guid><wfw:comment>http://www.blogjava.net/rory/comments/47339.html</wfw:comment><comments>http://www.blogjava.net/rory/archive/2006/05/21/47339.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.blogjava.net/rory/comments/commentRss/47339.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rory/services/trackbacks/47339.html</trackback:ping><description><![CDATA[今天把myblog用sql server部署了一下，可是发现分页查询的时候出现错误，看控制台报错说语句有错,由来发现分页的时候先查询总记录数目的那条语句出错了<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">count</span><span style="COLOR: #000000">(</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> y0_ </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> myblog_Blog this_ </span><span style="COLOR: #0000ff">inner</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> myblog_Blog_Category categories3_ </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> this_.id</span><span style="COLOR: #808080">=</span><span style="COLOR: #000000">categories3_.blogId </span><span style="COLOR: #0000ff">inner</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> myblog_Category category1_ </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> categories3_.categoryId</span><span style="COLOR: #808080">=</span><span style="COLOR: #000000">category1_.id </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"> category1_.id</span><span style="COLOR: #808080">=</span><span style="COLOR: #000000">? </span><span style="COLOR: #0000ff">order</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"> this_.postTime </span><span style="COLOR: #0000ff">desc</span></div>      原来开发的时候我是用的mysql，没有任何问题。原因就在最后面的order by 语句，sql server 在select count(*)里面不能用 order by。然后跟踪代码发现:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> PaginationSupport getBlogsByCategoryByPage(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> String categoryId, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> startIndex, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> pageSize) {<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (PaginationSupport) getHibernateTemplate().execute(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> HibernateCallback() {<br />            </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object doInHibernate(Session session) </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> HibernateException {<br />                Criteria criteria </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> session.createCriteria(Blog.</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">);<br />                Criteria cateCriteria </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> criteria.createCriteria(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">categories</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />                cateCriteria.add(Expression.eq(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">id</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,categoryId));<br />                criteria.addOrder(Order.desc(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">postTime</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br />                </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> totalCount </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ((Integer) criteria.setProjection(Projections.rowCount())<br />                        .uniqueResult()).intValue();<br />                criteria.setProjection(</span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">);<br />                <br />                List items </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();<br />                List blogs </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> ArrayList();<br />                </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(Iterator ite </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> items.iterator(); ite.hasNext();) {<br />                    Object[] objs </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (Object[])ite.next();<br />                    blogs.add(objs[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br />                }<br />                PaginationSupport ps </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> PaginationSupport(blogs, totalCount, pageSize, startIndex);<br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> ps;<br />            }<br />        }, </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">);<br />    }</span></div>原来问题就在Criteria.addOrder(Order.desc("postTime"));这句话的位置上面，int totalCount = ((Integer) criteria.setProjection(Projections.rowCount())<br />      .uniqueResult()).intValue();<br />这句话的时候就会生成上面那句话，如果在这之前addOrder就会出现问题，如果你用mysql不会出现问题，如果你用sql server就会报错。解决方法就是把addOrder语句放到totalCount下面就可以了。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> PaginationSupport getBlogsByCategoryByPage(</span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> String categoryId, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> startIndex, </span><span style="COLOR: #0000ff">final</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> pageSize) {<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (PaginationSupport) getHibernateTemplate().execute(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> HibernateCallback() {<br />            </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Object doInHibernate(Session session) </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> HibernateException {<br />                Criteria criteria </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> session.createCriteria(Blog.</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">);<br />                Criteria cateCriteria </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> criteria.createCriteria(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">categories</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />                cateCriteria.add(Expression.eq(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">id</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,categoryId));<br />                </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> totalCount </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ((Integer) criteria.setProjection(Projections.rowCount())<br />                        .uniqueResult()).intValue();<br />                criteria.setProjection(</span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">);<br />                <br />                </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br />                 * Fix a bug ,Order must add after get the totalCount,<br />                 * beacuse SqlServer not support order by in the select count(*).<br />                 </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br />                criteria.addOrder(Order.desc(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">postTime</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br />                List items </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();<br />                List blogs </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> ArrayList();<br />                </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(Iterator ite </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> items.iterator(); ite.hasNext();) {<br />                    Object[] objs </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (Object[])ite.next();<br />                    blogs.add(objs[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br />                }<br />                PaginationSupport ps </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> PaginationSupport(blogs, totalCount, pageSize, startIndex);<br />                </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> ps;<br />            }<br />        }, </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">);<br />    }</span></div><br />这样生成的sql语句就是这样的。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">count</span><span style="COLOR: #000000">(</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> y0_ </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> myblog_Blog this_ </span><span style="COLOR: #0000ff">inner</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> myblog_Blog_Category categories3_ </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> this_.id</span><span style="COLOR: #808080">=</span><span style="COLOR: #000000">categories3_.blogId </span><span style="COLOR: #0000ff">inner</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> myblog_Category category1_ </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> categories3_.categoryId</span><span style="COLOR: #808080">=</span><span style="COLOR: #000000">category1_.id </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"> category1_.id</span><span style="COLOR: #808080">=</span><span style="COLOR: #000000">?</span></div><br />以后大家也要注意了。呵呵。<br /><img src ="http://www.blogjava.net/rory/aggbug/47339.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rory/" target="_blank">莫多</a> 2006-05-21 22:49 <a href="http://www.blogjava.net/rory/archive/2006/05/21/47339.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何给webwork的action里的Field设置初始值(续)</title><link>http://www.blogjava.net/rory/archive/2006/04/22/42463.html</link><dc:creator>莫多</dc:creator><author>莫多</author><pubDate>Sat, 22 Apr 2006 03:21:00 GMT</pubDate><guid>http://www.blogjava.net/rory/archive/2006/04/22/42463.html</guid><wfw:comment>http://www.blogjava.net/rory/comments/42463.html</wfw:comment><comments>http://www.blogjava.net/rory/archive/2006/04/22/42463.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/rory/comments/commentRss/42463.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rory/services/trackbacks/42463.html</trackback:ping><description><![CDATA[前面一篇文章提到通过webwork的interceptor给webwork的action设置初始值，
<h2><a class="singleposttitle" id="viewpost1_TitleUrl" href="/rory/archive/2006/03/16/35656.html">如何给webwork的action里的Field设置初始值</a></h2><p>jdev说到"为什么要放在这里，而不是放到系统配置文件中，然后在系统设置界面中提供设置"，后来我就用spring来注入了。现在就把值放入properties文件中了。<br /></p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">bean </span><span style="COLOR: #ff0000">id</span><span style="COLOR: #0000ff">="propertyPlaceholderConfigurer"</span><span style="COLOR: #ff0000"> class</span><span style="COLOR: #0000ff">="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />      </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="locations"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">list</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />          </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">classpath:mail.properties</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />          </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">classpath:upload.properties</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">list</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />      </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">bean</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #008000">&lt;!--</span><span style="COLOR: #008000"> 收发外部邮箱 author:somebody</span><span style="COLOR: #008000">--&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">bean </span><span style="COLOR: #ff0000">id</span><span style="COLOR: #0000ff">="outMailManager"</span><span style="COLOR: #ff0000"> class</span><span style="COLOR: #0000ff">="cn.com.fivefortunes.mail.manager.impl.OutMailManagerImpl"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="javaMailSender"</span><span style="COLOR: #0000ff">&gt;&lt;</span><span style="COLOR: #800000">ref </span><span style="COLOR: #ff0000">bean</span><span style="COLOR: #0000ff">="javaMailSender"</span><span style="COLOR: #0000ff">/&gt;&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="attachementDirectory"</span><span style="COLOR: #0000ff">&gt;&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">${mail.attachementDirectory}</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="uploadFileDirectory"</span><span style="COLOR: #0000ff">&gt;&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">${upload.directory}</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="outMailHibernateManager"</span><span style="COLOR: #0000ff">&gt;&lt;</span><span style="COLOR: #800000">ref </span><span style="COLOR: #ff0000">bean</span><span style="COLOR: #0000ff">="outMailHibernateManager"</span><span style="COLOR: #0000ff">/&gt;&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="outMailAccountManager"</span><span style="COLOR: #0000ff">&gt;&lt;</span><span style="COLOR: #800000">ref </span><span style="COLOR: #ff0000">bean</span><span style="COLOR: #0000ff">="outMailAccountManager"</span><span style="COLOR: #0000ff">/&gt;&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">bean</span><span style="COLOR: #0000ff">&gt;</span></div>这样把uploadFileDirectory放入mananger中去了。在properties里面配置一下就ok了。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000"><br />upload.directory</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">D:\\tomcat5\\webapps\\files<br />mail.attachementDirectory</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">D:\\tomcat5\\webapps\\files<br /></span></div>上次还有网友这样说到"return ServletActionContext.getServletContext().getRealPath("/"); <br /><br />你这样换了部署环境每次都要改 <br />而且也该是 <br />D://tomcat5//webapps//files"<br />也许很多人都以为这是最好的方式，可是实际情况不是这样，我们的uploadfile目录已经有3G的文件，而基本上每天我们都要发布新的webapp，如果把uploadfile目录放到webapp的一个子目录，每次部署的时候都要先把uploadfile copy出来然后再部署。所以我们这么做就是把upload目录放到其他目录不影响uploadfile目录里面的文件。关于我们项目的部署见我的下一篇文章。谢谢大家。如果您有什么更好的方法请您指教。<img src ="http://www.blogjava.net/rory/aggbug/42463.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rory/" target="_blank">莫多</a> 2006-04-22 11:21 <a href="http://www.blogjava.net/rory/archive/2006/04/22/42463.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring操作数据库如此简单</title><link>http://www.blogjava.net/rory/archive/2006/04/07/39699.html</link><dc:creator>莫多</dc:creator><author>莫多</author><pubDate>Thu, 06 Apr 2006 16:03:00 GMT</pubDate><guid>http://www.blogjava.net/rory/archive/2006/04/07/39699.html</guid><wfw:comment>http://www.blogjava.net/rory/comments/39699.html</wfw:comment><comments>http://www.blogjava.net/rory/archive/2006/04/07/39699.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.blogjava.net/rory/comments/commentRss/39699.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rory/services/trackbacks/39699.html</trackback:ping><description><![CDATA[以前一直都没有试过，前几天打算把wordpress换成自己写的程序，就想到了数据的导入和导出，首先想到的是用数据库工具来导。可是觉得有些麻烦，我自己的程序是用spring+hibernate的。后来我就试了一下spring的JdbcTemplate,就和HibernateTemplate一样的好用。首先增加一个连接到wp数据库的dataSource 
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">bean </span><span style="COLOR: #ff0000">id</span><span style="COLOR: #0000ff">="dataSource2"</span><span style="COLOR: #ff0000"> class</span><span style="COLOR: #0000ff">="org.springframework.jdbc.datasource.DriverManagerDataSource"</span><span style="COLOR: #ff0000"> destroy-method</span><span style="COLOR: #0000ff">="close"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="driverClassName"</span><span style="COLOR: #0000ff">&gt;&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">org.hibernate.dialect.MySQLDialect</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="url"</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">jdbc:mysql://192.168.0.240:3306/wordpressωuseUnicode=true</span><span style="COLOR: #ff0000">&amp;amp;</span><span style="COLOR: #000000">characterEncoding=utf8</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="username"</span><span style="COLOR: #0000ff">&gt;&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000">root</span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">&lt;</span><span style="COLOR: #800000">property </span><span style="COLOR: #ff0000">name</span><span style="COLOR: #0000ff">="password"</span><span style="COLOR: #0000ff">&gt;&lt;</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;&lt;/</span><span style="COLOR: #800000">value</span><span style="COLOR: #0000ff">&gt;&lt;/</span><span style="COLOR: #800000">property</span><span style="COLOR: #0000ff">&gt;</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">&lt;/</span><span style="COLOR: #800000">bean</span><span style="COLOR: #0000ff">&gt;</span></div><p>    然后在转换程序里面get这个dataSource，new 一个JdbcTemplate(dataSource2)，这样就ok了。很简单吧。</p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> testCopyData() </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> Exception{<br />        DataSource ds </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (DataSource)applicationContext.getBean(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">dataSource2</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        <br />        CategoryManager cateManager </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (CategoryManager) applicationContext.getBean(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">categoryManager</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        <br />        JdbcTemplate jt </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> JdbcTemplate(ds);<br />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Total posts:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">jt.queryForInt(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">select count(*) from wp_posts</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br />        assertNotNull(ds);<br />        <br />        List cates </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> jt.queryForList(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">select * from wp_categories</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br />        </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(Iterator ite </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> cates.iterator(); ite.hasNext();){<br />            i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br />            Map result </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (Map) ite.next();<br />            Category cate </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Category();<br />            cate.setName((String)result.get(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">cat_name</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br />            cate.setOrder(i);<br />            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br />                cate.setDefaultCategory(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">);<br />            cateManager.saveCategory(cate);<br />            System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">cat_name:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">result.get(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">cat_name</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />        }<br />    }</span></div><img src ="http://www.blogjava.net/rory/aggbug/39699.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rory/" target="_blank">莫多</a> 2006-04-07 00:03 <a href="http://www.blogjava.net/rory/archive/2006/04/07/39699.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>