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

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

📁 struts2标签的详细解释说明教程
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://www.blogjava.net/max/archive/2006/12/28/90548.html -->
<HTML><HEAD id=Head><TITLE>在Struts 2中实现IoC - Max On Java - BlogJava</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META id=metaKeywords content="在Struts 2中实现IoC" name=keywords><LINK id=MainCss 
href="在Struts 2中实现IoC - Max On Java - BlogJava.files/style.css" type=text/css 
rel=stylesheet><LINK id=RSSLink title=RSS 
href="http://www.blogjava.net/max/rss.aspx" type=application/rss+xml 
rel=alternate>
<META content="MSHTML 6.00.2900.3132" name=GENERATOR></HEAD>
<BODY>
<FORM id=Form1 name=Form1 onsubmit="javascript:return WebForm_OnSubmit();" 
action=90548.html method=post>
<DIV><INPUT id=__EVENTTARGET type=hidden name=__EVENTTARGET> <INPUT 
id=__EVENTARGUMENT type=hidden name=__EVENTARGUMENT> <INPUT 
id="&#13;&#10;__VIEWSTATE" type=hidden name=__VIEWSTATE> </DIV>
<SCRIPT type=text/javascript>
<!--
var theForm = document.forms['Form1'];
if (!theForm) {
    theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</SCRIPT>

<SCRIPT src="在Struts 2中实现IoC - Max On Java - BlogJava.files/WebResource.axd" 
type=text/javascript></SCRIPT>

<SCRIPT language=JavaScript>
									function ctlent(evt,id)
											{
												if(evt.ctrlKey && evt.keyCode == 13)
												{	
													try
													{
														TempSave(id);
													}
													catch(ex)
													{
													}
													finally
													{
													    __doPostBack('AjaxHolder$PostComment$btnSubmit','')
													}
												}
		
												}</SCRIPT>

<SCRIPT language=JavaScript>function SetReplyAuhor(author){document.getElementById('AjaxHolder_PostComment_tbComment').value+="@"+author+"\n";document.getElementById('AjaxHolder_PostComment_tbComment').focus();return false}</SCRIPT>

<SCRIPT src="在Struts 2中实现IoC - Max On Java - BlogJava.files/ScriptResource.axd" 
type=text/javascript></SCRIPT>

<SCRIPT 
src="D:\C_TOOLS\tools\勉強の工具\sql2000 sp4\struts-2.0.9-all\在Struts 2中实现IoC - Max On Java - BlogJava.files\ScriptResource(1).axd" 
type=text/javascript></SCRIPT>

<SCRIPT 
src="D:\C_TOOLS\tools\勉強の工具\sql2000 sp4\struts-2.0.9-all\在Struts 2中实现IoC - Max On Java - BlogJava.files\ScriptResource(2).axd" 
type=text/javascript></SCRIPT>

<SCRIPT type=text/javascript>
<!--
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
// -->
</SCRIPT>
<!--done-->
<DIV id=header><!--done-->
<DIV class=header>
<DIV class=headerText><A class=headermaintitle id=Header1_HeaderTitle 
href="http://www.blogjava.net/max/">Max On Java</A><BR>The value of hands-on 
experience as compared to book learning is smaller in software development than 
in many other fields. </DIV></DIV></DIV>
<DIV id=mytopmenu>
<DIV id=mylinks><!--done--><A class=menu id=MyLinks1_HomeLink 
href="http://www.blogjava.net/">BlogJava</A>&nbsp;&nbsp; <A class=menu 
id=MyLinks1_MyHomeLink href="http://www.blogjava.net/max/">首页</A>&nbsp;&nbsp; <A 
class=menu id=MyLinks1_NewPostLink 
href="http://www.blogjava.net/max/admin/EditPosts.aspx?opt=1">新随笔</A> <A 
class=menu id=MyLinks1_ContactLink 
href="http://www.blogjava.net/max/contact.aspx?id=1">联系</A>&nbsp;&nbsp; <A 
class=menu id=MyLinks1_Syndication 
href="http://www.blogjava.net/max/rss">聚合</A><A id=MyLinks1_XMLLink 
href="http://www.blogjava.net/max/rss"><IMG 
style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" 
src="在Struts 2中实现IoC - Max On Java - BlogJava.files/xml.gif"></A>&nbsp;&nbsp; <A 
class=menu id=MyLinks1_Admin 
href="http://www.blogjava.net/max/admin/EditPosts.aspx">管理</A> </DIV>
<DIV id=mystats><!--done-->随笔-18&nbsp; 评论-753&nbsp; 文章-0&nbsp; trackbacks-0 
</DIV></DIV>
<DIV id=centercontent><!--done-->
<DIV class=post>
<DIV class=postTitle><A class=postTitle2 id=viewpost1_TitleUrl 
href="http://www.blogjava.net/max/archive/2006/12/28/90548.html">在Struts 
2中实现IoC</A> </DIV>
<P>IoC(Inversion of Control,以下译为控制反转)随着Java社区中轻量级容器(Lightweight 
Contianer)的推广而越来越为大家耳熟能详。在此,我不想再多费唇舌来解释“什么是控制反转”和“为什么需要控制反转”。因为互联网上已经有非常多的文章对诸如此类的问题作了精彩而准确的回答。大家可以去读一下Rod 
Johnson和Juergen Hoeller合著的《<A 
href="http://www.wrox.com/books/0764558315.shtml">Expert one-on-one J2EE 
Development without EJB</A>》或Martin Fowler所写的《<A class=l 
onmousedown="return clk(0,'','','res','1','')" 
href="http://www.martinfowler.com/articles/injection.html" target=_blank><SPAN 
style="COLOR: #0000cc">Inversion of Control Containers and the Dependency 
Injection pattern</SPAN></A>》。</P>
<P>言归正传,本文的目的主要是介绍在Struts 2中实现控制反转。</P>
<H2>历史背景</H2>
<P>众所周知,Struts 2是以Webwork 2作为基础发展出来。而在Webwork 
2.2之前的Webwork版本,其自身有一套控制反转的实现,Webwork 2.2在<A 
href="http://www.springframework.org/">Spring 
框架</A>的如火如荼发展的背景下,决定放弃控制反转功能的开发,转由Spring实现。值得一提的是,Spring确实是一个值得学习的框架,因为有越来越多的开源组件(如<A 
href="http://ibatis.apache.org/">iBATIS</A>等)都放弃与Spring重叠的功能的开发。因此,Struts 
2推荐大家通过Spring实现控制反转。</P>
<H2>具体实现</H2>
<P>首先,在开发环境中配置好Struts 2的工程。对这部分仍然有问题的朋友,请参考我的早前的文章。</P>
<P>然后,将所需的Spring的jar包加入到工程的构建环境(Build Path)中,如下图1所示:</P>
<P><IMG alt="图1 所依赖的Spring的jar包" 
src="在Struts 2中实现IoC - Max On Java - BlogJava.files/r_pic1.gif"> <BR><SPAN 
style="FONT-SIZE: smaller; FONT-STYLE: italic">图1 所依赖的Spring的jar包</SPAN> </P>
<P>本文使用的是Spring 
2.0,Spring强烈建议大家在使用其jar包时,只引用需要的包,原因是Spring是一个功能非常强大的框架,其中有些功能是您不需要的;而且Spring提倡的是“按需所取”,而不是EJB的“爱我就要爱我的一切”。当然,如果你怕麻烦或者是不清楚每个包的作用,引用一个Spring的总包也未尝不可。</P>
<P>接下来,就要修改WEB-INF\web.xml文件了,内容为:</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">xml 
version="1.0" encoding="UTF-8"</SPAN> <SPAN style="COLOR: #0000ff">?&gt;</SPAN> 
<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> 
<SPAN style="COLOR: #800000">web-app </SPAN><SPAN 
style="COLOR: #ff0000">version</SPAN> <SPAN style="COLOR: #0000ff">="2.4"</SPAN> 
<SPAN style="COLOR: #ff0000">xmlns</SPAN> <SPAN 
style="COLOR: #0000ff">="http://java.sun.com/xml/ns/j2ee"</SPAN> <SPAN 
style="COLOR: #ff0000"><BR>&nbsp; &nbsp; xmlns:xsi</SPAN> <SPAN 
style="COLOR: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</SPAN> <SPAN 
style="COLOR: #ff0000"><BR>&nbsp; &nbsp; xsi:schemaLocation</SPAN> <SPAN 
style="COLOR: #0000ff">="http://java.sun.com/xml/ns/j2ee&nbsp;<BR>&nbsp; &nbsp; 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> <SPAN 
style="COLOR: #800000">display-name</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000">Struts 2 IoC 
Demo</SPAN> <SPAN style="COLOR: #0000ff">&lt;/</SPAN> <SPAN 
style="COLOR: #800000">display-name</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> <SPAN 
style="COLOR: #800000">filter</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">filter-name</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN 
style="COLOR: #000000">struts-cleanup</SPAN> <SPAN 
style="COLOR: #0000ff">&lt;/</SPAN> <SPAN 
style="COLOR: #800000">filter-name</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">filter-class</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
org.apache.struts2.dispatcher.ActionContextCleanUp<BR>&nbsp; &nbsp; &nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN> <SPAN 
style="COLOR: #800000">filter-class</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">filter</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> 
<SPAN style="COLOR: #000000"><BR><BR>&nbsp; &nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">&lt;</SPAN> <SPAN style="COLOR: #800000">filter</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">filter-name</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000">struts2</SPAN> 
<SPAN style="COLOR: #0000ff">&lt;/</SPAN> <SPAN 
style="COLOR: #800000">filter-name</SPAN> <SPAN 

⌨️ 快捷键说明

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