﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-liuzheng-文章分类-Struts</title><link>http://www.blogjava.net/liuzheng/category/24700.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 06 Sep 2007 17:42:04 GMT</lastBuildDate><pubDate>Thu, 06 Sep 2007 17:42:04 GMT</pubDate><ttl>60</ttl><item><title>Struts核心类理解</title><link>http://www.blogjava.net/liuzheng/articles/142858.html</link><dc:creator>刘铮 </dc:creator><author>刘铮 </author><pubDate>Wed, 05 Sep 2007 04:01:00 GMT</pubDate><guid>http://www.blogjava.net/liuzheng/articles/142858.html</guid><wfw:comment>http://www.blogjava.net/liuzheng/comments/142858.html</wfw:comment><comments>http://www.blogjava.net/liuzheng/articles/142858.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liuzheng/comments/commentRss/142858.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liuzheng/services/trackbacks/142858.html</trackback:ping><description><![CDATA[&nbsp;
<p class="MsoNormal"><span lang="EN-US" style="font-size: 14pt">Struts</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">核心类：</span><span style="font-size: 14pt"> <span lang="EN-US">ActionServlet,RequestProcessor<o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left: 21pt; text-indent: -21pt; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><span lang="EN-US" style="font-size: 14pt; mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">1.<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span lang="EN-US" style="font-size: 14pt">ActionServlet</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">负责</span><span lang="EN-US" style="font-size: 14pt">Struts</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">架构的整个初始化工作，当</span><span lang="EN-US" style="font-size: 14pt">ActionServlet</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实例接受到</span><span lang="EN-US" style="font-size: 14pt">HTTP</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">请求时，在</span><span lang="EN-US" style="font-size: 14pt">doGet()</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span><span lang="EN-US" style="font-size: 14pt">doPost()</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法中都会调用</span><span lang="EN-US" style="font-size: 14pt">process()</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法，</span><span lang="EN-US" style="font-size: 14pt">Process()</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中又会调用</span><span lang="EN-US" style="font-size: 14pt">RequestProcessor</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span><span lang="EN-US" style="font-size: 14pt">process()</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法。</span><span lang="EN-US" style="font-size: 14pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left: 21pt; text-indent: -21pt; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><span lang="EN-US" style="font-size: 14pt; mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">2.<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span lang="EN-US" style="font-size: 14pt">RequestProcessor</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span><span lang="EN-US" style="font-size: 14pt">processActionPerform()</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调用</span><span lang="EN-US" style="font-size: 14pt">action</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span><span lang="EN-US" style="font-size: 14pt">execute()</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法。</span><span lang="EN-US" style="font-size: 14pt">RequestProcessor</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可以根据需要进行扩展，如</span><span lang="EN-US" style="font-size: 14pt">Spring</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span><span lang="EN-US" style="font-size: 14pt">org.springframework.web.struts.DelegatingRequestProcessor</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。扩展后必须在</span><span lang="EN-US" style="font-size: 14pt">Struts</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span><span lang="EN-US" style="font-size: 14pt">Config</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中加入</span><span lang="EN-US" style="font-size: 14pt">&lt;controller processorClass=<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left: 21pt"><span lang="EN-US" style="font-size: 14pt">"</span><span style="font-size: 14pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">ＸＸＸＸＸＸ</span><span lang="EN-US" style="font-size: 14pt">"/&gt;<o:p></o:p></span></p>
<img src ="http://www.blogjava.net/liuzheng/aggbug/142858.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liuzheng/" target="_blank">刘铮 </a> 2007-09-05 12:01 <a href="http://www.blogjava.net/liuzheng/articles/142858.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于Struts的AJAX </title><link>http://www.blogjava.net/liuzheng/articles/136445.html</link><dc:creator>刘铮 </dc:creator><author>刘铮 </author><pubDate>Mon, 13 Aug 2007 09:24:00 GMT</pubDate><guid>http://www.blogjava.net/liuzheng/articles/136445.html</guid><wfw:comment>http://www.blogjava.net/liuzheng/comments/136445.html</wfw:comment><comments>http://www.blogjava.net/liuzheng/articles/136445.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/liuzheng/comments/commentRss/136445.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/liuzheng/services/trackbacks/136445.html</trackback:ping><description><![CDATA[<p><font face="Courier New"><strong>首先，我们仍然是对Struts应用来做配置，仍然是在struts-config,xml文件里做配置，如下：</strong> </font></p>
<p class=code><font face="Courier New">&nbsp;&lt;action type="com.ajax.CheckAction"<br>&nbsp;&nbsp;&nbsp;　　scope="request" path="/ajax/check"&gt;<br>&nbsp;&nbsp;&nbsp;　　&lt;forward name="success" path="/check.jsp"/&gt;<br>&nbsp;&lt;/action&gt;</font></p>
<p><br><strong><font face="Courier New">跟普通的Struts应用的配置一样，只是没有ActionForm的配置。</font><font face="Courier New">下面是Action类：</font></strong></p>
<p class=code><font face="Courier New">&nbsp;package com.ajax;<br>&nbsp;<br>&nbsp;import java.io.PrintWriter;<br>&nbsp;<br>&nbsp;import javax.servlet.http.HttpServletRequest;<br>&nbsp;import javax.servlet.http.HttpServletResponse;<br>&nbsp;<br>&nbsp;import org.apache.struts.action.Action;<br>&nbsp;import org.apache.struts.action.ActionForm;<br>&nbsp;import org.apache.struts.action.ActionForward;<br>&nbsp;import org.apache.struts.action.ActionMapping;<br>&nbsp;import org.apache.struts.action.DynaActionForm;<br>&nbsp;<br>&nbsp;/**<br>&nbsp; * @author Administrator<br>&nbsp; *<br>&nbsp; * TODO To change the template for this generated type comment go to<br>&nbsp; * Window - Preferences - Java - Code Style - Code Templates<br>&nbsp; */<br>&nbsp;public class CheckAction extends Action<br>&nbsp;{<br>&nbsp; public final ActionForward execute(ActionMapping mapping, ActionForm form,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　HttpServletRequest request,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　HttpServletResponse response)<br>&nbsp;&nbsp; throws Exception<br>&nbsp;&nbsp; {<br>　　System.out.println("haha...............................");<br>　　String username= request.getParameter("username");<br>　　System.out.println(username);<br>　　String retn;<br>　　if("educhina".equals(username)) retn = "Can't use the same name with the old use,pls select a difference...";<br>　　else retn = "congraducation!you can use this name....";<br>　　PrintWriter out=response.getWriter();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　out.write(retn);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　out.close();<br>　　return mapping.findForward("success");<br>&nbsp;&nbsp; }<br>&nbsp; public static void main(String[] args)<br>&nbsp; {<br>&nbsp; }<br>&nbsp;}</font></p>
<p><font face="Courier New"><strong>我们可以看到里面的逻辑跟上例中Servlet里的逻辑一样。最后，我们来看看JSP:</strong></font></p>
<p class=code><font face="Courier New">&nbsp;&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;<br>&nbsp;&lt;html&gt;<br>&nbsp;&lt;head&gt;<br>&nbsp;&lt;title&gt;Check.html&lt;/title&gt;<br>&nbsp;<br>&nbsp;&lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;<br>&nbsp;&lt;meta http-equiv="description" content="this is my page"&gt;<br>&nbsp;<br>&nbsp;&lt;!--&lt;link rel="stylesheet" type="text/css" href="./styles.css"&gt;--&gt;<br>&nbsp;<br>&nbsp;&lt;/head&gt;<br>&nbsp;&lt;script type="text/javascript"&gt;<br>&nbsp; var http_request = false;<br>&nbsp; function send_request(url) {//初始化、指定处理函数、发送请求的函数<br>&nbsp;&nbsp; http_request = false;<br>&nbsp;&nbsp; </font><a href="file://开/"><font face="Courier New" color=#000000><u>file://开</u></font></a><font face="Courier New">始初始化XMLHttpRequest对象<br>&nbsp;&nbsp; if(window.XMLHttpRequest) { </font><a href="file://mozilla/"><font face="Courier New" color=#000000><u>file://Mozilla</u></font></a><font face="Courier New"> 浏览器<br>　　http_request = new XMLHttpRequest();<br>　　if (http_request.overrideMimeType) {//设置MiME类别<br>&nbsp;　　http_request.overrideMimeType('text/xml');<br>　　}<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; else if (window.ActiveXObject) { // IE浏览器<br>　　try {<br>&nbsp;　　http_request = new ActiveXObject("Msxml2.XMLHTTP");<br>　　} catch (e) {<br>&nbsp;　　try {<br>&nbsp;&nbsp;　　http_request = new ActiveXObject("Microsoft.XMLHTTP");<br>&nbsp;　　} catch (e) {}<br>　　}<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; if (!http_request) { // 异常，创建对象实例失败<br>　　window.alert("不能创建XMLHttpRequest对象实例.");<br>　　return false;<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; http_request.onreadystatechange = processRequest;<br>&nbsp;&nbsp; // 确定发送请求的方式和URL以及是否同步执行下段代码<br>&nbsp;&nbsp; http_request.open("GET", url, true);<br>&nbsp;&nbsp; http_request.send(null);<br>&nbsp; }<br>&nbsp; // 处理返回信息的函数<br>&nbsp;　　function processRequest() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　if (http_request.readyState == 4) { // 判断对象状态<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　if (http_request.status == 200) { // 信息已经成功返回，开始处理信息<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　alert(http_request.responseText);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　} else { </font><a href="file://页/"><font face="Courier New" color=#000000><u>file://页</u></font></a><font face="Courier New">面不正常<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　alert("您所请求的页面有异常。");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　　}<br>&nbsp;　　}<br>&nbsp; function userCheck() {<br>&nbsp;&nbsp; var f = document.forms[0];<br>&nbsp;&nbsp; var username = f.username.value;<br>&nbsp;&nbsp; if(username=="") {<br>　　window.alert("The user name can not be null!");<br>　　f.username.focus();<br>　　return false;<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; else {<br>　　send_request('ajax/check.do?username='+username);<br>&nbsp;&nbsp; }<br>&nbsp; }<br>&nbsp;<br>&nbsp;&lt;/script&gt;<br>&nbsp;&lt;body&gt;<br>&nbsp; &lt;form name="form1" action="" method="post"&gt;<br>&nbsp;User Name:&lt;input type="text" name="username" value=""&gt;&amp;nbsp;<br>&nbsp;&lt;input type="button" name="check" value="check" onClick="userCheck()"&gt;<br>&nbsp;&lt;input type="submit" name="submit" value="/oblog312/submit"&gt;<br>&nbsp;&lt;/form&gt;<br>&nbsp;&lt;/body&gt;<br>&nbsp;&lt;/html&gt;</font></p>
<p><font face="Courier New">我们可以看到，JSP基本是一样的，除了要发送的url：ajax/check.do?username="+username。&nbsp;</font></p>
<img src ="http://www.blogjava.net/liuzheng/aggbug/136445.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/liuzheng/" target="_blank">刘铮 </a> 2007-08-13 17:24 <a href="http://www.blogjava.net/liuzheng/articles/136445.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>