• springmvc的Controller里实现转发的同时弹出提示对话框


    以前写servlet时就用到这个,但是现在学了springMVC+hibernate后就不知道怎么实现了,后来在网上找了好多,看了好多,最后经过自己实践成功的如下:

    1.首先是Controller控制类接受处理路径下的那个方法参数要传入:

      HttpServletResponse response对象,这个对象用来实现响应。

    2.接下来要在方法内编写你的响应信息的文本格式和编码方式:

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

    这个可根据你工程的编码确定,不然会出现乱码。

    然后就是通过response对象获取回写输出的PrintWriter对象:
      PrintWriter out = response.getWriter();

    3.接下来就是你要跳转后弹出的提醒内容信息:

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

    后面必须要加上window.location.href='/你的工程名/user/index',这个是你跳转到的目标地址,

    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"; 
     }
     
  • 相关阅读:
    透视表提取不反复记录(1)-出现值
    ORA-38760: This database instance failed to turn on flashback database
    Android蓝牙串口程序开发
    指尖上的电商---(5)schema.xml配置具体解释
    iOS-UIImage imageWithContentsOfFile 和 imageName 对照
    JSON-RPC轻量级远程调用协议介绍及使用
    POJ 2296 Map Labeler(2-sat)
    接口測试-HAR
    [Leetcode]Combination Sum II
    MarkDown、Vim双剑合璧
  • 原文地址:https://www.cnblogs.com/SHANKS-log/p/5031758.html
Copyright © 2020-2023  润新知