关键点:
- json数据格式
- 获取json数据
- 输出json
- 分页
#json数据格式#
datagrid:
{"total":1,"rows":[{"name":"小李"}]}
combobox:
[{"value":"001","text":"小李"},{"value":"002","text":"小红"}]
其他:
#获取json数据#
#输出json#
方式一:PrintWriter
public String getJsonData(){ Map<String,Object> data = new HashMap<String,Object>(); List<Map<String,Object>> personList = new ArrayList<Map<String,Object>>(); Map<String,Object> person = new HashMap<String,Object>(); person.put("name", "小李"); personList.add(person); data.put("total", personList.size()); data.put("rows", personList); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out; try { out = response.getWriter(); out.println(JSONObject.fromObject(data)); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } return null; }
PrintWriter
方式二:stream
片段一
InputStream inputStream; public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String getJsonData(){ Map<String,Object> data = new HashMap<String,Object>(); List<Map<String,Object>> personList = new ArrayList<Map<String,Object>>(); Map<String,Object> person = new HashMap<String,Object>(); person.put("name", "小李"); personList.add(person); data.put("total", personList.size()); data.put("rows", personList); inputStream = new ByteArrayInputStream(JSONObject.fromObject(data).toString().getBytes()); return "inputStream"; }
.java 片段
片段二
<result type="stream" name="inputStream"> <param name="bufferSize">1024*1024</param> <param name="inputName">inputStream</param> </result>
struts.xml 片段
方式三:json
片段一
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.3.15.3</version> </dependency>
pom.xml 片段
片段二
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="pages" namespace="/pages" extends="struts-default,json-default"> <global-results> <result name="json" type="json" > <param name="root">jsonObject</param> </result> </global-results> </package> </struts>
struts-view.xml
片段三
// 返回 JSON Object jsonObject; // 页面跳转 static final String JSON = "json"; public Object getJsonObject() { return jsonObject; } public void setJsonObject(Object jsonObject) { this.jsonObject = jsonObject; }
.java 片段
本实现方式需要引入struts2-json-plugin
- struts.xml 需继承json-default
#分页#
见smse
时间: 2024-12-07 08:43:56