ky与你 2016-05-23 08:24 采纳率: 90%
浏览 2628
已采纳

springMVC中的Model是不能页面之间传递的?

我的请求被controller拦截,进入controller,然后controller跳转至show.jsp,
跳转之前我往show.jsp页的model里面增加了几个key,

然后我在show页去ajax另一个controller,这时候ajax调用的controller是无法获取
show.jsp页面的model的,这是为什么?request不是也可以在页面之间传递吗?

下方为代码:
第一次跳转页面的controller

 @RequestMapping("/show")
    public String showMvc(
            @RequestParam("name") String name,
            @RequestParam("pageIndex") Integer pageIndex,
            @RequestParam("pageSize") Integer pageSize, Model map) throws Exception {
        User user=new User();
        user.setName(name);
        userService.add(user);
        PageUtil pageUtil=userService.getUserListByPageUtil(pageIndex,pageSize,user);
        map.addAttribute("pageUtil",pageUtil);
        map.addAttribute("pageIndex",pageIndex);
        return "user/userShow";
    }

show.jsp页面的js,ajax代码

 function search(){
        alert(41234123);
        $.ajax({
            type:"post",
            url:"search",
            //data:$("form").serialize(),
            success : function() {
                alert(2412);
                alert('${pageUtil.totalPages}');
                alert("${pageUtil.totalPages}");
                $("#username").html();
                $("#username").html("${user.name}");
                $("#userage").html("${user.age}");
            }
        });
    };

ajax调用的controller

 @RequestMapping(value="/search", method = {RequestMethod.POST})
    @ResponseBody
    public void search(Model map) throws Exception {
        System.out.println("********************");
        System.out.println(map.containsAttribute("pageUtil"));
        System.out.println(map.containsAttribute("pageIndex"));
        System.out.println(map.asMap().get("pageUtil"));
        System.out.println(map.asMap().get("pageIndex"));
        System.out.println(map.toString());
        System.out.println("********************");
        String search="qinkai0";
        User user=userService.getUserByName(search);
        map.addAttribute("user",user);
    }

下方是结果

 ********************
false
false
null
null
{}
********************
  • 写回答

1条回答 默认 最新

  • effiu 2016-05-23 09:30
    关注

    这是两次请求,不同请求传的值,不能共用。建议将第一次请求传的值,保存起来(放入input中),这样可以反复使用

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置