微信后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.9 KiB

package com.ynxbd.common.result.struts2;
import com.opensymphony.xwork2.ActionInvocation;
import com.ynxbd.common.helper.common.JsonHelper;
import com.ynxbd.common.result.Result;
import com.ynxbd.wx.wxfactory.utils.XmlHelper;
import org.apache.struts2.ServletActionContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class BaseResult implements com.opensymphony.xwork2.Result {
@Override
public void execute(ActionInvocation actionInvocation) {
}
/**
* 重定向
*
* @param location 重定向地址
*/
public static Result redirect(String location) {
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.sendRedirect(response.encodeRedirectURL(location));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 请求返回success
*/
public static Result respStr() {
HttpServletResponse response = ServletActionContext.getResponse();
try (PrintWriter pw = response.getWriter()) {
pw.write("success");
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 返回json
*/
public static Result respJson(HttpServletResponse response, Object data) {
response.setContentType("application/json;charset=utf-8");
try (PrintWriter pw = response.getWriter()) {
pw.write(JsonHelper.toJsonString(data));
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 返回xml(action)
*/
public static Result respXml(String data) {
return respXml(ServletActionContext.getResponse(), data);
}
/**
* 返回xml
*/
public static Result respXml(HttpServletResponse response, String data) {
if (data == null) {
return null;
}
try (OutputStream out = response.getOutputStream()) {
out.write(data.getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 返回xml
*/
public static Result respStr(HttpServletResponse response, String data) {
if (data == null) {
data = "";
}
try (OutputStream out = response.getOutputStream()) {
out.write(data.getBytes(StandardCharsets.UTF_8));
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}