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

📄 struts 2_0的action讲解 - max on java - blogjava.htm

📁 struts2标签的详细解释说明教程
💻 HTM
📖 第 1 页 / 共 5 页
字号:
style="COLOR: #0000ff">&lt;</SPAN> <SPAN style="COLOR: #800000">body</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">h2</SPAN> <SPAN style="COLOR: #0000ff">&gt;&lt;</SPAN> 
<SPAN style="COLOR: #800000">s:property </SPAN><SPAN 
style="COLOR: #ff0000">value</SPAN> <SPAN 
style="COLOR: #0000ff">="message"</SPAN> <SPAN 
style="COLOR: #0000ff">/&gt;&lt;/</SPAN> <SPAN style="COLOR: #800000">h2</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</SPAN> <SPAN 
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN> 
<SPAN style="COLOR: #800000">body</SPAN> <SPAN 
style="COLOR: #0000ff">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN 
style="COLOR: #0000ff">&lt;/</SPAN> <SPAN style="COLOR: #800000">html</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</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> 
&nbsp; &nbsp;</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> 
&nbsp; &nbsp;<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> &nbsp; &nbsp;</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> 
&nbsp; &nbsp; &nbsp; &nbsp;</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> &nbsp; &nbsp;}</SPAN> </SPAN><SPAN style="COLOR: #000000"><BR><IMG 
src="Struts 2_0的Action讲解 - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp;<BR><IMG 
src="Struts 2_0的Action讲解 - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp;@Override&nbsp;<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> &nbsp; &nbsp;</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> 
&nbsp; &nbsp; &nbsp; &nbsp;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> 
&nbsp; &nbsp; &nbsp; &nbsp;</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> &nbsp; &nbsp;}</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">&lt;</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">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</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">&gt;</SPAN> <SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp; &nbsp; &nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN> <SPAN 
style="COLOR: #800000">result</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> 
<SPAN style="COLOR: #000000">/HelloWorld.jsp</SPAN> <SPAN 
style="COLOR: #0000ff">&lt;/</SPAN> <SPAN style="COLOR: #800000">result</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">action</SPAN> <SPAN style="COLOR: #0000ff">&gt;</SPAN> 
<SPAN style="COLOR: #000000"><BR></SPAN><SPAN 
style="COLOR: #0000ff">&lt;/</SPAN> <SPAN style="COLOR: #800000">package</SPAN> 
<SPAN style="COLOR: #0000ff">&gt;</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>&nbsp; &nbsp; 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>&nbsp; &nbsp; 
</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 + -