struts2返回字符串给ajax使用

给配置文件添加返回内容:

<result name="hint" type="stream">
                <param name="contentType">text/html</param>
                <param name="inputName">hint</param>
            </result>

 

在action中添加 参数:

private InputStream hint;
    public InputStream getHint() {
        return hint;
    }
    public void setHint(InputStream hint) {
        this.hint = hint;
    }

在action中添加一个方法:

  private String transcoding(String str) {
        try {
            return new String(str.getBytes("utf-8"), "iso-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

 

action需要执行的方法返回内容:

if(满足条件){
 this.hint= new StringBufferInputStream (transcoding("1"));
            return   "hint";
}

 

ajax:

success: function (result) { 
                 if (result == "1") {
                   alert("成功返回action配置的内容");
                    return false;
                } 
            }

 

posted @ 2017-04-11 11:41  Timesyys  阅读(1230)  评论(0编辑  收藏  举报