zoukankan      html  css  js  c++  java
  • SpringMVC Controller的返回类型

    Controller的三种返回类型中

    ModelAndView类型 带数据带跳转页面

    String 跳转页面不带数据

    void 通常是ajax格式请求时使用

    1返回ModelAndView

    controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。

    controller

    @RequestMapping("/test")
    public ModelAndView test(){
        ModelAndView mav=new ModelAndView("hello");//通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面
        mav.addObject("time", new Date());
        mav.getModel().put("name", "caoyc");
        return mav;
    }

    JSP

     time:${requestScope.time}
     <br/>
     name:${name }

    2   返回字符串  

    controller方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。

            @RequestMapping(value = "saveRegSigned")
            public String saveRegSigned(MeetingReg meetingReg, HttpServletRequest request, HttpServletResponse response,
                    Model model) throws Exception {
                meetingReg.setMeetingId(Utils.getMeetingId(request));
                Map<String, Object> resultMap = regService.saveRegSigned(meetingReg);
                model.addAttribute("resultMap", resultMap);
                return "modules/meeting/signed/RegSignedReturnPage";
            }
            

    JSP

                    <div class="code_reg">
                        <ul>
                            <li>注册号:${resultMap.regCode}</li>
                            <li>注册类型:${resultMap.regType}</li>
                        </ul>
                    </div>

    3   返回void

    void 

    如果返回值为空,则响应的视图页面对应为访问地址

    @RequestMapping("/index")
    public void index() {
        return;
    }

    对应的逻辑视图名为"index"

    4返回map

    Map

    复制代码
    @RequestMapping("/demo2/show") 
        public Map<String, String> getMap() { 
            Map<String, String> map = new HashMap<String, String>(); 
            map.put("key1", "value-1"); 
            map.put("key2", "value-2"); 
            return map; 
        } 
    复制代码

    在jsp页面中可直通过${key1}获得到值, map.put()相当于request.setAttribute方法。

    对应的逻辑视图名为../demo2/show+suffix

    返回其他object类型同map

    1.使用 String 作为请求处理方法的返回值类型是比较通用的方法,这样返回的逻辑视图名不会和请求 URL 绑定,具有很大的灵活性,而模型数据又可以通过 ModelMap 控制。
    2.使用void,map,Model 时,返回对应的逻辑视图名称真实url为:prefix前缀+视图名称 +suffix后缀组成。
    3.使用String,ModelAndView返回视图名称可以不受请求的url绑定,ModelAndView可以设置返回的视图名称。

    参考http://www.cnblogs.com/xiepeixing/p/4243801.html

        在controller方法形参上可以定义request和response,使用request或response指定响应结果:
        1、使用request转向页面,如下:
        request.getRequestDispatcher("页面路径").forward(request, response);

          2、也可以通过response页面重定向:
        response.sendRedirect("url")

         3、也可以通过response指定响应结果,例如响应json数据如下:
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("json串");

     

     

     

     

  • 相关阅读:
    红黑树的插入操作详解
    Java实现红黑树
    No-sql之redis常用命令
    如何配置JedisPool的参数
    JedisPool使用注意事项
    2-SAT问题的小结
    BZOJ 2142 礼物 组合数学 CRT 中国剩余定理
    BZOJ 4521 CQOI 2016 手机号码 数位DP
    BZOJ 4380 Myjnie 区间DP
    BZOJ 2754 SCOI 2012 喵星球上的点名 后缀数组 树状数组
  • 原文地址:https://www.cnblogs.com/miye/p/6970436.html
Copyright © 2011-2022 走看看