?? 在struts 2中實現ioc - max on java - blogjava.htm
字號:
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">Bob</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>
</SPAN> <SPAN style="COLOR: #0000ff">return</SPAN>
<SPAN style="COLOR: #000000">users;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN style="COLOR: #000000"><BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
<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">清單4
tutorial.ChatServiceImpl.java</SPAN>
<P>接下來,就該新建Action了。tutorial.ChatAction.java的代碼如下:</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
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top></SPAN>
<SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">com.opensymphony.xwork2.ActionSupport;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/None.gif" align=top><BR><IMG
id=Codehighlighter1_135_529_Open_Image
onclick="this.style.display='none'; Codehighlighter1_135_529_Open_Text.style.display='none'; Codehighlighter1_135_529_Closed_Image.style.display='inline'; Codehighlighter1_135_529_Closed_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedBlockStart.gif"
align=top><IMG id=Codehighlighter1_135_529_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_135_529_Closed_Text.style.display='none'; Codehighlighter1_135_529_Open_Image.style.display='inline'; Codehighlighter1_135_529_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">ChatAction </SPAN><SPAN
style="COLOR: #0000ff">extends</SPAN> <SPAN style="COLOR: #000000">ActionSupport
</SPAN><SPAN id=Codehighlighter1_135_529_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_135_529_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
</SPAN> <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">static</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">final</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">long</SPAN> <SPAN
style="COLOR: #000000">serialVersionUID </SPAN><SPAN
style="COLOR: #000000">=</SPAN> <SPAN style="COLOR: #000000"></SPAN><SPAN
style="COLOR: #000000">8445871212065L</SPAN> <SPAN
style="COLOR: #000000">; <BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
</SPAN> <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN
style="COLOR: #000000">ChatService chatService;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
</SPAN> <SPAN style="COLOR: #0000ff">private</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">userNames;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif"
align=top><BR><IMG id=Codehighlighter1_323_360_Open_Image
onclick="this.style.display='none'; Codehighlighter1_323_360_Open_Text.style.display='none'; Codehighlighter1_323_360_Closed_Image.style.display='inline'; Codehighlighter1_323_360_Closed_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_323_360_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_323_360_Closed_Text.style.display='none'; Codehighlighter1_323_360_Open_Image.style.display='inline'; Codehighlighter1_323_360_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"></SPAN><SPAN style="COLOR: #0000ff">void</SPAN> <SPAN
style="COLOR: #000000">setChatService(ChatService chatService) </SPAN><SPAN
id=Codehighlighter1_323_360_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_323_360_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
</SPAN> <SPAN style="COLOR: #0000ff">this</SPAN>
<SPAN style="COLOR: #000000">.chatService </SPAN><SPAN
style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #000000">chatService;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN style="COLOR: #000000"><BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
<BR><IMG id=Codehighlighter1_399_422_Open_Image
onclick="this.style.display='none'; Codehighlighter1_399_422_Open_Text.style.display='none'; Codehighlighter1_399_422_Closed_Image.style.display='inline'; Codehighlighter1_399_422_Closed_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_399_422_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_399_422_Closed_Text.style.display='none'; Codehighlighter1_399_422_Open_Image.style.display='inline'; Codehighlighter1_399_422_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_399_422_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_399_422_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
</SPAN> <SPAN style="COLOR: #0000ff">return</SPAN>
<SPAN style="COLOR: #000000">userNames;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN style="COLOR: #000000"><BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
@Override<BR><IMG id=Codehighlighter1_462_525_Open_Image
onclick="this.style.display='none'; Codehighlighter1_462_525_Open_Text.style.display='none'; Codehighlighter1_462_525_Closed_Image.style.display='inline'; Codehighlighter1_462_525_Closed_Text.style.display='inline';"
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_462_525_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_462_525_Closed_Text.style.display='none'; Codehighlighter1_462_525_Open_Image.style.display='inline'; Codehighlighter1_462_525_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">String execute() </SPAN><SPAN
id=Codehighlighter1_462_525_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_462_525_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
userNames </SPAN><SPAN
style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #000000">chatService.getUserNames();<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
</SPAN> <SPAN style="COLOR: #0000ff">return</SPAN>
<SPAN style="COLOR: #000000">SUCCESS;<BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN style="COLOR: #000000"><BR><IMG
src="在Struts 2中實現IoC - Max On Java - BlogJava.files/InBlock.gif" align=top>
<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">清單5
tutorial.ChatAction.java</SPAN>
<P>ChatAction類使用屬性(Getter/Setter)注入法取得ChatService對象。</P>
<P>然后,配置Spring的applicationContext.xml(位于WEB-INF下)文件,內容如下:</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">beans </SPAN><SPAN
style="COLOR: #ff0000">xmlns</SPAN> <SPAN
style="COLOR: #0000ff">="http://www.springframework.org/schema/beans"</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://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">bean </SPAN><SPAN style="COLOR: #ff0000">id</SPAN> <SPAN
style="COLOR: #0000ff">="chatService"</SPAN> <SPAN
style="COLOR: #ff0000">class</SPAN> <SPAN
style="COLOR: #0000ff">="tutorial.ChatServiceImpl"</SPAN> <SPAN
style="COLOR: #0000ff">/></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">bean </SPAN><SPAN style="COLOR: #ff0000">id</SPAN> <SPAN
style="COLOR: #0000ff">="chatAction"</SPAN> <SPAN
style="COLOR: #ff0000">class</SPAN> <SPAN
style="COLOR: #0000ff">="tutorial.ChatAction"</SPAN> <SPAN
style="COLOR: #ff0000">scope</SPAN> <SPAN
style="COLOR: #0000ff">="prototype"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">property </SPAN><SPAN style="COLOR: #ff0000">name</SPAN>
<SPAN style="COLOR: #0000ff">="chatService"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">ref </SPAN><SPAN
style="COLOR: #ff0000">local</SPAN> <SPAN
style="COLOR: #0000ff">="chatService"</SPAN> <SPAN
style="COLOR: #ff0000"></SPAN><SPAN style="COLOR: #0000ff">/></SPAN> <SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">property</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">bean</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">beans</SPAN>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -