日期:2014-05-16  浏览次数:20562 次

struts json ajax action 结合

首先,我的例子是从这里改的:

http://www.blogjava.net/max/archive/2007/06/12/123682.html

他用的json的jar包是别人写的,我用的Jar包是struts提供的struts2-json-plugin-2.1.8.1.jar

用到的jar包在附件中。


?

public class AjaxAction extends ActionSupport
{
	private static final long serialVersionUID = -5009042081262340542L;
	private int bookId;
	private String title;
	private double price;
	private List<String> comments;
	private transient String secret1;
	private String secret2;

	@JSON(name = "ISBN")
	public int getBookId()
	{
		return bookId;
	}

	public void setBookId(int bookId)
	{
		this.bookId = bookId;
	}

	public List<String> getComments()
	{
		return comments;
	}

	public void setComments(List<String> comments)
	{
		this.comments = comments;
	}

	public double getPrice()
	{
		return price;
	}

	public void setPrice(double price)
	{
		this.price = price;
	}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	@Override
	public String execute() throws Exception
	{

		bookId = 15645912;
		title = "Max On Java";
		price = 0.9999d;
		comments = new ArrayList<String>(3);
		comments.add("It's no bad!");
		comments.add("WOW!");
		comments.add("No comment!");
		secret1 = "You can't see me!";
		secret2 = "I am invisible!";
		return SUCCESS;
	}

}

?

?以上代码值得注意的是,通过@JSON的JAVA注释(Annotation),我们可以改变JSON结果的属性名称,另外带有transient修饰符与没有Getter方法的字段(field)都不会被串行化为JSON。

struts.xml:

?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="Struts2_AJAX_DEMO" extends="json-default">
		<action name="ajaxAction" class="AjaxAction">
			<result type="json" />
		</action>
	</package>
</struts>    

?上面配置文件的“package”元素和以往不同的是,它扩展了“json-default”而不是“struts-default”。“json-default”是在struts-json-plugin.xxx.jar包里的struts-plugin.xml中定义的。

?

?

发布运行应用程序,在浏览器中键入:http://found.mshome.net:8888/StrutsAjax/ajaxAction,出现下载文件对话框,原因是JSON插件将HTTP响应(Response)的MIME类型设为“application/json”。把文件ajaxAction下载下来,用记事本打开,内容如下:

{"ISBN":15645912,"comments":["It's no bad!","WOW!","No comment!"],"price":0.9999,"title":"Max On Java"}

?

对了,我的web.xml中是这样配置的:

?

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

?

?所以访问链接和上面那个人的例子不太一样。

html页面中这样的:大部分内容都和上面那个人的例子一样,只有url不同。

?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Json.html</title>

		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta htt