?? 在struts 2中實現ioc - max on java - blogjava.htm
字號:
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.FilterDispatcher<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-mapping</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">url-pattern</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000">/*</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">url-pattern</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">filter-mapping</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">filter-mapping</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
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">url-pattern</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000">/*</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">url-pattern</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">filter-mapping</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">listener</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">listener-class</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
org.springframework.web.context.ContextLoaderListener<BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">listener-class</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">listener</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR><BR> </SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">welcome-file-list</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">welcome-file</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000">index.html</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">welcome-file</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">welcome-file-list</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: #0000ff">></SPAN> </DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">清單1 WEB-INF\web.xml</SPAN>
<P>大家一看便知道,主要是加入Spring的ContextLoaderListener監聽器,方便Spring與Web容器交互。</P>
<P>緊接著,修改Struts.properties文件,告知Struts 2運行時使用Spring來創建對象(如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: #000000">struts.objectFactory</SPAN> <SPAN
style="COLOR: #000000">=</SPAN> <SPAN style="COLOR: #000000">spring</SPAN>
</DIV><SPAN style="FONT-SIZE: smaller; FONT-STYLE: italic">清單2
classes\struts.properties</SPAN>
<P>再下來,遵循Spring的原則——面向接口編程,創建接口ChatService,代碼如下:</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"><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top> <SPAN
style="COLOR: #0000ff">package</SPAN> <SPAN
style="COLOR: #000000">tutorial;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top><BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top></SPAN>
<SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">java.util.Set;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top><BR><IMG
id=Codehighlighter1_71_102_Open_Image
onclick="this.style.display='none'; Codehighlighter1_71_102_Open_Text.style.display='none'; Codehighlighter1_71_102_Closed_Image.style.display='inline'; Codehighlighter1_71_102_Closed_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedBlockStart.gif"
align=top><IMG id=Codehighlighter1_71_102_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_71_102_Closed_Text.style.display='none'; Codehighlighter1_71_102_Open_Image.style.display='inline'; Codehighlighter1_71_102_Open_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ContractedBlock.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">interface</SPAN>
<SPAN style="COLOR: #000000">ChatService </SPAN><SPAN
id=Codehighlighter1_71_102_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/dot.gif"> </SPAN><SPAN
id=Codehighlighter1_71_102_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
Set</SPAN> <SPAN style="COLOR: #000000"><</SPAN> <SPAN
style="COLOR: #000000">String</SPAN> <SPAN style="COLOR: #000000">></SPAN>
<SPAN style="COLOR: #000000">getUserNames();<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedBlockEnd.gif"
align=top>}</SPAN> </SPAN></DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">清單3
tutorial.ChatService.java</SPAN>
<P>然后,再創建一個默認實現ChatServiceImpl,代碼如下:</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"><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top> <SPAN
style="COLOR: #0000ff">package</SPAN> <SPAN
style="COLOR: #000000">tutorial;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top><BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top></SPAN>
<SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">java.util.HashSet;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top></SPAN>
<SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">java.util.Set;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top><BR><IMG
id=Codehighlighter1_120_287_Open_Image
onclick="this.style.display='none'; Codehighlighter1_120_287_Open_Text.style.display='none'; Codehighlighter1_120_287_Closed_Image.style.display='inline'; Codehighlighter1_120_287_Closed_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedBlockStart.gif"
align=top><IMG id=Codehighlighter1_120_287_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_120_287_Closed_Text.style.display='none'; Codehighlighter1_120_287_Open_Image.style.display='inline'; Codehighlighter1_120_287_Open_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ContractedBlock.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">class</SPAN> <SPAN
style="COLOR: #000000">ChatServiceImpl </SPAN><SPAN
style="COLOR: #0000ff">implements</SPAN> <SPAN
style="COLOR: #000000">ChatService </SPAN><SPAN
id=Codehighlighter1_120_287_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/dot.gif"> </SPAN><SPAN
id=Codehighlighter1_120_287_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif"
align=top><BR><IMG id=Codehighlighter1_158_284_Open_Image
onclick="this.style.display='none'; Codehighlighter1_158_284_Open_Text.style.display='none'; Codehighlighter1_158_284_Closed_Image.style.display='inline'; Codehighlighter1_158_284_Closed_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_158_284_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_158_284_Closed_Text.style.display='none'; Codehighlighter1_158_284_Open_Image.style.display='inline'; Codehighlighter1_158_284_Open_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> </SPAN> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN
style="COLOR: #000000">Set</SPAN> <SPAN style="COLOR: #000000"><</SPAN> <SPAN
style="COLOR: #000000">String</SPAN> <SPAN style="COLOR: #000000">></SPAN>
<SPAN style="COLOR: #000000">getUserNames() </SPAN><SPAN
id=Codehighlighter1_158_284_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/dot.gif"> </SPAN><SPAN
id=Codehighlighter1_158_284_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
Set</SPAN> <SPAN style="COLOR: #000000"><</SPAN>
<SPAN style="COLOR: #000000">String</SPAN> <SPAN
style="COLOR: #000000">></SPAN> <SPAN style="COLOR: #000000">users
</SPAN><SPAN style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">new</SPAN> <SPAN
style="COLOR: #000000">HashSet</SPAN> <SPAN style="COLOR: #000000"><</SPAN>
<SPAN style="COLOR: #000000">String</SPAN> <SPAN
style="COLOR: #000000">></SPAN> <SPAN style="COLOR: #000000">();<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
users.add(</SPAN> <SPAN
style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">Max</SPAN> <SPAN
style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">);<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
users.add(</SPAN> <SPAN
style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">Scott</SPAN> <SPAN
style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">);<BR><IMG
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -