springmvc的Controller裏實現轉發的同時彈出提示對話框

之前寫servlet時就用到這個,可是如今學了springMVC+hibernate後就不知道怎麼實現了,後來在網上找了好多,看了好多,最後通過本身實踐成功的以下:javascript

1.首先是Controller控制類接受處理路徑下的那個方法參數要傳入:html

  HttpServletResponse response對象,這個對象用來實現響應。java

2.接下來要在方法內編寫你的響應信息的文本格式和編碼方式:spring

  response.setContentType("text/html;charset=gb2312");app

這個可根據你工程的編碼肯定,否則會出現亂碼。編碼

而後就是經過response對象獲取回寫輸出的PrintWriter對象:
  PrintWriter out = response.getWriter();
url

3.接下來就是你要跳轉後彈出的提醒內容信息:spa

  out.print("<script language=\"javascript\">alert('恭喜你成功了!');window.location.href='/你的工程名/user/index'</script>");hibernate

後面必需要加上window.location.href='/你的工程名/user/index',這個是你跳轉到的目標地址,htm

4.最後就是你要在url地址欄顯示的正確路徑:

  return "/user/index";

--------------------------------------一個完整的例子--------------------------------

@RequestMapping(value = "/user/index", method = RequestMethod.POST)
public String checkLogin(@RequestParam("userName") String userName,
@RequestParam("password") String password,
HttpServletResponse response) throws IOException {
    //根據用戶名userName和密碼password查看是不是非法用戶,此處代碼略。。。。
    response.setContentType("text/html;charset=gb2312");
    PrintWriter out = response.getWriter();
    if(非法用戶){
    out.print("<script language=\"javascript\">alert('登陸失敗!');window.location.href='/你的工程名/login'</script>");
    return "/login";
    }
    return "/user/index";  }
相關文章
相關標籤/搜索