?? struts 2_0的action講解 - max on java - blogjava.htm
字號:
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">body</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">h2</SPAN> <SPAN style="COLOR: #0000ff">><</SPAN>
<SPAN style="COLOR: #800000">s:property </SPAN><SPAN
style="COLOR: #ff0000">value</SPAN> <SPAN
style="COLOR: #0000ff">="message"</SPAN> <SPAN
style="COLOR: #0000ff">/></</SPAN> <SPAN style="COLOR: #800000">h2</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN>
<SPAN style="COLOR: #800000">body</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">html</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> </DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">例1 HelloWorld.jsp</SPAN> <BR><BR>
<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_0的Action講解 - 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_0的Action講解 - Max On Java - BlogJava.files/None.gif"
align=top><BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/None.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">java.text.DateFormat;<BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/None.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">java.util.Date;<BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/None.gif"
align=top><BR><IMG
src="Struts 2_0的Action講解 - 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_0的Action講解 - Max On Java - BlogJava.files/None.gif"
align=top><BR><IMG id=Codehighlighter1_165_389_Open_Image
onclick="this.style.display='none'; Codehighlighter1_165_389_Open_Text.style.display='none'; Codehighlighter1_165_389_Closed_Image.style.display='inline'; Codehighlighter1_165_389_Closed_Text.style.display='inline';"
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/ExpandedBlockStart.gif"
align=top><IMG id=Codehighlighter1_165_389_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_165_389_Closed_Text.style.display='none'; Codehighlighter1_165_389_Open_Image.style.display='inline'; Codehighlighter1_165_389_Open_Text.style.display='inline';"
src="Struts 2_0的Action講解 - 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">HelloWorld </SPAN><SPAN
style="COLOR: #0000ff">extends</SPAN> <SPAN style="COLOR: #000000">ActionSupport
</SPAN><SPAN id=Codehighlighter1_165_389_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_0的Action講解 - Max On Java - BlogJava.files/dot.gif"> </SPAN><SPAN
id=Codehighlighter1_165_389_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/InBlock.gif" align=top>
</SPAN> <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN
style="COLOR: #000000">String message;<BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/InBlock.gif" align=top>
<BR><IMG id=Codehighlighter1_222_243_Open_Image
onclick="this.style.display='none'; Codehighlighter1_222_243_Open_Text.style.display='none'; Codehighlighter1_222_243_Closed_Image.style.display='inline'; Codehighlighter1_222_243_Closed_Text.style.display='inline';"
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_222_243_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_222_243_Closed_Text.style.display='none'; Codehighlighter1_222_243_Open_Image.style.display='inline'; Codehighlighter1_222_243_Open_Text.style.display='inline';"
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> </SPAN> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN
style="COLOR: #000000">String getMessage() </SPAN><SPAN
id=Codehighlighter1_222_243_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_0的Action講解 - Max On Java - BlogJava.files/dot.gif"> </SPAN><SPAN
id=Codehighlighter1_222_243_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/InBlock.gif" align=top>
</SPAN> <SPAN style="COLOR: #0000ff">return</SPAN>
<SPAN style="COLOR: #000000">message;<BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN style="COLOR: #000000"><BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/InBlock.gif" align=top>
<BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/InBlock.gif" align=top>
@Override <BR><IMG id=Codehighlighter1_284_387_Open_Image
onclick="this.style.display='none'; Codehighlighter1_284_387_Open_Text.style.display='none'; Codehighlighter1_284_387_Closed_Image.style.display='inline'; Codehighlighter1_284_387_Closed_Text.style.display='inline';"
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_284_387_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_284_387_Closed_Text.style.display='none'; Codehighlighter1_284_387_Open_Image.style.display='inline'; Codehighlighter1_284_387_Open_Text.style.display='inline';"
src="Struts 2_0的Action講解 - 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_284_387_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_0的Action講解 - Max On Java - BlogJava.files/dot.gif"> </SPAN><SPAN
id=Codehighlighter1_284_387_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/InBlock.gif" align=top>
message </SPAN><SPAN style="COLOR: #000000">=</SPAN>
<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #000000">"</SPAN> <SPAN
style="COLOR: #000000">Hello World, Now is </SPAN><SPAN
style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000"></SPAN><SPAN
style="COLOR: #000000">+</SPAN> <SPAN
style="COLOR: #000000">DateFormat.getInstance().format(</SPAN> <SPAN
style="COLOR: #0000ff">new</SPAN> <SPAN style="COLOR: #000000">Date());<BR><IMG
src="Struts 2_0的Action講解 - 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_0的Action講解 - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN style="COLOR: #000000"><BR><IMG
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/ExpandedBlockEnd.gif"
align=top>}</SPAN> </SPAN></DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">例1
classes/tutorial/HelloWorld.java</SPAN> <BR><BR>
<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: #800000">package
</SPAN><SPAN style="COLOR: #ff0000">name</SPAN> <SPAN
style="COLOR: #0000ff">="ActionDemo"</SPAN> <SPAN
style="COLOR: #ff0000">extends</SPAN> <SPAN
style="COLOR: #0000ff">="struts-default"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">action </SPAN><SPAN style="COLOR: #ff0000">name</SPAN>
<SPAN style="COLOR: #0000ff">="HelloWorld"</SPAN> <SPAN
style="COLOR: #ff0000">class</SPAN> <SPAN
style="COLOR: #0000ff">="tutorial.HelloWorld"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">result</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000">/HelloWorld.jsp</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">result</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">action</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">package</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> </DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">例1 classes/struts.xml中HelloWorld
Action的配置</SPAN> <BR><BR>
<P>在瀏覽器地址欄中鍵入<A
href="http://localhost:8080/Struts2_Action/HelloWorld.action">http://localhost:8080/Struts2_Action/HelloWorld.action</A>,可以看到如圖1所示頁面。</P><IMG
alt="圖1 HelloWorld輸出頁面"
src="Struts 2_0的Action講解 - Max On Java - BlogJava.files/r_img_1.gif"> <BR><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">圖1 HelloWorld輸出頁面</SPAN>
<BR><BR>參考JavaDoc,可知<A
href="http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/ActionSupport.html">ActionSupport</A>類實現了接口:
<UL>
<LI><A
href="http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/Action.html">com.opensymphony.xwork2.Action</A>
<LI><A
href="http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/LocaleProvider.html">com.opensymphony.xwork2.LoaleProvider</A>
<LI><A
href="http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/TextProvider.html">com.opensymphony.xwork2.TextProvider</A>
<LI><A
href="http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/Validateable.html">com.opensymphony.xwork2.Validateable</A>
<LI><A
href="http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/ValidationAware.html">com.opensymphony.xwork2.ValidationAware</A>
<LI>com.uwyn.rife.continuations.ContinuableObject
<LI><A
href="http://java.sun.com/j2se/1.4/docs/api/java/io/Serializable.html">java.io.Searializable</A>
<LI><A
href="http://java.sun.com/j2se/1.4/docs/api/java/lang/Cloneable.html">java.lang.Cloneable</A></LI></UL>
<P>默認情況下,當請求HelloWorld.action發生時,Struts運行時(Runtime)根據struts.xml里的Action映射集(Mapping),實例化tutoiral.HelloWorld類,并調用其execute方法。當然,我們可以通過以下兩種方法改變這種默認調用。這個功能(Feature)有點類似Struts
1.x中的LookupDispathAction。</P>
<OL>
<LI>在classes/sturts.xml中新建Action,并指明其調用的方法;
<LI>訪問Action時,在Action名后加上“!xxx”(xxx為方法名)。</LI></OL>
<P>實現方法請參考例2:</P>在classes/tutorial/HelloWorld.java中加入以下方法:<BR>
<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">public</SPAN><SPAN style="COLOR: #000000"> String
aliasAction() {<BR> message </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"></SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">自定義Action調用方法</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR>
</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">
SUCCESS;<BR>}</SPAN></DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">例2
classes/tutorial/HelloWorld.java代碼片段</SPAN><BR><BR>實現方法一,在classes/sturts.xml中加入下面代碼:<BR>
<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
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -