修正
@@ -21,6 +21,17 @@ | ||
21 | 21 | recordList.toArray(ary); |
22 | 22 | return "[" + join(",", ary) + "]"; |
23 | 23 | } |
24 | + public static String mapToJSON(HashMap<String,String> map) { | |
25 | + ArrayList<String> setList = new ArrayList<String>(); | |
26 | + for(String key:map.keySet()) { | |
27 | + String set = "'" + key + "':'" + map.get(key) + "'"; | |
28 | + setList.add(set); | |
29 | + } | |
30 | + String[] ary = new String[setList.size()]; | |
31 | + setList.toArray(ary); | |
32 | + String str = "({" + join(",", ary) + "})"; | |
33 | + return str; | |
34 | + } | |
24 | 35 | |
25 | 36 | public static String join(String spacer, String[] ary) { |
26 | 37 | if(ary.length == 0) { |
@@ -0,0 +1,28 @@ | ||
1 | +package refrect_action; | |
2 | + | |
3 | +import java.util.HashMap; | |
4 | +import java.util.List; | |
5 | + | |
6 | +import lib.string.JSonUtil; | |
7 | +import models.ContentsBiz; | |
8 | +import servercore.http.HttpRequest; | |
9 | +import servercore.http.HttpResponse; | |
10 | +import servercore.servelet.reflect.Action; | |
11 | +import servercore.http.HttpRequest.METHOD; | |
12 | + | |
13 | +public class ContentData extends Action{ | |
14 | + | |
15 | + public HttpResponse execute(HttpRequest request) throws Exception { | |
16 | + HttpResponse res = new HttpResponse(); | |
17 | + METHOD method = request.getMethod(); | |
18 | + int index = Integer.parseInt(request.getGetParams().get("idx")); | |
19 | + ContentsBiz biz = new ContentsBiz(); | |
20 | + res.addHeader("Cache-Control", "no-cache"); | |
21 | + if(method == METHOD.GET) { | |
22 | + HashMap<String,String> map = biz.getData(index); | |
23 | + String data = JSonUtil.mapToJSON(map); | |
24 | + res.setResponseStr(data); | |
25 | + } | |
26 | + return res; | |
27 | + } | |
28 | +} |
@@ -22,5 +22,19 @@ | ||
22 | 22 | } |
23 | 23 | return list; |
24 | 24 | } |
25 | + public HashMap<String,String> getData(int index) throws Exception{ | |
26 | + TableDataQuery query = new TableDataQuery(ConstUtil.Test01); | |
27 | + String[] culmname = {"title", "created_at", "text"}; | |
28 | + String sql = "select title, created_at, text from textfeeds where text_index = " + index; | |
29 | + HashMap<String,String> data = null; | |
30 | + query.setColumnName(culmname); | |
31 | + try { | |
32 | + query.getConnection(); | |
33 | + data = query.executeQuery(sql).get(0); | |
34 | + } finally { | |
35 | + query.closeConnection(); | |
36 | + } | |
37 | + return data; | |
38 | + } | |
25 | 39 | |
26 | 40 | } |