?? 在struts 2中實(shí)現(xiàn)ioc - max on java - blogjava.htm
字號:
<SPAN style="COLOR: #0000ff">></SPAN> </DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">清單6
WEB-INF\applicationContext.xml</SPAN>
<P>上述代碼有二點(diǎn)值得大家注意的:</P>
<OL>
<LI>Struts
2會為每一個請求創(chuàng)建一個Action對象,所以在定義chatAction時,使用scope="prototype"。這樣Spring就會每次都返回一個新的ChatAction對象了;
<LI>因?yàn)镃hatServiceImpl被配置為默認(rèn)的scope(也即是singleton,唯一的),所以在實(shí)現(xiàn)時應(yīng)保證其線程安全(關(guān)于編寫線程安全的代碼的討論已經(jīng)超出本文的范圍,更超出了本人的能力范圍,大家可以參考Addison
Wesley Professional出版的《<A
href="http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601">Java
Concurrency in Practice</A>》)。 </LI></OL>
<P>接下來,在classes/struts.xml中配置Action,內(nèi)容如下:</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"><!</SPAN> <SPAN style="COLOR: #ff00ff">DOCTYPE struts
PUBLIC<BR> "-//Apache Software Foundation//DTD Struts
Configuration 2.0//EN"<BR>
"http://struts.apache.org/dtds/struts-2.0.dtd"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">struts</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">include </SPAN><SPAN style="COLOR: #ff0000">file</SPAN>
<SPAN style="COLOR: #0000ff">="struts-default.xml"</SPAN> <SPAN
style="COLOR: #0000ff">/></SPAN> <SPAN style="COLOR: #000000">
<BR> <BR> </SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">package
</SPAN><SPAN style="COLOR: #ff0000">name</SPAN> <SPAN
style="COLOR: #0000ff">="Struts2_IoC"</SPAN> <SPAN
style="COLOR: #ff0000">extends</SPAN> <SPAN
style="COLOR: #0000ff">="struts-default"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">action </SPAN><SPAN style="COLOR: #ff0000">name</SPAN>
<SPAN style="COLOR: #0000ff">="Chat"</SPAN> <SPAN
style="COLOR: #ff0000">class</SPAN> <SPAN
style="COLOR: #0000ff">="chatAction"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">result</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000">/UserList.jsp</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">result</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">action</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">package</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000">
<BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">struts</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
</DIV><SPAN style="FONT-SIZE: smaller; FONT-STYLE: italic">清單7
classes\struts.xml</SPAN>
<P>這里的Action和平常不同的就是class屬性,它對應(yīng)于Spring所定義的bean的id,而不是它的類全名。</P>
<P>最后,讓我們看看/UserList.jsp,內(nèi)容如下:</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; BACKGROUND-COLOR: #ffff00"><%</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">@ page contentType</SPAN>
<SPAN style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">text/html;
charset=UTF-8</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"></SPAN><SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #ffff00"><%</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">@ taglib prefix</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">s</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">uri</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">/struts-tags</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"></SPAN><SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">html</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"><</SPAN>
<SPAN style="COLOR: #800000">head</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">title</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000">User List</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">title</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN>
<SPAN style="COLOR: #800000">head</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR><BR></SPAN><SPAN style="COLOR: #0000ff"><</SPAN>
<SPAN style="COLOR: #800000">body</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">h2</SPAN> <SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000">User List</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">h2</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">ol</SPAN> <SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">s:iterator
</SPAN><SPAN style="COLOR: #ff0000">value</SPAN> <SPAN
style="COLOR: #0000ff">="userNames"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">li</SPAN> <SPAN style="COLOR: #0000ff">><</SPAN>
<SPAN style="COLOR: #800000">s:property </SPAN><SPAN
style="COLOR: #0000ff">/></</SPAN> <SPAN style="COLOR: #800000">li</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">s:iterator</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">ol</SPAN> <SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN>
<SPAN style="COLOR: #800000">body</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">html</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> </DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">清單8 /UserList.jsp</SPAN>
<P>大功告成,分布運(yùn)行應(yīng)用程序,在瀏覽器中鍵入<A
href="http://localhost:8080/Struts2_IoC/Chat.action">http://localhost:8080/Struts2_IoC/Chat.action</A>,出現(xiàn)如圖2所示頁面:</P>
<P><IMG alt="圖2 /ListUser.jsp"
src="在Struts 2中實(shí)現(xiàn)IoC - Max On Java - BlogJava.files/r_pic2.gif"> <BR><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">圖2 /ListUser.jsp</SPAN> </P>
<H2>總結(jié)</H2>
<P>通過Spring在Struts 2上實(shí)現(xiàn)控制反轉(zhuǎn)是強(qiáng)烈推薦的做法,當(dāng)然您也可以組合其它的實(shí)現(xiàn)(如Pico等)。</P>
<DIV class=postDesc>posted on 2006-12-28 17:37 <A
href="http://www.blogjava.net/max/">Max</A> 閱讀(5395) <A
href="http://www.blogjava.net/max/archive/2006/12/28/90548.html#Post">評論(74)</A>
<A
href="http://www.blogjava.net/max/admin/EditPosts.aspx?postid=90548">編輯</A> <A
href="http://www.blogjava.net/max/AddToFavorite.aspx?id=90548">收藏</A> <A
href="http://www.blogjava.net/max/services/trackbacks/90548.aspx">引用</A> 所屬分類:
<A href="http://www.blogjava.net/max/category/16130.html">Struts 2.0系列</A>
</DIV></DIV><IMG height=1
src="在Struts 2中實(shí)現(xiàn)IoC - Max On Java - BlogJava.files/90548.jpg" width=1> <!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
rdf:about="http://www.blogjava.net/max/archive/2006/12/28/90548.html"
dc:identifier="http://www.blogjava.net/max/archive/2006/12/28/90548.html"
dc:title="在Struts 2中實(shí)現(xiàn)IoC"
trackback:ping="http://www.blogjava.net/max/services/trackbacks/90548.aspx" />
</rdf:RDF>
-->
<SCRIPT type=text/javascript>
//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('AjaxHolder$scriptmanager1', document.getElementById('Form1'));
Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tAjaxHolder$UpdatePanel1'], [], [], 90);
//]]>
</SCRIPT>
<DIV id=AjaxHolder_UpdatePanel1><A
name=pagedcomment></A><!--done--><BR><B>評論:</B>
<DIV class=feedbackNoItems></DIV>
<DIV class=feedbackItem>
<DIV class=feedbackListSubtitle><A title="permalink: re: 在Struts 2中實(shí)現(xiàn)IoC"
href="http://www.blogjava.net/max/archive/2006/12/28/90548.html#90551">#</A> <A
name=90551></A>re: 在Struts 2中實(shí)現(xiàn)IoC 2006-12-28 17:44 | <A
id=AjaxHolder_Comments_CommentList_ctl00_NameLink
href="http://www.blogjava.net/beansoft/" target=_blank>BeanSoft</A><BR>
<DIV align=left>呵呵,支持一個... 偶正在初學(xué) spring... <A
onclick='return SetReplyAuhor("BeanSoft")'
href="http://www.blogjava.net/max/archive/2006/12/28/90548.html#post">回復(fù)</A> <A
title=查看該作者發(fā)表過的評論 href="http://www.blogjava.net/comment?author=BeanSoft"
target=_blank>更多評論</A><BR><A id=AjaxHolder_Comments_CommentList_ctl00_DeleteLink
href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl00$DeleteLink','')"></A> <A
id=AjaxHolder_Comments_CommentList_ctl00_EditLink></A></DIV></DIV></DIV>
<DIV class=feedbackItem>
<DIV class=feedbackListSubtitle><A title="permalink: re: 在Struts 2中實(shí)現(xiàn)IoC"
href="http://www.blogjava.net/max/archive/2006/12/28/90548.html#90622">#</A> <A
name=90622></A>re: 在Struts 2中實(shí)現(xiàn)IoC 2006-12-29 00:52 | <A
id=AjaxHolder_Comments_CommentList_ctl01_NameLink target=_blank>AlleNny</A><BR>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -