(二)Struts2的Ajax支持(使用JSON插件实现)

http://prototype.conio.net/dist/

下载(对Ajax支持的prototype--js函数库):

 

http://code.google.com/p/jsonplugin/downloads/list
下载(Struts2的JSON插件):

 jsonplugin-0.25.jar Struts 2 JSON Plugin 0.25


----------------------------------------------------------

第一:手动建立项目结构(类似于MyEclipse创建Web Pro项目的后台操作)


1、新建文件夹结构如下:
  Struts2json
  |______WEB-INF
               |_______classes
               |_______src
               |_______lib

2、复制Tomcat里conf文件夹里的web.xml到WEB-INF文件夹下,并修改web.xml文件
web.xml文件:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app 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"
    version="2.5">
    
</web-app>

 

3、将刚才下载解压后Struts2下的lib文件夹里
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.1.jar
xwork-2.0.4.jar

拷贝到Struts2json下lib文件夹里,并将
jasonplugin-0.25.jar 也拷贝到Struts2json/WEB-INF/lib文件夹下。
prototype-1.4.0.js 拷贝到Struts2json文件夹下。

4、找到Strust2里src\apps\showcase\src\main\resources(就是解压后里面的实例)的struts.xml文件复制到Struts2json下classes文件夹下,并修改struts.xml文件 ,或直接在classes文件夹下新建一个
struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

</struts>

 

5、新建并手写一个build.xml(必须已经安装了Ant工具),并将build.xml放置到WEB-INF文件夹下 (MyEclipse内置了Ant)
build.xml文件:

<?xml version="1.0"?>
<project name="struts" basedir="." default="">

    <path id="classpath">
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="."/>
    </path>

    <target name="compile" description="Compile all source code">
        <javac destdir="classes" debug="true"
            deprecation="false" optimize="false" failοnerrοr="true">
            <src path="src"/>
            <classpath refid="classpath"/>
        </javac>
    </target>

</project>

 

 

总结:目录结构如下
  Struts2t
  |______WEB-INF
               |_______classes
                
             |______struts.xml
               |_______src
               |_______lib
                               |_______ commons-logging-1.0.4.jar
                               |_______ freemarker-2.3.8.jar
                               |_______ ognl-2.6.11.jar
                               |_______ struts2-core-2.0.11.1.jar
                               |_______ xwork-2.0.4.jar              
                               |_______ jsonplugin-0.25.jar
               |_______web.xml
               |_______build.xml
 |______prototype-1.4.0.js

----------------------------------------------------------

第二:编写核心代码

1、Struts2核心就是控制器,为Struts2添加核心Filter配置在web.xml文件中(拦截所有Web请求并由FilterDispatcher初始化)
web.xml文件:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app 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"
    version="2.5">
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.Struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>




2、编写表现层ajaxtest.jsp页面并放在与WEB-INF同一级目录下。
ajaxtest . jsp文件:

<%@ page language="java" contentType="text/html; charset=GBK"%>
<script src="prototype-1.4.0.js" type="text/javascript">
</script>
<script language="JavaScript">
	function gotClick()
	{
		//请求的地址
		var url = 'JSONExample.action';
		//将form1表单域的值转换为请求参数
		var params = Form.serialize('form1');
		//创建Ajax.Request对象,对应于发送请求
		var myAjax = new Ajax.Request(
		url,
		{
			//请求方式:POST
			method:'post',
			//请求参数
			parameters:params,
			//指定回调函数
			onComplete: processResponse,
			//是否异步发送请求
			asynchronous:true
		});
	}
    function processResponse(request)
	{
		$("show").innerHTML = request.responseText;
	}	
</script>
<html>
<head>
<title>使用JSON插件</title>
</head>
<body>
<form id="form1" name="form1" method="post">
<INPUT TYPE="text" name="field1" id="field1"/><br>
<INPUT TYPE="text" name="field2" id="field2"/><br>
<INPUT TYPE="text" name="field3" id="field3"/><br>
<INPUT TYPE="button" value="提交" onClick="gotClick();"/>
</form>
<div id="show">
</div>
</body>
</html>

 

3、编写POJO(Action)在src下新建文件夹org,在org下新建文件夹jee(这里是建立包名),并新建类JSONExample.java放置在src/org/jee文件夹下。

JSONExample . java文件:

package org.jee;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.googlecode.jsonplugin.annotations.JSON;

public class JSONExample
{
    private int[] ints = {10, 20};
    private Map map = new HashMap();
    private String customName = "custom";

    private String field1;
    //'transient'不会被序列化
    private transient String field2;
    //没有setter和getter方法的字段不会被序列化
    private String field3;

    public String execute()
	{
        map.put("name", "yeeku");
        return Action.SUCCESS;
    }

    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }
    public String getField2() {
        return field2;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }

    public String getField3() {
        return field3;
    }

    public void setField3(String field3) {
        this.field3 = field3;
    }

    public int[] getInts() {
        return ints;
    }

    public void setInts(int[] ints) {
        this.ints = ints;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    @JSON(name="newName")
    public String getCustomName() 
	{
        return this.customName;
    }
}



4、在struts.xml里配置Action,修改classes文件
struts.xml文件:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<package name="example"  extends="json-default">
		<action name="JSONExample" class="org.jee.JSONExample">
			<result type="json"/>
		</action>
	</package>

</struts>

注意:

第一:配置

<constant name="struts.i18n.encoding" value="UTF-8"/>

不使用GBK编码,而使用UTF-8编码,因为Ajax的POST请求都以UTF-8的方式进行编码的。

第二:配置包时,继承了json-default包,不再继承默认的default包,因为只有在json-default包下才有json类型的Result。



5、将Struts2t整个文件夹拷贝到Tomcat/webapps文件夹下

总结:目录结构如下
  Struts2t
  |______WEB-INF
               |_______classes
                              |______org
                                           |_____jee
                                                      |______JSONExample.class
                              |______struts.xml

               |_______src
                              |______org
                              |_____jee
                                          |______JSONExample.java
                |_______lib
                              |_______ commons-logging-1.0.4.jar
                              |_______ freemarker-2.3.8.jar
                              |_______ ognl-2.6.11.jar
                              |_______ struts2-core-2.0.11.1.jar
                              |_______ xwork-2.0.4.jar  
                              |_______ jsonplugin-0.25.jar   
               |_______web.xml
               |_______build.xml
 |______prototype-1.4.0.js
 |______ajaxtest.jsp

 

----------------------------------------------------------

三、测试

1、启动Tomcat6

2、http://localhost:8080/struts2t (进行测试)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值