Java tutorial
/** * Copyright (c) 2005-2009 springside.org.cn Licensed under the Apache License, * Version 2.0 (the "License"); $Id: Struts2Utils.java,v 1.1 2010/07/15 01:29:09 * alan Exp $ */ package com.manpowergroup.cn.core.utils; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.PropertyFilter; import org.apache.commons.lang.StringUtils; import org.apache.struts2.ServletActionContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.manpowergroup.cn.core.Result; /** * Struts2 Utils. * * ?Request/Response/Sessionjsp/freemaker. * * @author calvin */ public class Struts2Utils { //header ? private static final String ENCODING_PREFIX = "encoding"; private static final String NOCACHE_PREFIX = "no-cache"; private static final String ENCODING_DEFAULT = "UTF-8"; private static final boolean NOCACHE_DEFAULT = true; private static Logger logger = LoggerFactory.getLogger(Struts2Utils.class); // ?Request/Response/Session // /** * ?HttpSession. */ public static HttpSession getSession() { return ServletActionContext.getRequest().getSession(); } /** * ?HttpRequest. */ public static HttpServletRequest getRequest() { return ServletActionContext.getRequest(); } /** * ?HttpResponse. */ public static HttpServletResponse getResponse() { return ServletActionContext.getResponse(); } /** * ?Request Parameter. */ public static String getParameter(String name) { return getRequest().getParameter(name); } // jsp/freemaker // /** * . * eg. * render("text/plain", "hello", "encoding:GBK"); * render("text/plain", "hello", "no-cache:false"); * render("text/plain", "hello", "encoding:GBK", "no-cache:false"); * * @param headers ??header??"encoding:""no-cache:",UTF-8true. */ public static void render(final String contentType, final String content, final String... headers) { try { //?headers? String encoding = ENCODING_DEFAULT; boolean noCache = NOCACHE_DEFAULT; for (String header : headers) { String headerName = StringUtils.substringBefore(header, ":"); String headerValue = StringUtils.substringAfter(header, ":"); if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) { encoding = headerValue; } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) { noCache = Boolean.parseBoolean(headerValue); } else throw new IllegalArgumentException(headerName + "??header"); } HttpServletResponse response = ServletActionContext.getResponse(); //headers? String fullContentType = contentType + ";charset=" + encoding; response.setContentType(fullContentType); if (noCache) { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); } response.getWriter().write(content); response.getWriter().flush(); } catch (IOException e) { logger.error(e.getMessage(), e); } } /** * . * @see #render(String, String, String...) */ public static void renderText(final String text, final String... headers) { render("text/plain", text, headers); } /** * HTML. * @see #render(String, String, String...) */ public static void renderHtml(final String html, final String... headers) { render("text/html", html, headers); } /** * XML. * @see #render(String, String, String...) */ public static void renderXml(final String xml, final String... headers) { render("text/xml", xml, headers); } /** * JSON. * * @param string json. * @see #render(String, String, String...) */ public static void renderJson(final String string, final String... headers) { render("application/json", string, headers); } /** * JSON. * * @param map Map,json. * @see #render(String, String, String...) */ @SuppressWarnings("unchecked") public static void renderJson(final Map map, final String[] ignores, final String... headers) { JsonConfig config = new JsonConfig(); config.setIgnoreTransientFields(true); config.setExcludes(ignores); String jsonString = JSONObject.fromObject(map, config).toString(); renderJson(jsonString, headers); } /** * JSON. * * @param map Map,json. * @see #render(String, String, String...) */ @SuppressWarnings("unchecked") public static void renderJson(final Map map, final String... headers) { String jsonString = JSONObject.fromObject(map).toString(); renderJson(jsonString, headers); } /** * JSON. * * @param object Java,json. * @see #render(String, String, String...) */ public static void renderJson(final Object object, final String... headers) { String jsonString = JSONObject.fromObject(object).toString(); renderJson(jsonString, headers); } /** * JSON????listmapset * * @param object Java,json. * @see #render(String, String, String...) */ public static void renderJsonFilter(final Object object, final String[] filters, final String... headers) { List<String> ignoreList = new ArrayList<String>(); if (filters != null && filters.length > 0) { ignoreList = Arrays.asList(filters); } renderJsonFilter(object, ignoreList, headers); } /** * JSON????listmapset * * @param object Java,json. * @see #render(String, String, String...) */ public static void renderJsonFilter(final Object object, final List<String> ignoreList, final String... headers) { JsonConfig config = new JsonConfig(); PropertyFilter filter = new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if (ignoreList != null && ignoreList.contains(name)) { return true; } return false; } }; config.setJsonPropertyFilter(filter); String jsonString = JSONObject.fromObject(object, config).toString(); renderJson(jsonString, headers); } }