对于Struts2中Action中返回字符串的处理,比如做级联时,就可能用到返回json(字符串)的数据,些时Action该返回字符串,其实:让Action中的返回字符串的方法,直接return null;并在return之前用输出流输出字符串就可以了,跟servlet输入HTMl方法一样。
1、
- public class CscandmsAction extends ActionSupport implements ServletResponseAware{
- Logger log=Logger.getLogger(CscandmsAction.class);
- private HttpServletResponse response;
- public void setServletResponse(HttpServletResponse arg0) {
- this.response=arg0;
- }
- /**
- * country dms做级联时返回的JSON数据(字符串)
- * @param numcountryguid
- * @return
- */
- public String getTestJSON() throws Exception{
- response.setCharacterEncoding("utf-8");
- PrintWriter pw=null;
- try {
- pw = response.getWriter();
- pw.write("JSON字符串");
- } catch (IOException e) {
- log.error(e.getMessage(),e.fillInStackTrace());
- }
- //response.setCharacterEncoding("utf-8");
- pw.flush();
- pw.close();
- return null;
- }
- }