Ajax的工具类AjaxUtils,使用struts返回Json类型
发布时间:2020-12-15 00:33:39 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class AjaxUtil { /** * ajax response methodbr/ * 如果出现异常,则返回错误代码505 * * @param response * @param text * ajax返回的内容 *
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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,// 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; } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |