Struts2+JSON+JQUERY DEMO
看到别人用了Struts2和JSON,自己也想练练手。记录下练习过程中遇到的问题,以便参考。
使用Maven新建项目:
先挂上pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sk</groupId>
<artifactId>struts</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>struts Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Log -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-beta7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0-beta7</version>
</dependency> <!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-taglib</artifactId>
<version>1.3.10</version>
</dependency> <!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency> <!-- JSON LIB -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
</dependency>
<!-- 如果缺少这个插件的话机会报: Unable to find parent packages json-default 这个错误折磨了我大半天,后来自己仔细看看tomcate启动日志发现了这个错误,经查询,是缺少了
struts2-json-plugin-2.1.8.1.jar 这个包,因为struts2默认是以插件形式进行加载的,所以是少加载的json处理的包。加入这个包运行正常。如果没有这个包的话。
运行时会报There is no Action mapped for namespace·······所有的Action都不能运行。 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.15</version>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build> </project>
再整个VO:User.java
package com.sk.struts2.bean;
import java.io.Serializable;
public class User implements Serializable {
private String id;
private String username;
private String pwd;
// 省略setter和getter
}
然后是action:JSONAction.java
package com.sk.struts2.action; import net.sf.json.JSONObject; import com.opensymphony.xwork2.ActionSupport;
import com.sk.struts2.bean.User; public class JSONAction extends ActionSupport { private static final long serialVersionUID = -795596058695827298L; private User user;
private String jsonString; @Override
public String execute() throws Exception {
JSONObject jsonObject = null;
if(user != null){
jsonObject = JSONObject.fromObject(user);
jsonString = jsonObject.toString();
System.out.println(jsonString);
}
return SUCCESS;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public String getJsonString() {
return jsonString;
} public void setJsonString(String jsonString) {
this.jsonString = jsonString;
} }
接着是View:json.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World!</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="<%= request.getContextPath() %>/resources/js/json.js"></script>
</head>
<body>
<form>
ID:<input type="text" name="user.id"/><br />
username:<input type="text" name="user.username"/><br />
pwd:<input type="text" name="user.pwd"/> <input id="btn" type="button" value="submit" />
</form> <h2>Here is result:</h2>
<div id="result"></div>
</body>
</html>
还有JS:json.js
$(function(){
$('#btn').click(function(){
var params=$("input").serialize();
var actionPath = getRootPath() + "/jsonTest/jsonAction";
$.ajax({
url: actionPath,
// 数据发送方式
type: "post",
// 接受数据格式
dataType : "json",
// 要传递的数据
data : params,
// 回调函数,接受服务器端返回给客户端的值,即result值
success : show
}).always(function(){alert('done');});
});
});
function show(result){
//测试result是否从服务器端返回给客户端
//alert(result);
//解析json对象
var json = eval("("+result+")");
var obj = "编号: "+json.id+" 用户名: "+json.username+" 密码: "+json.pwd;
$("#result").html(obj);
}
//js获取项目根路径,如: http://localhost:8083/uimcardprj
function getRootPath(){
//获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath=window.document.location.href;
//获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
var pathName=window.document.location.pathname;
var pos=curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8083
var localhostPaht=curWwwPath.substring(0,pos);
//获取带"/"的项目名,如:/uimcardprj
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
return(localhostPaht+projectName);
}
最后奉上: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> <constant name="struts.devMode" value="true" /> <package name="test" namespace="/jsonTest" extends="json-default">
<action name="jsonAction" class="com.sk.struts2.action.JSONAction">
<result type="json">
<!-- 此处将reslut的值返回给客户端,root的值对应要返回的值的属性result.注意:root为固定写法,否则不会把result的值返回给客户端 -->
<param name="root">jsonString</param>
</result>
</action>
</package> </struts>
ok,all done.下面测试哈:
最后给个目录结构:

OK.Enjoy!
Struts2+JSON+JQUERY DEMO的更多相关文章
- Struts2+json+hignchart(简单柱状图实现--适合jquery小白)
做了一个简单的基于Struts2 + Json + HighChart的小例子,费了一下午+晚上的时间,虽然简单,但对于我这种Jquery+Ajax小白的人还是很值得记录的. 哈哈哈 # 0. 关键点 ...
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(转载汇总)
使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互 使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——aja ...
- 22、JSON/jQuery上
1)掌握JSON及其应用 2)了解jQuery的背景和特点 3)理解js对象和jQuery对象的区别 4)掌握jQuery九类选择器及应用(上) 声明:今天服务端我们使用Struts2技术 一 ...
- Flex+Struts2+JSON实现Flex和后台的HTTP Service请求
http://www.fengfly.com/plus/view-191093-1.html Flex+Struts2+JSON的后台代码我在这就不多说了.不懂得请看我写的上一篇文章<Strut ...
- Jqgrid入门-结合Struts2+json实现数据展示(五)
DEMO用的是ssh框架实现的,具体怎么搭建的就不多做说明了.分页表格的数据操作难点就是数据展现.至于增删改直接用hibernate原生的方法实现即可. 初步分析:表格要实现分页,那么 ...
- struts2实现jQuery的异步交互
struts2中jQuery的异步交互有两种方式: 1)是利用构造字符串的方式来实现: 使用该方法主要是在服务器端根据前端的请求,返回一个字符串信息,然后前端的jQuery通过解析该字符串信息得到对应 ...
- Struts2+AJAX+JQuery 实现用户登入与注册功能。
要求 必备知识 JAVA/Struts2,JS/JQuery,HTML/CSS基础语法. 开发环境 MyEclipse 10 演示地址 演示地址 预览截图(抬抬你的鼠标就可以看到演示地址哦): 关于U ...
- Struts2+AJAX+JQuery 实现用户登入与注册功能
要求:必备知识:JAVA/Struts2,JS/JQuery,HTML/CSS基础语法:开发环境:MyEclipse 10 关于UI部分请查看下列链接,有详细制作步骤: 利用:before和:afte ...
- Struts2 整合jQuery实现Ajax功能(1)
技术领域非常多东西流行,自然有流行的道理.这几天用了jQuery,深感有些人真是聪明绝顶,能将那么多技术融合的如此完美. 首先明白个概念: jQuery是什么:是使用javascript语言开发的,用 ...
随机推荐
- Posix 共享内存区
要点 与mmap配合使用 open与shm_open的区别,open打开磁盘上的普通文件,shm_open创建和打开的文件在/dev/shm文件夹下,该文件夹对应的是内存 gcc编译时加参数-lrt ...
- 基于UUID生成短ID
为什么需要短ID 数据库操作过程最常用到: 自增ID UUID 前者多数依赖Mysql的auto_increment,但数据移植麻烦. 如果是主从或主主,不同库里自增ID还可能不一致. 后者长度是个问 ...
- using 语句中使用的类型必须可隐式转换为“System.IDisposable”
在entity framework 中错误 using 语句中使用的类型必须可隐式转换为“System.IDisposable” 的错误. 原因是: 没有引用 EntityFramework 这个程序 ...
- “Microsoft Visual Studio遇到了问题,需要关闭”解决办法
运行VS2008,打开项目,弹出错误界面 . 解决办法:将项目中的所有设计窗体关闭并保存,重新打开就OK~
- ruby 格式化当前日期时间
ruby 格式化当前日期时间 ruby 用Time类获取当前时间. t = Time.new puts t 可以看到输出的是(我现在运行的时间): Sat Jan 29 10:45:22 +0800 ...
- 【转】android如何浏览并选择图片 音频 视频
转自:http://www.cnblogs.com/top5/archive/2012/03/06/2381986.html 这几天 在学习并开发android系统的图片浏览 音频 视频 的浏览 ...
- 无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent
无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent 在Flex Application里,是不能直接用addChild添加Sprite ...
- 1515 跳 - Wikioi
题目描述 Description邪教喜欢在各种各样空间内跳.现在,邪教来到了一个二维平面.在这个平面内,如果邪教当前跳到了(x,y),那么他下一步可以选择跳到以下4个点:(x-1,y), (x+1,y ...
- MAC 13信道
房东的路由器一直连不上,手机却能连上,前天设置了13信道,后来又忘了,最后找到个连接WIFI的方法,在网络偏好设置里选择向导,诊断中可以连上wifi.
- .htaccess文件讲解
.htaccess文件(或者"分布式配置文件")提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作为用户 ...