⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 在struts 2中实现ioc - max on java - blogjava.htm

📁 struts2标签的详细解释说明教程
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<SPAN style="COLOR: #0000ff">&gt;</SPAN> </DIV><SPAN 
style="FONT-SIZE: smaller; FONT-STYLE: italic">清单6 
WEB-INF\applicationContext.xml</SPAN> 
<P>上述代码有二点值得大家注意的:</P>
<OL>
  <LI>Struts 
  2会为每一个请求创建一个Action对象,所以在定义chatAction时,使用scope="prototype"。这样Spring就会每次都返回一个新的ChatAction对象了; 

  <LI>因为ChatServiceImpl被配置为默认的scope(也即是singleton,唯一的),所以在实现时应保证其线程安全(关于编写线程安全的代码的讨论已经超出本文的范围,更超出了本人的能力范围,大家可以参考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,内容如下:</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">&lt;!</SPAN> <SPAN style="COLOR: #ff00ff">DOCTYPE struts 
PUBLIC<BR>&nbsp; &nbsp; &nbsp; &nbsp; "-//Apache Software Foundation//DTD Struts 
Configuration 2.0//EN"<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
"http://struts.apache.org/dtds/struts-2.0.dtd"</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN 
style="COLOR: #0000ff">&lt;</SPAN> <SPAN style="COLOR: #800000">struts</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</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">/&gt;</SPAN> <SPAN style="COLOR: #000000">&nbsp; 
&nbsp;<BR>&nbsp; &nbsp;&nbsp;<BR>&nbsp; &nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">&lt;</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">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; &nbsp; &nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</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">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">&lt;</SPAN> <SPAN style="COLOR: #800000">result</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN 
style="COLOR: #000000">/UserList.jsp</SPAN> <SPAN 
style="COLOR: #0000ff">&lt;/</SPAN> <SPAN style="COLOR: #800000">result</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; &nbsp; &nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN> <SPAN 
style="COLOR: #800000">action</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> 
<SPAN style="COLOR: #000000"><BR>&nbsp; &nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">&lt;/</SPAN> <SPAN style="COLOR: #800000">package</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000">&nbsp; 
&nbsp;<BR></SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN> <SPAN 
style="COLOR: #800000">struts</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> 
</DIV><SPAN style="FONT-SIZE: smaller; FONT-STYLE: italic">清单7 
classes\struts.xml</SPAN> 
<P>这里的Action和平常不同的就是class属性,它对应于Spring所定义的bean的id,而不是它的类全名。</P>
<P>最后,让我们看看/UserList.jsp,内容如下:</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">&lt;%</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">%&gt;</SPAN> <SPAN 
style="COLOR: #000000"><BR></SPAN><SPAN 
style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">&lt;%</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">%&gt;</SPAN> <SPAN 
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> <SPAN 
style="COLOR: #800000">html</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> 
<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> 
<SPAN style="COLOR: #800000">head</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> <SPAN 
style="COLOR: #800000">title</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> 
<SPAN style="COLOR: #000000">User List</SPAN> <SPAN 
style="COLOR: #0000ff">&lt;/</SPAN> <SPAN style="COLOR: #800000">title</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN 
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN> 
<SPAN style="COLOR: #800000">head</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN 
style="COLOR: #000000"><BR><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> 
<SPAN style="COLOR: #800000">body</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> <SPAN 
style="COLOR: #800000">h2</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN 
style="COLOR: #000000">User List</SPAN> <SPAN 
style="COLOR: #0000ff">&lt;/</SPAN> <SPAN style="COLOR: #800000">h2</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> <SPAN 
style="COLOR: #800000">ol</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN 
style="COLOR: #000000"><BR>&nbsp; &nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">&lt;</SPAN> <SPAN style="COLOR: #800000">s:iterator 
</SPAN><SPAN style="COLOR: #ff0000">value</SPAN> <SPAN 
style="COLOR: #0000ff">="userNames"</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; &nbsp; &nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> <SPAN 
style="COLOR: #800000">li</SPAN> <SPAN style="COLOR: #0000ff">&gt;&lt;</SPAN> 
<SPAN style="COLOR: #800000">s:property </SPAN><SPAN 
style="COLOR: #0000ff">/&gt;&lt;/</SPAN> <SPAN style="COLOR: #800000">li</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN> <SPAN 
style="COLOR: #800000">s:iterator</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN> <SPAN 
style="COLOR: #800000">ol</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN 
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN> 
<SPAN style="COLOR: #800000">body</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN 
style="COLOR: #0000ff">&lt;/</SPAN> <SPAN style="COLOR: #800000">html</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</SPAN> </DIV><SPAN 
style="FONT-SIZE: smaller; FONT-STYLE: italic">清单8 /UserList.jsp</SPAN> 
<P>大功告成,分布运行应用程序,在浏览器中键入<A 
href="http://localhost:8080/Struts2_IoC/Chat.action">http://localhost:8080/Struts2_IoC/Chat.action</A>,出现如图2所示页面:</P>
<P><IMG alt="图2 /ListUser.jsp" 
src="在Struts 2中实现IoC - Max On Java - BlogJava.files/r_pic2.gif"> <BR><SPAN 
style="FONT-SIZE: smaller; FONT-STYLE: italic">图2 /ListUser.jsp</SPAN> </P>
<H2>总结</H2>
<P>通过Spring在Struts 2上实现控制反转是强烈推荐的做法,当然您也可以组合其它的实现(如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> 
&nbsp;<A 
href="http://www.blogjava.net/max/admin/EditPosts.aspx?postid=90548">编辑</A>&nbsp;<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中实现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中实现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中实现IoC" 
href="http://www.blogjava.net/max/archive/2006/12/28/90548.html#90551">#</A>&nbsp;<A 
name=90551></A>re: 在Struts 2中实现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>呵呵,支持一个... 偶正在初学 spring...&nbsp;&nbsp;<A 
onclick='return SetReplyAuhor("BeanSoft")' 
href="http://www.blogjava.net/max/archive/2006/12/28/90548.html#post">回复</A>&nbsp;&nbsp;<A 
title=查看该作者发表过的评论 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>&nbsp;&nbsp;<A 
id=AjaxHolder_Comments_CommentList_ctl00_EditLink></A></DIV></DIV></DIV>
<DIV class=feedbackItem>
<DIV class=feedbackListSubtitle><A title="permalink: re: 在Struts 2中实现IoC" 
href="http://www.blogjava.net/max/archive/2006/12/28/90548.html#90622">#</A>&nbsp;<A 
name=90622></A>re: 在Struts 2中实现IoC 2006-12-29 00:52 | <A 
id=AjaxHolder_Comments_CommentList_ctl01_NameLink target=_blank>AlleNny</A><BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -