package com.util;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
public class JsonUtils {
String string="true";
boolean systemOut=false;
Map<String,Object> map = new HashMap<String,Object>();
/*将Object对象转换成为json数组对象,如将数组转换为[{a1},{a2},{a3}]*/
public static JSONArray getJsonArrayFromObject(Object o){
return JSONArray.fromObject(o);
}
/*将Object对象转换成为json对象*/
public static JSONObject getJsonObjectFromObject(Object o){
return JSONObject.fromObject(o);
}
public void sendJson(){
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(JSONObject.fromObject(getMap()));
if(this.systemOut)
System.out.println(JSONObject.fromObject(getMap()).toString());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendJson(Object o){
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=utf-8");
PrintWriter out = response.getWriter();
if ( o instanceof Object[] || o instanceof List )
out.print(JSONArray.fromObject(o).toString());
else
out.print(JSONObject.fromObject(o).toString());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//增加返回类型 json 或�? html
public void sendJson(Object o,String type){
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/"+type+";charset=utf-8");
PrintWriter out = response.getWriter();
if ( o instanceof Object[] || o instanceof List )
out.print(JSONArray.fromObject(o).toString());
else
out.print(JSONObject.fromObject(o).toString());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendJson(List list){
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(getJsonArrayFromObject(list));
if(this.systemOut)
System.out.println(getJsonArrayFromObject(list).toString());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendJson(Map map){
this.map=map;
sendJson();
}
public void sendJsonForGrid(List list){
try {
map.put("total", list.size());
map.put("rows",list);
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(JSONObject.fromObject(getMap()));
if(this.systemOut)
System.out.println(JSONObject.fromObject(getMap()));
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendString(){
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(getString());
if(this.systemOut)
System.out.println(getString());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendString(String msg){
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(msg);
if(this.systemOut)
System.out.println(msg);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendSuccess(){
map.clear();
map.put("success","true");
map.put("msg","操作成功");
sendJson(map);
}
public void sendSuccess(String msg){
map.clear();
map.put("success","true");
map.put("msg","操作成功"+msg);
sendJson(map);
}
/**
* @author :张斌,2011/12/19
* 变更:对map多添加一个key-value(message:操作失败!)
* 原因:与MessageResult中的属�?message�?��
*/
public void sendFailure(){
map.clear();
map.put("success","false");
map.put("msg","操作失败");
map.put("message","操作失败");
sendJson(map);
}
public void sendFailure(String msg){
map.clear();
map.put("success","false");
map.put("msg","操作失败"+msg);
sendJson(map);
}
public void sendJsonForDzsw(Object o){
map.clear();
map.put("data",o);
map.put("success", true);
sendJson(map);
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public boolean isSystemOut() {
return systemOut;
}
public void setSystemOut(boolean systemOut) {
this.systemOut = systemOut;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
jquery autoComplete+struts2+mysql5.5
共35个文件
jar:7个
xml:4个
js:4个
4星 · 超过85%的资源 需积分: 9 61 下载量 90 浏览量
2012-12-19
14:09:52
上传
评论 2
收藏 1.76MB RAR 举报
温馨提示
本实例用jquery autoComplete结合struts2和mysql数据库整合。 实现自动补全功能,封装方便使用。 包含数据库脚本。
资源推荐
资源详情
资源评论
收起资源包目录
autoComplete.rar (35个子文件)
autoComplete
src
com
auto
AutoCompleteAction.java 3KB
util
JsonUtils.java 5KB
struts.xml 384B
.classpath 1KB
.mystrutsdata 240B
.mymetadata 301B
.settings
com.genuitec.eclipse.j2eedt.core.prefs 1KB
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 200B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 466B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 395B
.myeclipse
test.sql 2KB
.project 2KB
WebRoot
META-INF
MANIFEST.MF 36B
images
indicator.gif 2KB
js
jquery-1.7.2.js 256KB
autocommon.js 3KB
jquery-1.7.2.min.js 93KB
jquery.autocomplete.js 22KB
index.jsp 2KB
css
jquery.autocomplete.css 910B
WEB-INF
classes
com
auto
AutoCompleteAction.class 3KB
util
JsonUtils.class 6KB
struts.xml 384B
lib
mysql-connector-java-5.0.5-bin.jar 501KB
commons-logging-1.1.1.jar 59KB
commons-lang-2.5.jar 273KB
commons-beanutils-1.8.3.jar 227KB
json-lib-2.4-jdk15.jar 155KB
ezmorph-1.0.6.jar 84KB
commons-collections-3.1.jar 546KB
web.xml 714B
共 35 条
- 1
wgcking-231
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页