一、使用HttpServletResponse用流的形式输出
在Action中直接使用HttpServletResponse的getWriter()方法使用PrintWriter输出;
public class AjaxPrizeKindsAction extends ActionSupport {
private Logger logger = Logger.getLogger(AjaxPrizeKindsAction.class);
private IPrizeKindInfoService prizeKindInfoService;
@Override
public String execute() throws Exception {
logger.debug("enter into AjaxPrizeKindsAction");
List<PrizeKindInfoVO> list = prizeKindInfoService.findAllByCondition();
if(list != null && list.size() > 0 ){
Object[] temp = list.toArray();
JSONArray jsonArray = JSONArray.fromObject(temp);
String json = jsonArray.toString();
WebUtil.printOut(json, "text/html", "UTF-8");
}else{
String json = null;
WebUtil.printOut(json, "text/html", "UTF-8");
}
return null;
}
public void setPrizeKindInfoService(IPrizeKindInfoService prizeKindInfoService) {
this.prizeKindInfoService = prizeKindInfoService;
}
}
WebUtil工具类:
public class WebUtil {
/**
* 页面输出
* @param res 输入内容
* @param contentType 内容格式,可空
* @param encoding 编码格式,可空
* @throws Exception
*/
public static void printOut(String res, String contentType, String encoding) throws IOException{
PrintWriter out = null;
if(contentType == null || contentType.trim().equals("")){
contentType = "text/html";
}
if(encoding == null || encoding.trim().equals("")) {
encoding = "UTF-8";
}
logger.debug("contentType: " +contentType + " | encoding: " + encoding);
try{
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType(contentType);
response.setCharacterEncoding(encoding);
response.getWriter();
out = response.getWriter();
out.println(res);
}catch (IOException e) {
logger.error("IOException in printOut of BaseAction:", e);
throw e;
}
finally {
if(out != null) {
out.flush();
out.close();
}
}
}
}
在JSP中的AJAX请求:
$.post(url, params, function (result,textStatus){
if(textStatus == 'success'){
if(result != null){
//处理result
}
}
struts.xml中的配置:
<!-- ajax -->
<package name="ajax" extends="struts-default" namespace="/ajax">
<action name="" class="com.test.controller.AjaxPrizeKindsAction"></action>
</package>
这样的做法是借助工具类自定义返回流的形式,比较复杂,并且跟struts2没有多大关系,struts2支持一种
stream类型的Ruselt,这种类型可以直接向客户端浏览器生成二进制响应,文本响应。
二、使用stram类型的Result实现AJAX
public class AjaxPrizeKindsAction extends ActionSupport {
private Logger logger = Logger.getLogger(AjaxPrizeKindsAction.class);
private InputStream inputStream;
@Override
public String execute() throws Exception {
logger.debug("enter into AjaxPrizeKindsAction");
String a = "{\"a\":\"c\",\"sd\":\"ds\"}";
inputStream = new ByteArrayInputStream(a.toString().getBytes());
return SUCCESS;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
xml中的配置
<action name="ajaxprizekinds" class="com.shcredit.controller.prize.prizeactivity.prizeactdet.AjaxPrizeKindsAction">
<result name="success" type="stream">
<!-- 指定下载文件的文件类型 -->
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>