其实,说到这个主题,总觉得没有什么必要总结。因为它与servlet的处理方式及其相似,几乎是一样的。只不过是增加增加了几行代码而已,先看一下处理代码吧。
Jsp页面采用ajax方式异步发送请求:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ajaxAndStruts2.jsp' starting page</title>
<script type="text/javascript" src="../javascript/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function() {
$("#ok").click(function() {
$.post("../getMyLover.action",{"name":$("#name").val()},function(returnData, status){
/*如果返回结果为JSON格式的时候*/
var data = $(returnData)[0];
var name = data.name;
var age = data.age;
var sex = data.sex;
/*如果返回结果为XML格式的时候*/
/*
var data = $(returnData);
var name = data.find("name").text();
var age = data.find("age").text();
var sex = data.find("sex").text();
*/
$showLover = $("#showLover");
$showLover.text("");
$table = $("<table align = 'center' width = '80%' border = 1></table>");
$table.append("<tr><td>name</td><td>age</td><td>sex</td><tr>");
$table.append("<tr><td>"+name+"</td><td>"+age+"</td><td>"+sex+"</td></tr>");
$showLover.append($table);
},"json");//注意此处的请求内容的格式,如果为JSON则应为“json”,如果为XML则为“xml”
});
});
</script>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<input type="text" id="name">
<input type="button" id="ok" value="select">
<div id = "showLover"></div>
</body>
</html>
==========================---如果请求的为JSON格式的时候,服务器端的处理
方法1:需要加载struts2提供的struts2-json-plugin-2.3.1.2.jar。
此处采用的是注解的方式配置,其对应的XML配置也是蛮简单的,此处不赘述。
package com.dong.struts2;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
//json-default是struts2-json-plugin-2.3.1.2.jar中的struts-plugin.xml中定义的包,它继承自struts-default,则我们则可以直接继承这个包
@ParentPackage("json-default")
//配置Action,此处注意的就是type必须设置为json,如果不设置则会产生错误,同时params只是提供了如何的排除某些属性
@Action(value = "getMyLover", results =
{ @Result(name = "success",type="json",params={"excludeProperties","sex"}) })
public class JsonPluginStruts2 extends ActionSupport
{
private String name;
private String sex;
private int age;
//用来设定在客户端显示的名字
@JSON(name="myLover")
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String execute() throws Exception
{
if ("dongdong".equals(this.name))
{
this.name = "jingjing";
this.age = 21;
this.sex = "girl";
}
else if ("jingjing".equals(this.name))
{
this.name = "dongdong";
this.age = 22;
this.sex = "boy";
}
return SUCCESS;
}
}
那么这样我们就完成了服务器与客户端以JSON格式交换信息。
---------------------------------------------------处理XML
下面看服务器端处理XML的方法:
//代码比较简单,不加注释了
package com.dong.struts2;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.spi.XmlWriter;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import com.dong.util.Lover;
import com.opensymphony.xwork2.ActionSupport;
@Action(value = "getLover")
public class ByJsonTest extends ActionSupport
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String execute() throws Exception
{
Document document = DocumentHelper.createDocument();
Element root = DocumentHelper.createElement("MyLover");
document.setRootElement(root);
Element loverName = root.addElement("name");
Element age = root.addElement("age");
Element sex = root.addElement("sex");
Lover lover = new Lover();
if(name.equals("dongdong"))
{
lover.setName("xiaojing");
lover.setAge(21);
lover.setSex("girl");
}
else if(name.equals("jingjing"))
{
lover.setName("dongdong");
lover.setAge(22);
lover.setSex("boy");
}
loverName.setText(lover.getName());
age.setText(lover.getAge() + "");
sex.setText(lover.getSex());
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml;utf-8");
response.setHeader("cache-control","no-cache");
PrintWriter out = response.getWriter();
XMLWriter write = new XMLWriter(out);
write.write(document);
write.flush();
write.close();
out.flush();
out.close();
//注意此处的范围值,可以为null,但是不能为success、input等
return "none";
}
}