struts2下实现的json传递list,object。
必须的jar:
java bean:
package upload.progress.action; public class music {
private String name;
private String singer;
private String source;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
} }
struts2.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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" /> <include file="example.xml"/> <package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
--> <package name="default" namespace="/Fileupload" extends="struts-default"> <action name="practice" class="action.PracticeAction" method="practice_one">
<result type="redirect" name="success">
/js/uploadify.jsp
</result>
</action> </package>
<!-- Add packages here -->
<package name="ajax" namespace="/ajax" extends="json-default"> <action name="ajaxTest1" class="action.ajaxTest1" method="ajaxTest">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param> </result>
</action>
<action name="json" class="upload.progress.action.jsonTest1" method="returnMessage">
<result name="success" type="json"></result>
</action>
<action name="jsonMusic" class="upload.progress.action.jsonTest1" method="returnMusic">
<result name="success" type="json"></result>
</action>
<action name="jsonMusicList" class="upload.progress.action.jsonTest1" method="returnMusicList">
<result name="success" type="json">
<param name="root">musicList</param>
</result>
</action>
</package> </struts>
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
<base href="<%=basePath%>"> <title>My JSP 'jsonTest1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<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">
-->
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#showMessage").click(function(){ $.ajax({
type: "POST",
dataType: "json",
url: "ajax/json.action",
success: function(data){
$("#message").html("<font color='red'>"+data.message+"</font>");
}
});
}); $("#showMusic").click(function(){
$.ajax({
type : "POST",
dataType : "json",
url : "ajax/jsonMusic.action",
success : function(data){
$.each(data,function(i,item){
if(item!=null){
alert(item.name);
}
})
}
});
}); $("#showList").click(function(){
$.ajax({
type : "POST",
dataType : "json",
url : "ajax/jsonMusicList.action",
success : function(data){
$.each(data,function(i,item){
if(item!=null){
alert(item.name);
}
})
}
});
});
});
</script>
</head> <body>
<table align="center">
<tr>
<td id="showMessage">Message</td>
<td id="showMusic">showMusic</td>
<td id="showList">showList</td>
</tr> </table>
<div id="message"></div>
</body>
</html>
action:
package upload.progress.action; import java.util.ArrayList;
import java.util.List; import org.json.JSONArray; import com.opensymphony.xwork2.ActionSupport; public class jsonTest1 extends ActionSupport {
/**
* 处理json的action
*/
private static final long serialVersionUID = -1519192049194890338L;
private String message;
private music Music;
private List<music> musicList; public String returnMessage(){
System.out.println("json invoking");
message="json 字符串";
return SUCCESS;
} public String returnMusic(){
Music =new music();
Music.setName("忘情水");
Music.setSinger("刘德华");
Music.setSource("百度");
return SUCCESS;
}
public String returnMusicList(){
musicList=new ArrayList<music>();
music Music1 =new music();
Music1.setName("忘情水1");
Music1.setSinger("刘德华1");
Music1.setSource("百度1"); music Music2 =new music();
Music2.setName("忘情水2");
Music2.setSinger("刘德华2");
Music2.setSource("百度2"); music Music3 =new music();
Music3.setName("忘情水3");
Music3.setSinger("刘德华3");
Music3.setSource("百度3");
musicList.add(Music1);
musicList.add(Music2);
musicList.add(Music3); return SUCCESS;
} public String jsonArray(){
List<String> list=new ArrayList<String>();
JSONArray jsonArray=new JSONArray(list); return SUCCESS;
}
public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public music getMusic() {
return Music;
} public void setMusic(music music) {
Music = music;
} public List<music> getMusicList() {
return musicList;
} public void setMusicList(List<music> musicList) {
this.musicList = musicList;
} public static long getSerialversionuid() {
return serialVersionUID;
} }
使用流的方式:
action:
package action; import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction; import Util.HibernateUtil; import com.opensymphony.xwork2.ActionSupport; public class ajaxTest1 extends ActionSupport {
/**
* 判断姓名是否存在
*/
private static final long serialVersionUID = 1L;
private InputStream inputStream;
private String name;
private String location;
@SuppressWarnings("unchecked")
public String ajaxTest() throws UnsupportedEncodingException{
if(name.equals("")){
inputStream=new ByteArrayInputStream("姓名不可以是空值哦。".getBytes("utf-8"));
System.out.println("AJAX invoing-------------name="+name);
}else{
//inputStream=new ByteArrayInputStream("姓名可以使用".getBytes("utf-8"));
SessionFactory sessionFactory= HibernateUtil.getSessionFactory();
Session session=sessionFactory.getCurrentSession();
Transaction tr= session.beginTransaction();
String hql="from domain.user where name='"+name+"' ";
Query query=session.createQuery(hql);
List<domain.user> list=query.list();
//System.out.println(list.get(0).getName()); if(!list.isEmpty()){
inputStream=new ByteArrayInputStream("姓名已存在。".getBytes("utf-8"));
}else{
inputStream=new ByteArrayInputStream("姓名可以使用。".getBytes("utf-8"));
}
session.clear();
session.flush();
tr.commit();
}
return SUCCESS;
}
public InputStream getInputStream() {
return inputStream;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public static long getSerialversionuid() {
return serialVersionUID;
} }
jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
<base href="<%=basePath%>"> <title>ajaxTest1</title> <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">
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery.js"></script>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript"> $(function(){
alert();
$("#examine").focusout(function(){ test();
}); }); function test(){
$.ajax({
type: "POST",
url: "ajax/ajaxTest1.action",
data: "name="+$("#examine").val(),
success: function(msg){
alert( "Data Saved: " + msg );
} });
}
</script> </head> <body>
<form action="" method="post" enctype="multipart/form-data"></form>
<table>
<tr>
<td>姓名:</td>
<td></td>
<td>密码:</td>
</tr>
<tr>
<td> <input name="username" type="text" maxlength="40" id="examine"></td>
<td></td>
<td> <input name="password" type="password" maxlength="40" ></td>
</tr>
</table> </body>
</html>
struts2下实现的json传递list,object。的更多相关文章
- Json传递数据两种方式(json大全)
1.Json传递数据两种方式(json大全)----------------------------字符串 var list1 = ["number","name&quo ...
- Poco::JSON::Array 中object 设置preserveInsertionOrder 时,stringify出错-->深入解析
在使用poco version 1.6.0时 Poco::JSON::Array 在object 设置preserveInsertionOrder =true 时 调用 array.stringif ...
- [Asp.net mvc]Asp.net mvc 使用Json传递数据
在之前的练习中一直是直接传递的Model到后台或是单个数据到后台,今天在使用中遇到了点问题,不能使用Model传递到后台,但又要实现多个数据到后台,实验了多次有了以下的解决方案,给自己留个笔记. 功能 ...
- 通过json传递图片(base64编码)
程序一: 后台代码: public ActionResult Index() { FileStream fs = new FileStream("e:\\file\\psb.jpg" ...
- xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cycle” in ARC-enabled code
xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cyc ...
- JSON: JavaScript Object Notation
JSON是JavaScript Object Notation 的缩写,是JS提供的一种数据交换格式.1) JSON对象本质上就是一个JS对象,但是这个对象比较特殊,它可以直接转换为字符串,在不同语言 ...
- android跟服务器使用json传递数据
最近在做项目,使用了json传递数据,把服务器对象转换成json字符串返回,android使用gson包解析json字符串变成对象. 1.服务器代码编写,我这边是在servlet里面 Peron pe ...
- IE8、IE9浏览器下报:JSON未定义 解决方法
IE8.IE9浏览器下报:JSON未定义的问题 解决方法: 在jsp中引入如下代码 <!-- 解决 IE8.IE9 下显示混乱的问题--><% String browserStrin ...
- 玩转Web之Json(四)---json与(Object/List/Map)的相互转化
在做web应用时,经常需要将json转化成Object/list/map或者将Object/List/map转化成json,通过简单封装可以在写代码是减轻很多负担.本文将给出json转化的一系列方法. ...
随机推荐
- IOS开发小项目—找色块游戏
1.项目代码: @interface NextViewController () { int r ;//色块层数的全局变量 int m;//后面用于tag值的变化 UIView *view;//色块 ...
- 【jmter】逻辑控制器(Logic Controller)
1. Jmeter官网对逻辑控制器的解释是:“Logic Controllers determine the order in which Samplers are processed.”.意思是说, ...
- SPOJ #692. Fruit Farm
Another palindrome related problem. Actually nothing too theoretical here, but please keep following ...
- bzoj1760 [Baltic2009]Triangulation
给定一个多边形的三角剖分(n<=1e5),且每个三角形有其颜色,问最多可以把这个三角剖分分成几个联通的部分,使任何一种颜色不出现在多个连通块中 建出三角剖分对应的树,同种颜色的点之间的路径是不能 ...
- 【springmvc】之常用的注解
原理这里不叙述,只讲怎么用 1. spring mvc中的@PathVariable是用来获得请求url中的动态参数的 @RequestMapping(value="/user/{userI ...
- libcurl上传文件,添加自定义头
原文 http://www.cnblogs.com/meteoric_cry/p/4285881.html 主题 curl libcurl参数很多,一不小心就容易遇到问题.曾经就遇到过一个很蛋疼的问 ...
- ASP.net中网站访问量统计方法代码(在线人数,本月访问,本日访问,访问流量,累计访问)
一.建立一个数据表IPStat用于存放用户信息 我在IPStat表中存放的用户信息只包括登录用户的IP(IP_Address),IP来源(IP_Src)和登录时间 (IP_DateTime),些表的信 ...
- 解决AD域认证问题—“未知的身份验证机制”
场景: Ad认证登录系统,之前正常.不知服务器调了什么,导致无法登录.提示信息如标题. 解决方案: DirectoryEntry adRoot = new DirectoryEntry("L ...
- Python 正则表达式-OK
Python正则表达式入门 一. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分. 正则表达式是用于处理字符串的强大工具, 拥有自己独特的语法以及一个独立的处理引擎, 效率上 ...
- IntentService简介
参照文章:http://android.tgbus.com/Android/tutorial/201106/355229.shtml IntentService是Service类的子类,用来处理异步请 ...