第一种,使用你所说的stream来进行服务端处理:
public class TextResult extends ActionSupport {
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public String execute() throws Exception {
inputStream = new StringBufferInputStream("ok");
return SUCCESS;
}
}
struts.xml:
<action name="text-result" class="actions.TextResult">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
这样,客户端向text-result发送请求时,请会返回ok这个字符流。
--------------------------------------------------------
第二种,通过你刚刚说的使用返回null来解决:
public String updateViewCount()throws Exception{
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
String xmlValue="";
try{
WebGame wg=webGameService.getObjectById(Integer.parseInt(id));
wg.setWgViewCount(wg.getWgViewCount()+1);
webGameService.modifyObject(wg);
xmlValue="<sup ";
xmlValue+="result='"+wg.getWgViewCount()+"' ";
xmlValue+="good='"+wg.getWgBallot()+"' ";
xmlValue+="id='"+id+"' ";
xmlValue+="/>";
}
catch(Exception e){
xmlValue="<sup ";
xmlValue+="error='"+e.getMessage()+"' ";
xmlValue+="/>";
}
xmlValue=xmlValue.replace("&", "&");
System..out.println(xmlValue);
response.getWriter().write(xmlValue);
return null;
}
struts.xml
<action name="updateViewCount" class="struts.action.WebEvalAction" method="updateViewCount">
</action>
第二种与第一种本质上是没有区别的,一个是自己手动来完成输出流,一个是通过struts2自己来完成。