?? 在struts 2中實(shí)現(xiàn)ioc - max on java - blogjava.htm
字號(hào):
?<!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中實(shí)現(xiàn)IoC - Max On Java - BlogJava</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META id=metaKeywords content="在Struts 2中實(shí)現(xiàn)IoC" name=keywords><LINK id=MainCss
href="在Struts 2中實(shí)現(xiàn)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=" __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中實(shí)現(xiàn)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中實(shí)現(xiàn)IoC - Max On Java - BlogJava.files/ScriptResource.axd"
type=text/javascript></SCRIPT>
<SCRIPT
src="D:\C_TOOLS\tools\勉強(qiáng)の工具\(yùn)sql2000 sp4\struts-2.0.9-all\在Struts 2中實(shí)現(xiàn)IoC - Max On Java - BlogJava.files\ScriptResource(1).axd"
type=text/javascript></SCRIPT>
<SCRIPT
src="D:\C_TOOLS\tools\勉強(qiáng)の工具\(yùn)sql2000 sp4\struts-2.0.9-all\在Struts 2中實(shí)現(xiàn)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> <A class=menu
id=MyLinks1_MyHomeLink href="http://www.blogjava.net/max/">首頁</A> <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">聯(lián)系</A> <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中實(shí)現(xiàn)IoC - Max On Java - BlogJava.files/xml.gif"></A> <A
class=menu id=MyLinks1_Admin
href="http://www.blogjava.net/max/admin/EditPosts.aspx">管理</A> </DIV>
<DIV id=mystats><!--done-->隨筆-18 評(píng)論-753 文章-0 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中實(shí)現(xiàn)IoC</A> </DIV>
<P>IoC(Inversion of Control,以下譯為控制反轉(zhuǎn))隨著Java社區(qū)中輕量級(jí)容器(Lightweight
Contianer)的推廣而越來越為大家耳熟能詳。在此,我不想再多費(fèi)唇舌來解釋“什么是控制反轉(zhuǎn)”和“為什么需要控制反轉(zhuǎn)”。因?yàn)榛ヂ?lián)網(wǎng)上已經(jīng)有非常多的文章對(duì)諸如此類的問題作了精彩而準(zhǔn)確的回答。大家可以去讀一下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中實(shí)現(xiàn)控制反轉(zhuǎn)。</P>
<H2>歷史背景</H2>
<P>眾所周知,Struts 2是以Webwork 2作為基礎(chǔ)發(fā)展出來。而在Webwork
2.2之前的Webwork版本,其自身有一套控制反轉(zhuǎn)的實(shí)現(xiàn),Webwork 2.2在<A
href="http://www.springframework.org/">Spring
框架</A>的如火如荼發(fā)展的背景下,決定放棄控制反轉(zhuǎn)功能的開發(fā),轉(zhuǎn)由Spring實(shí)現(xiàn)。值得一提的是,Spring確實(shí)是一個(gè)值得學(xué)習(xí)的框架,因?yàn)橛性絹碓蕉嗟拈_源組件(如<A
href="http://ibatis.apache.org/">iBATIS</A>等)都放棄與Spring重疊的功能的開發(fā)。因此,Struts
2推薦大家通過Spring實(shí)現(xiàn)控制反轉(zhuǎn)。</P>
<H2>具體實(shí)現(xiàn)</H2>
<P>首先,在開發(fā)環(huán)境中配置好Struts 2的工程。對(duì)這部分仍然有問題的朋友,請參考我的早前的文章。</P>
<P>然后,將所需的Spring的jar包加入到工程的構(gòu)建環(huán)境(Build Path)中,如下圖1所示:</P>
<P><IMG alt="圖1 所依賴的Spring的jar包"
src="在Struts 2中實(shí)現(xiàn)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強(qiáng)烈建議大家在使用其jar包時(shí),只引用需要的包,原因是Spring是一個(gè)功能非常強(qiáng)大的框架,其中有些功能是您不需要的;而且Spring提倡的是“按需所取”,而不是EJB的“愛我就要愛我的一切”。當(dāng)然,如果你怕麻煩或者是不清楚每個(gè)包的作用,引用一個(gè)Spring的總包也未嘗不可。</P>
<P>接下來,就要修改WEB-INF\web.xml文件了,內(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">xml
version="1.0" encoding="UTF-8"</SPAN> <SPAN style="COLOR: #0000ff">?></SPAN>
<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"><</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> xmlns:xsi</SPAN> <SPAN
style="COLOR: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</SPAN> <SPAN
style="COLOR: #ff0000"><BR> xsi:schemaLocation</SPAN> <SPAN
style="COLOR: #0000ff">="http://java.sun.com/xml/ns/j2ee <BR>
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">display-name</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000">Struts 2 IoC
Demo</SPAN> <SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">display-name</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">filter</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">filter-name</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000">struts-cleanup</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">filter-name</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">filter-class</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
org.apache.struts2.dispatcher.ActionContextCleanUp<BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">filter-class</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">filter</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR><BR> </SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">filter</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">filter-name</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000">struts2</SPAN>
<SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">filter-name</SPAN> <SPAN
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -