struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)
2013-12-06 15:23
946 查看
JSON主要创建如下两种数据对象:
由JSON格式字符串创建,转换成JavaScript的Object对象;
由JSON格式字符串创建,转换成JavaScript的List或数组链表对象。
更多关于JSON的信息,请参考:JSON概述及其在JavaScript与Java中的应用(整理)
结果如下:
View Code
由JSON格式字符串创建,转换成JavaScript的Object对象;
由JSON格式字符串创建,转换成JavaScript的List或数组链表对象。
更多关于JSON的信息,请参考:JSON概述及其在JavaScript与Java中的应用(整理)
1. JSP页面中将对象转换为JSON字符串提交
1.1 创建JSP文件(convertObject2Json.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="../../js/json2.js"> </script> <script type="text/javascript"> function ajaxTransferText(){ var BigText = document.getElementById("BigText").value; var ajaxTransferObjectRef = new ajaxTransferObject("张三", "密码11", 10, BigText); var JSONString = JSON.stringify(ajaxTransferObjectRef); if (window.ActiveXObject) { myAjaxObject = new ActiveXObject("Microsoft.XMLHTTP"); } else { myAjaxObject = new XMLHttpRequest(); } var urlString = "jsonString=" + JSONString; alert(urlString); myAjaxObject.open("POST", "postJson.action"+"?timestamp=" + new Date().getTime(), true); myAjaxObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); myAjaxObject.send(urlString); } function ajaxTransferObject(username, password, age, BigText){ this.username = username; this.password = password; this.age = age; this.BigText = BigText; } </script> </head> <body> <textarea name="textarea" id="BigText" cols="45" rows="5">需要提交的信息主体...</textarea> <br/> <input type="button" value="提交试试" onclick="ajaxTransferText()"/> </body> </html>
1.2 创建后台处理Action类
package com.clzhang.ssh.demo6; import net.sf.json.JSONObject; import com.opensymphony.xwork2.ActionSupport; /** * 获取前台提交的JSON数据 * @author Administrator * */ public class PostJSONAction extends ActionSupport { public static final long serialVersionUID = 1; private String jsonString; public String getJsonString() { return jsonString; } public void setJsonString(String jsonString) { this.jsonString = jsonString; } public String execute() { System.out.println(jsonString); JSONObject json = JSONObject.fromObject(jsonString); System.out.println("username=" + json.get("username")); System.out.println("username=" + json.get("password")); System.out.println("username=" + json.get("age")); System.out.println("username=" + json.get("BigText")); return null; } }
1.3 修改配置文件struts.xml
<action name="postJson" class="com.clzhang.ssh.demo6.PostJSONAction"> </action>
1.4 测试
打开IE,输入地址:http://127.0.0.1:8080/st/ssh/demo6/convertObject2Json.jsp结果如下:
package com.clzhang.ssh.demo6; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; import net.sf.json.JSONArray; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 返回给前台JSON数据,以四种方式 * @author Administrator * */ public class ReturnJSONAction extends ActionSupport { public static final long serialVersionUID = 1; private List<String> lstString = new ArrayList<String>(); public ReturnJSONAction() { lstString.add("你今天吃了吗?"); lstString.add("吃了多吃点"); lstString.add("没吃回家吃去"); lstString.add("好好学习"); lstString.add("天天向上"); } public String listString() throws IOException { JSONArray json = JSONArray.fromObject(lstString); System.out.println(json.toString()); ServletActionContext.getResponse().setContentType("text/html"); ServletActionContext.getResponse().setCharacterEncoding("utf-8"); ServletActionContext.getResponse().getWriter().printf(json.toString()); ServletActionContext.getResponse().getWriter().flush(); ServletActionContext.getResponse().getWriter().close(); return null; } public String listBean() throws IOException { List<UserInfo> listBean = new ArrayList<UserInfo>(); for(String str: lstString) { UserInfo userInfo1 = new UserInfo(); userInfo1.setUsername(str); userInfo1.setPassword("P" + Math.random()); userInfo1.setAge((int)(Math.random()*10)); userInfo1.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh-mm-ss") .format(new Date())); listBean.add(userInfo1); } JSONArray json = JSONArray.fromObject(listBean); System.out.println(json.toString()); ServletActionContext.getResponse().setContentType("text/html"); ServletActionContext.getResponse().setCharacterEncoding("utf-8"); ServletActionContext.getResponse().getWriter().printf(json.toString()); ServletActionContext.getResponse().getWriter().flush(); ServletActionContext.getResponse().getWriter().close(); return null; } public String mapString() throws Exception { LinkedHashMap<String, String> mapString = new LinkedHashMap<String, String>(); int count = 1; for(String str: lstString) { mapString.put(""+count++, str+count); } JSONArray json = JSONArray.fromObject(mapString); System.out.println(json.toString()); ServletActionContext.getResponse().setContentType("text/html"); ServletActionContext.getResponse().setCharacterEncoding("utf-8"); ServletActionContext.getResponse().getWriter().printf(json.toString()); ServletActionContext.getResponse().getWriter().flush(); ServletActionContext.getResponse().getWriter().close(); return null; } public String mapBean() throws Exception { LinkedHashMap<String, UserInfo> mapString = new LinkedHashMap<String, UserInfo>(); int count = 1; for(String str: lstString) { UserInfo userInfo1 = new UserInfo(); userInfo1.setUsername(str); userInfo1.setPassword("P" + Math.random()); userInfo1.setAge((int)(Math.random()*10)); userInfo1.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh-mm-ss") .format(new Date())); mapString.put(""+count++, userInfo1); } JSONArray json = JSONArray.fromObject(mapString); System.out.println(json.toString()); ServletActionContext.getResponse().setContentType("text/html"); ServletActionContext.getResponse().setCharacterEncoding("utf-8"); ServletActionContext.getResponse().getWriter().printf(json.toString()); ServletActionContext.getResponse().getWriter().flush(); ServletActionContext.getResponse().getWriter().close(); return null; } public String execute() throws IOException { return null; } }
View Code
2.3 修改配置文件struts.xml
<action name="getJson" class="com.clzhang.ssh.demo6.ReturnJSONAction"> </action>
2.4 测试
打开IE,输入地址:http://127.0.0.1:8080/st/ssh/demo6/getJsonFromResp.jsp2.4.1 按下“返回List中存String类型的JSON”按钮
2.4.2 按下“返回List中存Bean类型的JSON”按钮
2.4.3 按下“返回Map中存String类型的JSON”按钮
2.4.4 按下“返回Map中存Bean类型的JSON”按钮
相关文章推荐
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 jsp页面
- 不通过struts2,直接将Json对象转为字符串返回到jsp页面
- Spring MVC 后端获取前端提交的json格式字符串并直接转换成control方法对应的参数对象
- 在action中将字符串、对象、list集合保存到session中,在jsp页面获取
- JSon字符串转JSon对象,使JSP页面直接返回JSON对象
- 工作总结 返回 json 后台对象中,某一个字段设为string 整个对象转换为json 返回到页面中
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- 将后台数据(C#)转换成JSON字符串传到页面转换成json对象存储在JS中
- struts2后台返回json到jsp页面
- 在action中将字符串、对象、list集合保存到值栈中,在jsp页面中获取的方法
- jsp获取后台返回的对象中包含的list以及el获取后台json对象并且解析
- 后台date类型转换为json字符串时,返回前台页面的是long time值问题解决
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及文件上传的部分
- Struts2——将字符串、对象、list集合保存到值栈中,在jsp页面中获取的方法
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及非文件上传的部分
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 serlet配置页面
- spring mvc IE8下JSP页面form表单提交后台不能获取值
- jsp页面获取后台session保存的对象
- Struts2的自定义页面提交对象类型转换
- [C#]Winform后台提交数据且获取远程接口返回的XML数据,转换成DataSet