跟我学AJAX技术——如何应用AJAX实现无刷新查询功能的Web应用示例(第二部分)

  • 格式:doc
  • 大小:337.50 KB
  • 文档页数:12

1.1跟我学AJAX技术——如何应用AJAX实现无刷新查询功能的Web应用示例(第二部分)
1.1.1在项目中添加Struts相关的表单ActionForm组件类
1、程序类名称为searchActionForm
包名称为com.px1987.webajax.actionform.SearchActionForm,如下为创建的示图。

2、在该Web应用中添加一个实现查询功能得Action组件
设置相关的参数:/searchCity,com.px1987.webajax.action.SearchAction
将产生出下面的状态
同时在struts-config.xml中对它进行设置
3、编程该Action类以响应客户端的Get方式的请求
package com.px1987.webajax.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.px1987.webajax.model.*;
import java.io.*;
public class SearchAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
String cityName = request.getParameter("cityName");
try
{
cityName = new String(cityName.getBytes("ISO-8859-1"), "gb2312");
catch (UnsupportedEncodingException e)
{
// TODO 自动生成catch 块
e.printStackTrace();
}
CreateXMLInterface createXML=new CreateXMLBean();
//它代表我们的业务功能方法
String xmtFileResult=createXML.createXMLText(cityName);
response.setContentType("text/xml; charset=GBK");
/**
将Cache-Control 设为no-cache 将确保浏览器不会从缓存相同的URL(包括参数)返回的应答。

*/
response.setHeader("Cache-Control", "no-cache");
PrintWriter out;
try
{
out = response.getWriter();
out.write(xmtFileResult); // out.print(sb.toString());
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}
注意:
重要的是把响应的内容类型设置为text/xml,否则XMLHttpRequest 不会把响应内容解
析成XML DOM。

4、创建业务类的接口和业务类的实现类
(1)业务类的接口CreateXMLInterface,包名称为com.px1987.webajax.model;
(2)在该接口中定义下面的方法
package com.px1987.webajax.model;
public interface CreateXMLInterface
{
public String createXMLText(String inputCityName);
}
(3)业务类的实现类CreateXMLBean,包名称为com.px1987.webajax.model
5、编程该业务组件类
package com.px1987.webajax.model;
public class CreateXMLBean implements CreateXMLInterface
{
public CreateXMLBean()
{
}
/* 产生的XML文档为
<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\"?>
<message>
<data> XXX </data>
<data> XXX </data>
</message>
*/
public String createXMLText(String inputCityName)
StringBuffer oneStringBuffer=new StringBuffer("<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\"?><message>");
//实际应该利用DAO进行数据库访问来获得
oneStringBuffer.append("<data>"+inputCityName+"</data>");
oneStringBuffer.append("<data>"+inputCityName+"</data>");
oneStringBuffer.append("</message>");
return oneStringBuffer.toString();
}
}
1.1.2测试本示例的目前实现的应用效果
1、配置服务器
2、部署本Web项目
3、启动服务器
4、执行该页面
http://127.0.0.1:8080/StrutsAJAXWebApp/index.jsp
5、将产生出下面的结果
6、注意中文乱码的解决的方法
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
request.setCharacterEncoding("gb2312"); //对异步请求无效
String cityName = request.getParameter("cityName");
cityName = new String(cityName.getBytes("ISO-8859-1"), "gb2312");
//。

}
7、再执行,将不会出现中文乱码
8、理解和体验“无刷新”的效果
尽管也是get提交,但与正常的get提交的不同点在于浏览器的URL地址栏中不出现查询字符串。

同时在服务器的控制台中产生出下面的结果
9、如果用户没有输入,则出现下面的错误提示
10、如果出现运行时的错误,提示为。