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

📄 struts 2与ajax(第一部分) - max on java - blogjava.htm

📁 struts2标签的详细解释说明教程
💻 HTM
📖 第 1 页 / 共 5 页
字号:
style="COLOR: #000000"> title;<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif" 
align=top> &nbsp; &nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" 
align=top><BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp;@Override<BR><IMG id=Codehighlighter1_990_1266_Open_Image 
onclick="this.style.display='none'; Codehighlighter1_990_1266_Open_Text.style.display='none'; Codehighlighter1_990_1266_Closed_Image.style.display='inline'; Codehighlighter1_990_1266_Closed_Text.style.display='inline';" 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif" 
align=top><IMG id=Codehighlighter1_990_1266_Closed_Image style="DISPLAY: none" 
onclick="this.style.display='none'; Codehighlighter1_990_1266_Closed_Text.style.display='none'; Codehighlighter1_990_1266_Open_Image.style.display='inline'; Codehighlighter1_990_1266_Open_Text.style.display='inline';" 
src="Struts 2与AJAX(第一部分) - 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_990_1266_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与AJAX(第一部分) - Max On Java - BlogJava.files/dot.gif"></SPAN><SPAN 
id=Codehighlighter1_990_1266_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp; &nbsp; &nbsp;bookId </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN 
style="COLOR: #000000">15645912</SPAN><SPAN style="COLOR: #000000">;<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp; &nbsp; &nbsp;title </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Max On 
Java</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">;<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp; &nbsp; &nbsp;price </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN 
style="COLOR: #000000">0.9999d</SPAN><SPAN style="COLOR: #000000">;<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp; &nbsp; &nbsp;comments </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN 
style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 
ArrayList</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN 
style="COLOR: #000000">String</SPAN><SPAN 
style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN 
style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">);<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp; &nbsp; &nbsp;comments.add(</SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">It's no 
bad!</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">);<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp; &nbsp; &nbsp;comments.add(</SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">WOW!</SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp; &nbsp; &nbsp;comments.add(</SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">No 
comment!</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">);<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp; &nbsp; &nbsp;secret1 </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">You can't see 
me!</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">;<BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/InBlock.gif" align=top> 
&nbsp; &nbsp; &nbsp; &nbsp;secret2 </SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">I am 
invisible!</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">;<BR><IMG 
src="Struts 2与AJAX(第一部分) - 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与AJAX(第一部分) - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif" 
align=top> &nbsp; &nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG 
src="Struts 2与AJAX(第一部分) - Max On Java - BlogJava.files/ExpandedBlockEnd.gif" 
align=top>}</SPAN></SPAN></DIV><SPAN 
style="FONT-SIZE: smaller; FONT-STYLE: italic">清单1 
src/tutorial/JsonPluginAction.java</SPAN> 
<P>以上代码值得注意的是,通过@JSON的JAVA注释(Annotation),我们可以改变JSON结果的属性名称,另外带有transient修饰符与没有Getter方法的字段(field)都不会被串行化为JSON。</P>
<P>然后,我们来配置一下此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: #0000ff">&lt;?</SPAN><SPAN style="COLOR: #ff00ff">xml 
version="1.0" encoding="UTF-8"</SPAN><SPAN 
style="COLOR: #0000ff">?&gt;</SPAN><SPAN 
style="COLOR: #000000"><BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">&lt;!</SPAN><SPAN style="COLOR: #ff00ff">DOCTYPE struts 
PUBLIC<BR>&nbsp; &nbsp; "-//Apache Software Foundation//DTD Struts Configuration 
2.0//EN"<BR>&nbsp; &nbsp; 
"http://struts.apache.org/dtds/struts-2.0.dtd"</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN 
style="COLOR: #000000"><BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">struts</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"> &nbsp; 
&nbsp;<BR>&nbsp; &nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">package </SPAN><SPAN 
style="COLOR: #ff0000">name</SPAN><SPAN 
style="COLOR: #0000ff">="Struts2_AJAX_DEMO"</SPAN><SPAN style="COLOR: #ff0000"> 
extends</SPAN><SPAN style="COLOR: #0000ff">="json-default"</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">action </SPAN><SPAN 
style="COLOR: #ff0000">name</SPAN><SPAN 
style="COLOR: #0000ff">="JsonPlugin"</SPAN><SPAN style="COLOR: #ff0000"> 
class</SPAN><SPAN 
style="COLOR: #0000ff">="tutorial.JsonPluginAction"</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">result </SPAN><SPAN 
style="COLOR: #ff0000">type</SPAN><SPAN 
style="COLOR: #0000ff">="json"</SPAN><SPAN style="COLOR: #ff0000"> </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">action</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"> &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp;<BR>&nbsp; &nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">&lt;/</SPAN><SPAN 
style="COLOR: #800000">package</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN 
style="COLOR: #0000ff">&lt;/</SPAN><SPAN 
style="COLOR: #800000">struts</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN></DIV><SPAN 
style="FONT-SIZE: smaller; FONT-STYLE: italic">清单2 src/struts.xml</SPAN> 
<P>上面配置文件的“package”元素和以往不同的是,它扩展了“json-default”而不是“struts-default”。“json-default”是在jsonplugin-0.11.jar包里的struts-plugin.xml中定义的。该文件同时定义了“json”的结果类型,有兴趣的朋友可以打开此文件看看。</P>
<P>发布运行应用程序,在浏览器中键入:<A 
href="http://localhost:8080/Struts2_Ajax/JsonPlugin.action">http://localhost:8080/Struts2_Ajax/JsonPlugin.action</A>,出现下载文件对话框,原因是JSON插件将HTTP响应(Response)的MIME类型设为“application/json”。把文件下载下来,用记事本打开,内容如下:</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">{</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">ISBN</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">15645912</SPAN><SPAN 
style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">comments</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">:[</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">It's no bad!</SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">WOW!</SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">No 
comment!</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">],</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">price</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">0.9999</SPAN><SPAN 
style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000"></SPAN><SPAN 
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">title</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN 
style="COLOR: #000000">Max On Java</SPAN><SPAN 
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">}</SPAN></DIV><SPAN 
style="FONT-SIZE: smaller; FONT-STYLE: italic">清单3 例子1输出的JSON串</SPAN> 
<P>当然这还不是一个完整的AJAX的例子,下面让我们写一个HTML文件将其完成,HTML代码如下:</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">&lt;!</SPAN><SPAN style="COLOR: #ff00ff">DOCTYPE html 
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</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: #ff0000">xmlns</SPAN><SPAN 
style="COLOR: #0000ff">="http://www.w3.org/1999/xhtml"</SPAN><SPAN 
style="COLOR: #ff0000"> </SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN><SPAN 
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">head</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">title</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">JSON 
Plugin</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN 
style="COLOR: #800000">title</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">script 
</SPAN><SPAN style="COLOR: #ff0000">type</SPAN><SPAN 
style="COLOR: #0000ff">="text/javascript"</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN 
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> &nbsp; &nbsp;<BR>&nbsp; 
&nbsp; </SPAN><SPAN 
style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">var</SPAN><SPAN 
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> bXmlHttpSupport </SPAN><SPAN 
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN><SPAN 
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> (</SPAN><SPAN 
style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">typeof</SPAN><SPAN 
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> XMLHttpRequest </SPAN><SPAN 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -