zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Struts2 + Json + Jstl 节选自 《Netkiller Java 手札》

JAVAJSONJSON struts2 JSTL 手札
2023-09-14 08:59:51 时间

Struts 配置文件

 package name="information" extends="main" namespace="/inf" 

 action name="Information" 

 result type="tiles" information /result 

 /action 

 /package 

Action 文件

package cn.netkiller.action;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.StringReader;

import java.net.URL;

import java.net.URLConnection;

import java.nio.charset.Charset;

import javax.json.Json;

import javax.json.JsonObject;

import javax.json.JsonReader;

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ActionSupport;

public class Infomation extends ActionSupport{

 private static final long serialVersionUID = 1L;


String URL = "http://www.example.com/list/json/93/20/0.html"; System.out.printf("%s Requeted URL is %s", this.getClass().getName(), URL); StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null; try { URL url = new URL(URL); urlConn = url.openConnection(); if (urlConn != null) urlConn.setReadTimeout(60 * 1000); if (urlConn != null urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); if (bufferedReader != null) { int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); bufferedReader.close(); in.close(); jsonString = sb.toString(); System.out.println(jsonString); JsonReader reader = Json.createReader(new StringReader(jsonString)); JsonObject jsonObject = reader.readObject(); this.setJsonObject(jsonObject); reader.close(); // System.out.println(jsonObject.size()); /*for (int i = 0; i jsonObject.size() - 2; i++) { JsonObject rowObject = jsonObject.getJsonObject(Integer.toString(i)); // System.out.println(rowObject.toString()); System.out.printf("%s\t%s\t%s\n", rowObject.getJsonString("id"), rowObject.getJsonString("title"), rowObject.getJsonString("ctime"));
} catch (Exception e) { throw new RuntimeException("Exception while calling URL:" + URL, e); return Action.SUCCESS; public JsonObject getJsonObject() { return jsonObject; public void setJsonObject(JsonObject jsonObject) { this.jsonObject = jsonObject; public String getJsonString() { return jsonString; public void setJsonString(String jsonString) { this.jsonString = jsonString; }

JSP 文件

 %@ page language="java" contentType="text/html; charset=UTF-8"

 pageEncoding="UTF-8"% 

 %@ taglib prefix="s" uri="/struts-tags"% 

 %@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"% 

 table 

 c:forEach items="${jsonObject.entrySet()}" var="json"

 varStatus="status" 

 c:if test="${not status.last}" 

 td ${json.key+1} /td 

 td ${json.value.getJsonString("id")} /td 

 td ${json.value.getJsonString("title")} /td 

 td ${json.value.getJsonString("ctime")} /td 

 /tr 

 /c:if 

 /c:forEach 

 /table 

===================

 c:forEach items="${jsonObject.entrySet()}" var="json" 

 ${json.value.toString()}

 /c:forEach 

===========

 s:property value="jsonString" / 

解决双引号问题

 %@ page language="java" contentType="text/html; charset=UTF-8"

 pageEncoding="UTF-8"% 

 %@ taglib prefix="s" uri="/struts-tags"% 

 %@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"% 

 %@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"% 

 table 

 c:forEach items="${jsonObject.entrySet()}" var="json"

 varStatus="status" 

 c:if test="${not status.last}" 

 c:set var="id"

 value="${fn:replace(json.value.getJsonString(id),\", )}" / 

 c:set var="title" value="${fn:replace(json.value.getJsonString(title),\", )}" / 

 c:set var="ctime"

 value="${fn:replace(json.value.getJsonString(ctime),\", )}" / 

 td c:out value="${status.count}" / /td 

 td a href="/inf/Detail.do?id= c:out value="${id}"/ " c:out

 value="${title}" / /a /td 

 td c:out value="${ctime}" / /td 

 /tr 

 /c:if 

 /c:forEach 

 /table 

 c:set var="pages" value="${jsonObject.getJsonObject(pages)}" / 


java中好用的list转json的工具hutool 最近做服务器接口开发的时候遇到的小问题,数据库查询之后的数据怎样快捷的转化为json数据,第一时间想到了查库,查了挺久的,好多都是用代码实现,比较懒,这方面内容代码实现的偏多,虽然确实比较简单,但能快点就快点
netkiller Nickname:netkiller | English name: Neo chen | QQ: 291379 | 订阅号:netkiller-ebook | 网站:http://www.netkiller.cn