`
SevenRedCity
  • 浏览: 60931 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

(二)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" failonerror="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 (进行测试)

 

5
2
分享到:
评论
2 楼 lk617238688 2010-09-25  
学习! 
1 楼 e_sky 2008-07-10  
  学习了...

相关推荐

    Struts2 + jQuery+JSON 实现ajax

    1、将struts2的json插件加入web工程的lib,jsonplugin的下载地址:http://code.google.com/p/jsonplugin/downloads/list

    struts2的json插件配置详解(附demo)

    为了方便ajax调用传输数据,在struts2中加入的json插件用来做对象的序列化和反序列化,json插件的下载地址 http://code.google.com/p/jsonplugin/ 1. 下载json插件包,将jar包拷贝到WEB-INF/lib目录 注:struts2...

    Struts2之ajax初析的并结合jquery一个例子

    Web2.0的随波逐流,Ajax那是大放异彩,Struts2框架自己整合了对Ajax的原生支持(struts 2.1.7+,之前的版本可以通过插件实现),框架的整合只是使得JSON的创建变得异常简单,并且可以简单的融入到Struts2框架中,...

    Struts 2的JSON插件

    Struts 2并没有开发新的AJAX框架,而是使用时下Java EE平台中比较流行的AJAX框架——Dojo和DWR。 最近在Musachy Barroso等同志的无私奉献下,开发了Struts 2的JSON插件(Plugin),极大地方便了我们输出JSON结果...

    struts2 spring2 hibernate3 ajax实现的一个注册登录实例

    演示功能包括: ...使用Struts2的JSON插件完成与Ajax的交互 实现校验用户名功能 压缩包中包括sql文件 修改reg_login\WEB-INF路径下的applicationContext.xml文件,将其中的数据库密码修改为你的数据库密码

    Struts2返回JSON对象

    将jsonplugin-0.30.jar放到工程的lib下面,然后struts2在struts.xml的配置文件中可以指定返回的结果集的类型为:json,把返回的结果封装为json字符串JSON插件是Struts2的Ajax插件,通过利用JSON插件,允许开发者以...

    struts2与json的整合

    JSON插件是Struts2的Ajax插件,通过利用JSON插件,允许开发者以非常灵活的方式开发AJAX应用。 struts2在配置文件中可以指定返回的结果集的类型为:json.

    struts2.0_Ajax

    Struts 2其中的一个重要的功能(Feature)就是“First-class AJAX support - Add interactivity and flexibility with AJAX tags that look and feel just like standard Struts tags(大意:一流的AJAX支持——通过...

    struts2与Ajax.pdf

    介绍struts2中的AJAX插件json的应用。

    Struts2+Spring+Hibernate+Ehcache+AJAX+JQuery+Oracle 框架集成用户登录注册Demo工程

    2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。 4.在服务端分页查询功能,优点:实时性:跳页才查询。数据量小:只加载当前页的记录...

    struts项目学习笔记

    Struts2 是一个非常优秀的MVC框架,基于Model2...整合Ajax支持:json插件 Struts2的今生前世: 1.早期开发模型Servlet+JSP+JavaBean显得力不从心: 流程凌乱、数据传递无序、缺乏辅助功能。 2.MVC模式的轻量级Web应

    json 介绍及资料

    json 资料介绍 struts2A 运用json插件实现 Ajax功能

    Struts2 in action中文版

    8.1.1 页面上:如何使用自定义结果组件构建Struts 2 Ajax应用程序 171 8.1.2 实现JSON结果类型 173 8.2 常用的结果类型 180 8.2.1 RequestDispatcher,也叫做dispatcher 180 8.2.2 ServletRedirectResult,也叫做...

    Struts2和Ajax数据交互示例详解

    我们从Web 2.0的随波逐流,Ajax的大放异彩说起,Struts2框架自己整合了对Ajax的原生支持(struts 2.1.7+,之前的版本可以通过插件实现),框架的整合只是使得JSON的创建变得异常简单,并且可以简单的融入到Struts2...

    struts 2.0与ajax 的实例

    struts 2.0与ajax结合的实例以及原理讲解。返回数据使用JSON形式,使用的是google 的ajax插件。 里面外附一份javascript内核系列的教程。

    Struts2&JQuery 新闻发布

    自己用Struts2 结合Struts2的JSON插件做的AJAX新闻发布,有无刷新添加、修改、删除新闻的功能以及一个简易的自动完成、查询功能,经过自己测试在M$IE和FF上面运行没有问题,希望大家能有有所收获,如果有什么问题...

    低清版 大型门户网站是这样炼成的.pdf

    3.7.4 基于json插件的ajax实现 185 3.8 小结 190 第4章 orm中间件名流hibernate 3接管持久层 191 4.1 orm简介 191 4.1.1 持久化与持久层 191 4.1.2 jdbc劣势 192 4.1.3 实体域模型与关系数据模型 193 4.1.4 ...

    SSH(Struts2.2.1+Hibernate3.6+Spring3.0.5)+json框架包

    并且已经包含了jquery,json等插件,可直接使用Ajax功能!由于文件大小限制,jar包中删除了hibernate的核心jar,需要大家自己下个添加,其余jar包已经完整!测试例子已经删除。需要手动编写。出错的话自己检查代码。...

    jsonplugin

    struts2 的AJAX 插件

Global site tag (gtag.js) - Google Analytics