Java实现ajax
jsp端的代码,sucess:function(){} 里面就是返回的处理
function ChangeTime(){ alert("www"); var startYmd = $('#t1').val(); var endYmd = $('#t2').val(); var now = new Date(); var today = new Date(now.getYear(),now.getMonth(),now.getDate()); today = now.getYear() + "-" + (now.getMonth()+1) + "-" + now.getDate(); var compdays = dateCompare(today, startYmd); var diffdays = dateCompare(startYmd, endYmd); if(diffdays < 0){ alert("終了日が開始日より前です。選択し直して下さい。"); return; } if(diffdays > 30){ //検索期間制限:索期間は一ヶ月以上の場合、メッセージ「検索期間が一ヶ月以内の日付を選択して下さい。」を提示する alert("検索期間が一ヶ月以内の日付を選択して下さい。"); return; } window.location.href="staffkakuteijyoukyou.do?startYmd="+startYmd+"&endYmd="+endYmd+"&SelYmd=1"; var url="./staffkakuteijyoukyou.do?"; $.ajax({ url : url, dateType:"text", type : 'post', cache : false, data:"startYmd="+ startYmd + "&endYmd="+endYmd+"&SelYmd=1", success : function (returnVal) { //这里的returnVal就是action里面输出的流,也就是所谓的返回信息 if(returnVal=="0"){ msg.text("出力するスタッフが見つかりません。"); }else{ //if(ele=='pdf'){ // window.open('attendanceConfirm.do?method=output&outupType='+outupType+"&staffNameType="+staffNameType+"&restTimeType="+restTimeType+"&sagyoDate="+sDate,'','menubar=0,titlebar=0,status=0,location=0'); //}else{ // window.location.href="attendanceConfirm.do?method=excel&outupType="+outupType+"&staffNameType="+staffNameType+"&restTimeType="+restTimeType+"&sagyoDate="+sDate; //} window.location.href="staffkakuteijyoukyou.do?startYmd="+startYmd+"&endYmd="+endYmd+"&SelYmd=1"; } }, error : function (e) { alert($.customUM.msg.ajaxError); } }); }
java 的action 代码,通过ajax无刷新的调用acton,然后将数据以流的形式就行输出,就可以在ajax进行接收并且利用
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String sagyoDate = request.getParameter("sagyoDate"); //sagyoDate = "2015-05-11"; String returnVal = "1"; if(checkDate(sagyoDate)){ //HttpSession session = request.getSession(true); //String ko_cd = (String) session.getAttribute("username"); String ko_cd = request.getParameter("ko_cd"); Integer pattern = SagyoDao.getInstance().getPattern(DBCommon.SAGYO_DB, ko_cd,sagyoDate); if(pattern==null||pattern==0){ returnVal = "0"; } }else{ returnVal = "2"; } response.setContentType("text/html"); response.setCharacterEncoding("shift-jis"); PrintWriter out = response.getWriter(); out.print(returnVal); out.flush(); out.close(); return null; }