<code id="ymukc"><xmp id="ymukc">

Struts2Utils 工具類

wewopargt的頭像 wewopargt 0 2016-03-09 15:33 1

 基本信息

× 1   

瀏覽數: 29659

分享時間: 2 年 前

3

package com.deppon.oms.module.ltlsieveorder.shared.vo;


import java.io.IOException;

import java.io.OutputStream;

import java.util.Collection;

import java.util.Map;


import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;


import org.apache.commons.lang.StringUtils;

import org.apache.struts2.ServletActionContext;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import net.sf.json.JSONArray;

import net.sf.json.JSONObject;


/**

 * Struts2工具類

 * 

 * @author 楊尚川

 */

public class Struts2Utils {


public static final Logger log = LoggerFactory.getLogger(Struts2Utils.class);


private Struts2Utils() {


};


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 final String TEXT_TYPE = "text/plain";

private static final String JSON_TYPE = "application/json";

private static final String XML_TYPE = "text/xml";

private static final String HTML_TYPE = "text/html";

private static final String JS_TYPE = "text/javascript";


public static HttpSession getSession() {


return ServletActionContext.getRequest().getSession();


}


public static HttpServletRequest getRequest() {


return ServletActionContext.getRequest();


}


public static HttpServletResponse getResponse() {


return ServletActionContext.getResponse();


}


public static String getParameter(String name) {


return getRequest().getParameter(name);


}


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) {


log.error(e.getMessage(), e);


}


}


/**

* 直接輸出文本.

* @see #render(String, String, String...)

*/

public static void renderText(final String text, final String... headers) {


render(TEXT_TYPE, text, headers);


}


/**

* 直接輸出HTML.

* @see #render(String, String, String...)

*/

public static void renderHtml(final String html, final String... headers) {


render(HTML_TYPE, html, headers);


}


/**

* 直接輸出XML.

* @see #render(String, String, String...)

*/

public static void renderXml(final String xml, final String... headers) {


render(XML_TYPE, xml, headers);


}


/**

* 直接輸出JSON.

*

* @param jsonString

*            json字符串.

* @see #render(String, String, String...)

*/

public static void renderJson(final String jsonString, final String... headers) {


render(JSON_TYPE, 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();

render(JSON_TYPE, 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();

render(JSON_TYPE, jsonString, headers);


}


/**

* 直接輸出JSON.

*

* @param collection

*            Java對象集合, 將被轉化為json字符串.

* @see #render(String, String, String...)

*/

public static void renderJson(final Collection<?> collection, final String... headers) {


String jsonString = JSONArray.fromObject(collection).toString();

render(JSON_TYPE, jsonString, headers);


}


/**

* 直接輸出JSON.

*

* @param array

*            Java對象數組, 將被轉化為json字符串.

* @see #render(String, String, String...)

*/

public static void renderJson(final Object[] array, final String... headers) {


String jsonString = JSONArray.fromObject(array).toString();

render(JSON_TYPE, jsonString, headers);


}


public static void renderImage(byte[] data, String type) {


try {


HttpServletResponse response = ServletActionContext.getResponse();

response.setContentType(type);

try (OutputStream out = response.getOutputStream()) {


out.write(data, 0, data.length);

out.flush();


}


} catch (IOException e) {


log.error("渲染圖像失敗", e);


}


}


}



12 [下一頁]

  • pevahanifo的頭像 pevahanifo 2018-10-20 19:53 代碼數:0

    Thanks for this great post, i find it very interesting and very well thought out and put together. I look forward to reading your work in the future. megabucksranch.com

  • cixexey的頭像 cixexey 2018-07-15 16:49 代碼數:0

    This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. KFZ

  • kanong的頭像 kanong 2016-07-12 22:42 代碼數:0

    ??

  • cixexey的頭像 cixexey 2018-08-05 19:52 代碼數:0

    Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.teachings 

  • cixexey的頭像 cixexey 2018-08-05 19:55 代碼數:0

    I just want to let you know that I just check out your site and I find it very interesting and informative..a course in miracles

  • cixexey的頭像 cixexey 2018-08-05 19:56 代碼數:0

    Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. online courses

  • cixexey的頭像 cixexey 2018-08-21 07:57 代碼數:0

    Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work. Healing Empire

  • pevahanifo的頭像 pevahanifo 2018-10-12 22:23 代碼數:0

    I was reading your article and wondered if you had considered creating an ebook on this subject.  Your writing would sell it fast.  You have a lot of writing talent. jovell condo

  • pevahanifo的頭像 pevahanifo 2018-10-13 20:53 代碼數:0

    Great write-up, I am a big believer in commenting on blogs to inform the blog writers know that they’ve added something worthwhile to the world wide web!..Juniper hill condo

  • pevahanifo的頭像 pevahanifo 2018-10-13 23:42 代碼數:0

    I’ve read some good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website.Woodleigh Residences

您的評論:

  
六合特码资料
<code id="ymukc"><xmp id="ymukc">
<code id="ymukc"><xmp id="ymukc">