zoukankan      html  css  js  c++  java
  • SpringMVC中传入的对象存放在哪里

    今天在完善登陆模块的时候发现了一个小问题,将代码做简化如下
    <form id="loginForm">
                 <label for="username">用户名</label>
                 <input type="text" name="username" autocomplete="off" placeholder="请输入用户名">
                 <label for="password">密码</label>
                 <input type="password" name="password" placeholder="请输入密码">
                 <button type="submit" id="loginbtn">登陆</button>
    </form>

    这样一个简单的表单,提交到SpringMVC映射的路径,可以直接用Bean做参数来接收
    @RequestMapping(value="login",method=RequestMethod.POST)  
    @ResponseBody //直接写回字符串而不是跳转
    public ModelAndView loginCheck(UserVo user, HttpServletRequest request, Model model) throws IOException{
           System.out.println(user); //这里可以直接取到user,username和password都与表单提交来的属性相同
           //然而通过以下四种方式都取不到user
                System.out.println(request.getAttribute("user")); //null
                System.out.println(request.getAttribute("userVo"));    //null
                System.out.println(request.getSession().getAttribute("user")); //null
                System.out.println(request.getSession().getAttribute("userVo")); //null
               System.out.println("==================");
           //原来user被封装在了model对象中
           System.out.println(model.containsAttribute("userVo")); //true
    
          //*****调用service进行验证返回业务对象的实例userBo*****
          //ajax返回验证结果给前端

            JsonResult result = new JsonResult();

            result.setResultCode(1);

            result.setData(userBo.getUserNum());

            mm.addAttribute("result", result);

            return new ModelAndView(new MappingJackson2JsonView(), mm);

        }

    乍一看好像不是什么大问题,Spring爱封装就让他封装,我们只要使用就好了,然而-------

    我在前端用ajax成功的回调函数打出接收到的json数据,不看不知道,一看吓一跳

    {
        "userVo":{
            "username":"1",
            "password":"123"},
        
         "result":{
            "resultCode":1,
            "resultMessage":"ajax成功",
            "data":1}
    }

    上次提交的密码赫然在列,也就是说Model在请求前后之间是保留传递的,虽然servlet中我们已经习惯了请求带参数转发给下个页面的模式,会对一些参数进行销毁,但是在springmvc中,很可能被框架本身的调试折腾的焦头烂额,从而忽略了这些细节

    那么,怎么像servlet中的request.removeAttibute一样移除这个我们不想传递的user呢?

    注意到在整个login方法中我并没有操作传入的参数model,这说明model的传输是由框架帮我们完成的,所以首先想到的是直接操作model,发现没有remove之类的方法,换个思路,对SpringMVC略有了解的同学应该知道Controller中的方法支持传入多种类型的参数,他们之间也是可以相互转换的,利用的原理就是Map,想到这一点,再看model的方法,其中有一个叫asMap(),问题就豁然开朗了,只需要在你想清除掉密码的位置加一行代码

    model.asMap().remove("userVo");

    其中userVo是该函数传入的参数 (UserVo user,  *,*,*)的类型,这一点从前面的json数据也可以看出来

    public ModelAndView loginCheck(UserVo user, HttpServletRequest request,HttpServletResponse response, Model model)

    SpringMVC利用利用反射,帮我们完成了表单提交过来的数据的封装,实在是方便,不过还是要记得及时处理不需要或不可传输的数据,以保证我们程序的安全和洁净,又要两点啦,开工, Fighting~~

  • 相关阅读:
    URAL 2046 A
    URAL 2056 Scholarship 水题
    Codeforces Gym 100286I iSharp 水题
    Codeforces Gym H. Hell on the Markets 贪心
    Codeforces Gym 100286G Giant Screen 水题
    Codeforces Gym 100286B Blind Walk DFS
    Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
    Codeforces Gym 100286A. Aerodynamics 计算几何 求二维凸包面积
    Codeforces Gym 100418K Cards 暴力打表
    Codeforces Gym 100418J Lucky tickets 数位DP
  • 原文地址:https://www.cnblogs.com/iny7/p/5271755.html
Copyright © 2011-2022 走看看