Ajax的工具类AjaxUtils,使用struts返回Json类型

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
public class AjaxUtil {  
  
    /** 
     * ajax response method<br/> 
     * 如果出现异常,则返回错误代码505 
     *  
     * @param response 
     * @param text 
     * ajax返回的内容 
     *  
     */  
    public static void ajaxResponse(HttpServletResponse response, String text) {  
        try {  
            response.setContentType("text/html;charset=utf-8");  
            response.getWriter().write(text);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * <pre> 
     * 注意:ajax请求必须携带:jsonnp请求callback 
     *  
     * eg: 
     * $.getJSON(path + "ajax/news/comment.go?newsId=" + newsId + "&content=" +content+ "&callback=?", function(data) { 
     *   alert(data['val']); 
     * }); 
     *  
     * </pre> 
     *  
     * @param response 
     * @param msg 
     */  
    public static void jsonnpResponse(HttpServletResponse response,  
            String callback, String msg) {  
        try {  
            response.setContentType("text/html;charset=utf-8");  
            response.getWriter().write(callback + "(" + msg + ")");  
        } catch (IOException e) {  
  
        }  
    }  
  
    /** 
     * 异常操作,需要浏览器地址返回上一步 
     *  
     * @param response 
     * @param alertMsg 
     *            弹出提示信息 
     */  
    public static void ajaxResponseRollBack(HttpServletResponse response,  
            String msg) {  
        try {  
            response.setContentType("text/html;charset=utf-8");  
            response.getWriter().write(  
                    "<script type='text/javascript'>alert('" + msg  
                            + "');window.history.back();</script>");  
        } catch (IOException e) {  
            // LogConsoleUtil.write(Common.class, "ajaxResponseRollBack()",  
            // e.getMessage());  
        }  
    }  
  
    // ajaxJSON返回  
    public static void ajaxJSONResponse(HttpServletResponse response,  
            String str, Object object) {  
        response.setContentType("text/json;charset=utf-8");  
        JSONObject json = new JSONObject();  
        json.put(str, object);  
        try {  
            response.getWriter().write(json.toString());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public static void ajaxResponseAlert(HttpServletResponse response,  
            String msg) {  
        try {  
            response.setContentType("text/html;charset=utf-8");  
            response.getWriter().write(  
                    "<script type='text/javascript'>alert('" + msg  
                            + "');</script>");  
        } catch (IOException e) {  
            // LogConsoleUtil.write(Common.class, "ajaxResponseRollBack()",  
            // e.getMessage());  
        }  
    }  
  
    public static Integer StringToInteger(HttpServletRequest request,  
            String name) {  
        String obj = request.getParameter(name);  
        Integer result = -1;  
        if (obj != null) {  
            result = Integer.parseInt(obj);  
        }  
        return result;  
    }  
  
}  

标签: 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:checkbox 全选和全不选jQuery代码实现

下一篇:jquery操作select(取值,设置选中)