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

<channel>
	<title>haojii &#187; Struts</title>
	<atom:link href="http://www.haojii.com/category/struts/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.haojii.com</link>
	<description>一个专注于技术的IT男</description>
	<lastBuildDate>Thu, 19 Apr 2012 07:58:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>如何保证Struts Action的线程安全?</title>
		<link>http://www.haojii.com/2011/03/threadsafe-problem-in-struts-action/</link>
		<comments>http://www.haojii.com/2011/03/threadsafe-problem-in-struts-action/#comments</comments>
		<pubDate>Sat, 19 Mar 2011 15:08:53 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Struts]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=523</guid>
		<description><![CDATA[使用Spring代理Struts Action请求的话这个问题可以通过配置bean的生成方式简单的解决线程安全问题（ org.springframework.web.struts.DelegatingActionProxy） 如果自己手工做怎么做？这是我们系统里一个很普遍的架构 我暂时想的是用ThreadLocal控制BusinessLogicServerLayer对象或者是EntityManager对象，应该可以解决线程问题。 不知道还有啥解决方案？ To be continued&#8230;<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1" color="#FF0000" style="display: block !important; padding: 20px 0 5px !important;">其他文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Struts1 RESTFul Style URL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">为什么EL表达式里的属性名不是javabean的字段呢？</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F04%2Fcoincidence%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">阴差阳错</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F10%2Fturkey-turkey%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Turkey turkey</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>使用Spring代理Struts Action请求的话这个问题可以通过配置bean的生成方式简单的解决线程安全问题（ org.springframework.web.struts.DelegatingActionProxy）</p>
<p>如果自己手工做怎么做？这是我们系统里一个很普遍的架构</p>
<pre class="brush: java; title: ; notranslate">

public XXBusinessLogicAction extends DispatchAction {

private BusinessLogicServiceLayer  service;

....

}
public BusinessSerivceLayer {

private BusinessLogicDAO dao;

public boolean create(obj) {

dao.create(obj);

}
public BusinessLogicDAO {

private EntityManger em;

public boolean create(obj) {

em.persist(obj);

}
</pre>
<p>我暂时想的是用ThreadLocal控制BusinessLogicServerLayer对象或者是EntityManager对象，应该可以解决线程问题。</p>
<p>不知道还有啥解决方案？</p>
<p>To be continued&#8230;</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1" color="#FF0000" style="display: block !important; padding: 20px 0 5px !important;">其他文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Struts1 RESTFul Style URL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">为什么EL表达式里的属性名不是javabean的字段呢？</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F01%2Fcool-css-button-1%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">炫酷CSS超链接按钮一</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F09%2Fedianping-user-experience-analyse%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">E店评的用户体验分析</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2011/03/threadsafe-problem-in-struts-action/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Struts1 RESTFul Style URL</title>
		<link>http://www.haojii.com/2011/02/struts1-restful-style-url/</link>
		<comments>http://www.haojii.com/2011/02/struts1-restful-style-url/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 15:05:37 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[Struts]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=499</guid>
		<description><![CDATA[作为一个程序员我还是更喜欢研究技术问题，喜欢动手实干，吹牛／空谈／诡辩实在不是我的强项啊，这两天开会太多，空闲时间太少，抽空研究了下Struts1 RESTFUL 风格URL的设计和实现。 使用到UrlRewriteFilter，文档 http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.1/index.html 用起来还算简单，在web.xml中配置一个filter，大家知道filter和Servlet的工作顺序 filter-&#62;Servlet-&#62;filter 所以这个其实和Struts1的入口org.apache.struts.action.ActionServlet没啥冲突的。 如何用这个？要配置一个urlrewrite.xml rule from 定义的是用户看得到的，用户发起的，浏览器地址栏的url to 定义的是程序内部的URL，这里配置的其实是Struts的某个dispatchAction outbound-rule from定义的是程序内部的URL，from里的特殊字符需要转义，to里的不需要转义 to 定义的是程序生成的HTML里的link 比如下面的HTML代码生成的两个link是这样的 http://localhost:8080/url_rewrite_demo/user/get http://localhost:8080/url_rewrite_demo/user/delete 可以看到，struts的html:link也如我们期望的那样生成了期望看到的clean的URL。 基于这个我们可以在struts1的基础上支持RESTFul风格URL的API了， 如果在请求的Header里加上不同的值，根据rewrite配置其实可以render HTML/XML/JSON，这样简单的API就能工作了，暂时还没考虑用户认证＋权限什么的。 一个风格清晰的URL如下 /users /user/1 (GET for read) /user/1/edit (GET for form, POST for update) /user/1/delete (GET for &#8230; <a href="http://www.haojii.com/2011/02/struts1-restful-style-url/">继续阅读 <span class="meta-nav">&#8594;</span></a><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1" color="#FF0000" style="display: block !important; padding: 20px 0 5px !important;">其他文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">如何保证Struts Action的线程安全?</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">为什么EL表达式里的属性名不是javabean的字段呢？</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2011%2F01%2Fphilips-led-light%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">炫彩7色LED灯</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F02%2Fsecond-post-from-iphone%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Second post from iPhone</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>作为一个程序员我还是更喜欢研究技术问题，喜欢动手实干，吹牛／空谈／诡辩实在不是我的强项啊，这两天开会太多，空闲时间太少，抽空研究了下Struts1 RESTFUL 风格URL的设计和实现。</p>
<p>使用到UrlRewriteFilter，文档 <a href="http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.1/index.html">http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.1/index.html</a></p>
<p>用起来还算简单，在web.xml中配置一个filter，大家知道filter和Servlet的工作顺序 filter-&gt;Servlet-&gt;filter</p>
<p>所以这个其实和Struts1的入口org.apache.struts.action.ActionServlet没啥冲突的。</p>
<pre class="brush: xml; title: ; notranslate">

&lt;!-- UrlRewriteFilter filter --&gt;

&lt;!-- UrlRewriteFilter filter --&gt;
&lt;filter&gt;
&lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
&lt;filter-class&gt;
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
&lt;/filter-class&gt;
&lt;init-param&gt;
&lt;param-name&gt;logLevel&lt;/param-name&gt;
&lt;param-value&gt;WARN&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;/filter&gt;
</pre>
<p>如何用这个？要配置一个urlrewrite.xml</p>
<pre class="brush: xml; title: ; notranslate">

&lt;rule&gt;

&lt;note&gt;&lt;/note&gt;

&lt;from&gt;/user/(.*)&lt;/from&gt;

&lt;to type=&quot;forward&quot;&gt;/UserAction.do?method=$1&lt;/to&gt;

&lt;/rule&gt;

&lt;outbound-rule&gt;

&lt;note&gt;&lt;/note&gt;

&lt;from&gt;/UserAction.do\?method=(.*)&lt;/from&gt;

&lt;to&gt;/user/$1&lt;/to&gt;

&lt;/outbound-rule&gt;
</pre>
<p><strong>rule</strong></p>
<p>from 定义的是用户看得到的，用户发起的，浏览器地址栏的url</p>
<p>to 定义的是程序内部的URL，这里配置的其实是Struts的某个dispatchAction</p>
<p><strong>outbound-rule</strong></p>
<p>from定义的是程序内部的URL，from里的特殊字符需要转义，to里的不需要转义</p>
<p>to 定义的是程序生成的HTML里的link</p>
<p>比如下面的HTML代码生成的两个link是这样的</p>
<p>http://localhost:8080/url_rewrite_demo<span style="color: #ff0000;">/</span><span style="color: #ff0000;">user/get</span></p>
<p>http://localhost:8080/url_rewrite_demo<span style="color: #ff0000;">/</span><span style="color: #ff0000;">user/delete</span></p>
<pre class="brush: xml; title: ; notranslate">

&lt;a href=&quot;&lt;c:url value='/UserAction.do?method=get' /&gt;&quot;&gt;get&lt;/a&gt;

&lt;%request.setAttribute(&quot;request_method&quot;,&quot;delete&quot;);%&gt;

&lt;html:link action=&quot;/UserAction&quot; paramId=&quot;method&quot; paramName=&quot;request_method&quot;&gt;${request_method}&lt;/html:link&gt;
</pre>
<p>可以看到，struts的html:link也如我们期望的那样生成了期望看到的clean的URL。</p>
<p>基于这个我们可以在struts1的基础上支持RESTFul风格URL的API了，</p>
<p>如果在请求的Header里加上不同的值，根据rewrite配置其实可以render HTML/XML/JSON，这样简单的API就能工作了，暂时还没考虑用户认证＋权限什么的。</p>
<div id="_mcePaste"></div>
<div>一个风格清晰的URL如下</div>
<div>/users</div>
<div id="_mcePaste">/user/1 (GET for read)</div>
<div id="_mcePaste">/user/1/edit (GET for form, POST for update)</div>
<div id="_mcePaste">/user/1/delete (GET for form, POST for delete)</div>
<div id="_mcePaste">/user/create (GET for form, POST for create)</div>
<div>
<div></div>
</div>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1" color="#FF0000" style="display: block !important; padding: 20px 0 5px !important;">其他文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fthreadsafe-problem-in-struts-action%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">如何保证Struts Action的线程安全?</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">为什么EL表达式里的属性名不是javabean的字段呢？</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F01%2Fintegrate-gravatar-for-comment-using-ajax%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">使用AJAX集成Gravatar头像到自己的评论系统</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F01%2Fcool-css-button-1%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">炫酷CSS超链接按钮一</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2011/02/struts1-restful-style-url/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>为什么EL表达式里的属性名不是javabean的字段呢？</title>
		<link>http://www.haojii.com/2009/12/why-el-use-java-property-instead-of-field/</link>
		<comments>http://www.haojii.com/2009/12/why-el-use-java-property-instead-of-field/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 04:17:50 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[Struts]]></category>
		<category><![CDATA[EL]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=40</guid>
		<description><![CDATA[这个问题的场景,相信很多人都写过Struts的helloworld程序,下面是一个常见的hello.jsp页面，请求经由org.apache.struts.action.ActionServlet或者org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter几经处理之后，最后返回ActionForward或者action的result相应的jsp或者各种模板页面。 这个结果页面中本质一直没有变，取得它所需要的值，通常是一个javabean的属性，假设这个javabean是下面的Bean20091228，那么为什么这里我们写的是属性${message}而不是字段${msg}呢？ 要搞清楚问题首先要区分java中类的字段和属性的区别，对，这是显然有区别的 字段：通常是在类中定义的类成员变量 例如：下面的value和msg 属性：类中读写方法的声明，属性的名字不一定要和实际访问的字段相同 例如：下面的类我们可以说Bean20091228有支持读写的属性message, 属性名称默认小写开头。 搞清楚这个问题之后，下面这段代码用反射机制取得这个类的PropertyDescriptor，并且使用反射设置message属性的值,第5行代码检查是否当前迭代属性是message,第7行代码调用该属性的写方法，设置新的值为”hello,reflection” 该程序执行的打印结果为hello,reflection 所以EL表达式这玩意，在JSP页面解析的时候，技术上是使用reflection机制取得javabean的attribute的值，这就是为什么我们使用EL表达式的时候，使用的是属性值的原因。 这个问题搞清楚之后，不管是Struts1的BeanUtils参数类型转换，或者是Strut2使用的Interceptor参数类型转换都能触类旁通，想想也知道框架干了哪些事情，是怎么干的了。 最后想说的是Struts的各种书、视频教程很多，大家也都是依葫芦画瓢照做，但是很少有人思考这么基础的问题，学一门东西不是死命的学就能学的好的，应当常常思考。学习学的是方法，每种技术都有它的生命周期，方法永远都是活的，要想在IT的世界活下去，一定要勤于思考，与菜鸟共勉。<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1" color="#FF0000" style="display: block !important; padding: 20px 0 5px !important;">其他文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2008%2F10%2Fel-get-array-length-in-jsp%2F&from=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">EL如何取得数组长度？EL取不了要靠JSTL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F04%2Fhow-to-use-el-in-google-app-engine%2F&from=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">GoogleAppEngine中如何使用EL表达式?</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F&from=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Struts1 RESTFul Style URL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2008%2F10%2Fservlet-jsp-paging%2F&from=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Servlet+JSP分页的一个例子</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>这个问题的场景,相信很多人都写过Struts的helloworld程序,下面是一个常见的hello.jsp页面，请求经由org.apache.struts.action.ActionServlet或者org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter几经处理之后，最后返回ActionForward或者action的result相应的jsp或者各种模板页面。</p>
<p>这个结果页面中本质一直没有变，取得它所需要的值，通常是一个javabean的属性，假设这个javabean是下面的Bean20091228，<span style="color: #ff6600;">那么为什么这里我们写的是属性${message}而不是字段${msg}呢？</span></p>
<pre class="brush: xml; title: ; notranslate">
&lt;%@ page contentType=&quot;text/html; charset=UTF-8&quot; %&gt;

&lt;html&gt;
&lt;head&gt;
    &lt;title&gt; JSP Hello World &lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
 ${message}
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p><span style="color: #ff0000;"><strong>要搞清楚问题首先要区分java中类的字段和属性的区别，对，这是显然有区别的</strong></span></p>
<p><span style="color: #ff6600;">字段：通常是在类中定义的类成员变量<span style="color: #000000;"> </span></span>例如：下面的value和msg</p>
<p><span style="color: #ff6600;">属性：类中读写方法的声明，属性的名字不一定要和实际访问的字段相同 </span>例如：下面的类我们可以说Bean20091228有支持读写的属性message, 属性名称默认小写开头。</p>
<pre class="brush: java; title: ; notranslate">
public class Bean20091228 {

	public Bean20091228(int value, String msg) {
		this.value = value;
		this.msg = msg;
	}
	private int value;
	private String msg;
	public int getVal() {
		return value;
	}
	public void setVal(int value) {
		this.value = value;
	}
	public String getMessage() {
		return msg;
	}
	public void setMessage(String msg) {
		this.msg = msg;
	}
}
</pre>
<p>搞清楚这个问题之后，下面这段代码用反射机制取得这个类的PropertyDescriptor，并且使用反射设置message属性的值,第5行代码检查是否当前迭代属性是message,第7行代码调用该属性的写方法，设置新的值为”hello,reflection”<br />
该程序执行的打印结果为hello,reflection</p>
<pre class="brush: java; highlight: [4,5,7]; title: ; notranslate">
	public static void main(String[] args) throws Exception {
		Bean20091228 bean = new Bean20091228(1,&quot;hello,world&quot;);

		BeanInfo bi = Introspector.getBeanInfo(bean.getClass());
		for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
			if(pd.getName().equals(&quot;message&quot;))
			{
				pd.getWriteMethod().invoke(bean, new Object[]{&quot;hello,reflection&quot;});
			}
		}
		System.out.println(bean.getMessage());
	}
</pre>
<p>所以EL表达式这玩意，在JSP页面解析的时候，技术上是使用reflection机制取得javabean的attribute的值，这就是为什么我们使用EL表达式的时候，使用的是属性值的原因。</p>
<p>这个问题搞清楚之后，不管是Struts1的BeanUtils参数类型转换，或者是Strut2使用的Interceptor参数类型转换都能触类旁通，想想也知道框架干了哪些事情，是怎么干的了。</p>
<p>最后想说的是Struts的各种书、视频教程很多，大家也都是依葫芦画瓢照做，但是很少有人思考这么基础的问题，学一门东西不是死命的学就能学的好的，应当常常思考。学习学的是方法，每种技术都有它的生命周期，方法永远都是活的，要想在IT的世界活下去，一定要勤于思考，与菜鸟共勉。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1" color="#FF0000" style="display: block !important; padding: 20px 0 5px !important;">其他文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2008%2F10%2Fel-get-array-length-in-jsp%2F&from=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">EL如何取得数组长度？EL取不了要靠JSTL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F04%2Fhow-to-use-el-in-google-app-engine%2F&from=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">GoogleAppEngine中如何使用EL表达式?</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2011%2F02%2Fstruts1-restful-style-url%2F&from=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Struts1 RESTFul Style URL</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2008%2F10%2Fservlet-jsp-paging%2F&from=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fwhy-el-use-java-property-instead-of-field%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Servlet+JSP分页的一个例子</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2009/12/why-el-use-java-property-instead-of-field/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

