hnakamur/sastruts-csvdownload-ex1 at master - GitHub
クエリ結果の件数が多いときに備えて、結果をリストに持つのではなくorg.seasar.extension.jdbc.Select#iterateで1件ずつコールバックして出力するようにしています。
2011-06-28
SAStrutsでJSONをレスポンスに書き出すサンプル
SAStrutsでJSONをレスポンスに書き出すサンプルを作ってみました。JSONIC - simple json encoder/decoder for javaを利用しています。
アクションからの呼び出し例
ユーティリティの実装
アクションからの呼び出し例
@Execute(validator = false)
public String hello() {
Map<String, Object> json = new HashMap<String, Object>();
json.put("author", "Goldratt & Fox");
json.put("title", "The Race");
JSONResponseUtil.writeJSON(json);
return null;
}ユーティリティの実装
package net.naruh.sastruts.util;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
import net.arnx.jsonic.JSON;
import org.seasar.framework.exception.IORuntimeException;
import org.seasar.struts.util.RequestUtil;
import org.seasar.struts.util.ResponseUtil;
/**
* JSONをレスポンスに書き出すためのユーティリティクラスです。
* @author hnakamur
*/
public final class JSONResponseUtil {
/** JSONのエンコーディング */
public static final String JSON_ENCODING = "UTF-8";
/** JSONのコンテントタイプ */
public static final String JSON_CONTENT_TYPE = "application/json";
/**
* JSONをレスポンスに書き出します。
* @param source 書き出すオブジェクト
* @param prettyPrint 出力を整形するか
*/
public static void writeJSON(Object source) {
writeJSON(source, false, JSON_CONTENT_TYPE, null);
}
/**
* JSONをレスポンスに書き出します。
* @param source 書き出すオブジェクト
* @param prettyPrint 出力を整形するか
*/
public static void writeJSON(Object source, boolean prettyPrint) {
writeJSON(source, prettyPrint, JSON_CONTENT_TYPE, null);
}
/**
* JSONをレスポンスに書き出します。
* @param source 書き出すオブジェクト
* @param prettyPrint 出力を整形するか
* @param contentType コンテントタイプ
*/
public static void writeJSON(Object source, boolean prettyPrint, String contentType) {
writeJSON(source, prettyPrint, contentType, null);
}
/**
* JSONをレスポンスに書き出します。
* @param source
* 書き出すオブジェクト
* @param prettyPrint
* 出力を整形するか
* @param contentType
* コンテントタイプ。 デフォルトはapplication/context。
* @param encoding
* エンコーディング。 指定しなかった場合は、リクエストのcharsetEncodingが設定される。
* リクエストのcharsetEncodingも指定がない場合は、UTF-8。
*/
public static void writeJSON(Object source, boolean prettyPrint, String contentType, String encoding) {
if (contentType == null) {
contentType = JSON_CONTENT_TYPE;
}
if (encoding == null) {
encoding = RequestUtil.getRequest().getCharacterEncoding();
if (encoding == null) {
encoding = JSON_ENCODING;
}
}
HttpServletResponse response = ResponseUtil.getResponse();
response.setContentType(contentType + "; charset=" + encoding);
try {
Writer out = new OutputStreamWriter(response
.getOutputStream(), encoding);
try {
JSON.encode(source, out, prettyPrint);
} finally {
out.close();
}
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
}
登録:
コメント (Atom)
ラベル
- 7zip
- Android
- ant
- Apache
- arping
- ASUS
- binary
- BootCamp
- CentOS
- checkstyle
- chkconfig
- coLinux
- css
- csv
- curl
- cygwin
- dd
- Debian
- dns
- docomo
- Doma
- eclipse
- EPEL
- ext4
- firefox
- font
- gimp
- git
- haskcell
- Haskell
- heartbeat
- HTML
- html5
- i18n
- imageFORMULA
- ImageMagick
- ime
- InternetExplorer
- iPhone
- iptables
- java
- javascript
- json
- jsonic
- keepalived
- keychain
- kvm
- libvirt
- Linux
- Linux-VServer
- Lion
- load-balancer
- lvm
- mail-server
- maven
- mybatis
- MySQL
- nat
- netatalk
- network
- nginx
- node.js
- nslookup
- NTFS
- OpenVZ
- oracle
- OSX
- Perl
- permission
- PHP
- plink
- port-forwarding
- PostgreSQL
- ProLiant
- putty
- Q-Oper8
- raid
- Rails
- rpmforge
- Ruby
- sastruts
- scanner
- ScanSnap
- scp
- screen
- screensaver
- serial-console
- sl6
- slirp
- spring3
- ssh
- ssh-agent
- struts2
- Subversion
- svnsync
- syntax highlight
- tap
- teraterm
- Time Machine
- tomcat
- ubuntu
- UL80AG
- ultravnc
- vim
- VIP
- virt-clone
- vmware
- vnc
- webworker
- Windows
- Windows 7
- Windows XP
- Wireshark
- wolframalpha
- Xcode
- XHTML
- yum
- 本