zoukankan      html  css  js  c++  java
  • SpringMVC服务端返回数据

    下面介绍controller向jsp页面传递数据的几种方式

    请求转发

    1.在学习MVC之前,用request的setAttribute方法传递数据

    @RequestMapping("/response")                                
    public ModelAndView sendData01(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("result");          
        request.setAttribute("user_name", "清雅轩");               
        return mav;                                             
    }                                                           

    在jsp页面用el表达式即可接到数据。

    2.用ModelAndView的addObject方法

    @RequestMapping("/modelandview")                            
    public ModelAndView sendData02() {                          
        ModelAndView mav = new ModelAndView("result");          
        Map<String, String> map = new HashMap<String, String>();
        map.put("user_id", "1");                                
        map.put("user_name", "清雅轩");                            
        mav.addObject("map", map);                              
        return mav;
    }                                             

    3.用ModelMap的addAttribute方法

    @RequestMapping("/modelmap")                   
    public String sendData03(ModelMap modelMap) {  
        modelMap.addAttribute("user_name", "清雅轩"); 
        return "result";                           
    }                                              

    这里返回值为一个字符串,而不是ModelAndView对象,实际上字符串形式与ModelAndView的viewName等价,解析器也会将字符串解析出路径。

    还有一种返回值为void的形式,这种默认会将@RequestMapping中的请求路径进行解析得出路径。

    @RequestMapping("/void")  
    public void voidMethod() {
    //默认的返回路径为/WEB-INF/void.jsp                 
    }                         

    4.用Model的addAttribute方法

    @RequestMapping("/model")                   
    public String sendData04(Model model) {     
        model.addAttribute("user_name", "清雅轩"); 
        return "result";                        
    }                                           

    5.用Map集合

    @RequestMapping("/map")                            
    public String sendData05(Map<String, String> map) {
        map.put("user_name", "清雅轩");                   
        return "result";                               
    }                                                  

    重定向

    1.在路径后拼凑数据

    @RequestMapping("/redirect")                          
    public String sendData06() {                          
        return "redirect:result.jsp?user_name=qingyaxuan";
    }                                                     

    这种方式传递的数据属于param域,用${param.user_name}可以获取。但是这种方式在地址栏会显示出参数,而且只能传递简单参数

    2.用RedirectAttribute的addFlashAttribute方法

    因为/WEB-INF/的安全级别很高,重定向方式无法访问/WEB-INF/目录下的jsp文件,所以需要先重定向到controller中再由请求转发访问。

    @RequestMapping("/redirect02")                              
    public String sendData07(RedirectAttributes redi) {         
                                                                
        Map<String, String> map = new HashMap<String, String>();
        map.put("user_id", "1");                                
        map.put("user_name", "qingyaxuan");                     
        redi.addFlashAttribute("map", map);                     
        redi.addFlashAttribute("account", "account");           
        return "redirect:redirect03";                           
    }                                                           
                                                                
    @RequestMapping("/redirect03")                              
    public String sendData08() {                                
        return "result";                                        
    }                                                           

    用addFlashAttribute传递的数据属于request域,可用$(requestScope.account)获取。

    而且这种方式可以传递复杂数据类型

    使用addFlashAttribute方法传值是为了解决下列问题,引用别人的解释说明一下:

    正常的MVC Web应用程序在每次提交都会POST数据到服务器。一个正常的Controller (被注解 @Controller标记)从请求获取数据和处理它 (保存或更新数据库)。一旦操作成功,用户就会被带到(forward)一个操作成功的页面。传统上来说,这样的POST/Forward/GET模式,有时候会导致多次提交问题. 例如用户按F5刷新页面,这时同样的数据会再提交一次。

    为了解决这问题, POST/Redirect/GET 模式被用在MVC应用程序上. 一旦用户表单被提交成功, 我们重定向(Redirect)请求到另一个成功页面。这样能够令浏览器创建新的GET请求和加载新页面。这样用户按下F5,是直接GET请求而不是再提交一次表单。

    虽然这一方法看起来很完美,并且解决了表单多次提交的问题,但是它又引入了一个获取请求参数和属性的难题. 通常当我们生成一次http重定向请求的时候,被存储到请求数据会丢失,使得下一次GET请求不可能访问到这次请求中的一些有用的信息.

    Flash attributes 的到来就是为了处理这一情况. Flash attributes 为一个请求存储意图为另外一个请求所使用的属性提供了一条途径. Flash attributes 在对请求的重定向生效之前被临时存储(通常是在session)中,并且在重定向之后被立即移除。

    但引述的说法中,数据是被存在session中的,我试的例子中数据是存在request域中的,不知究竟孰是孰非,有知道的dalao希望能指点迷津。

  • 相关阅读:
    Linux下关于信号block与unblock的小研究
    perl打印乘法表
    Linux下libaio的一个简单例子
    heritrix的简单使用以及在后台调用heritrix
    perl修改文件内容
    Linux下mmap函数的一个练习
    Linux real uid 和 effective uid相关总结
    阶段性小总结
    归并排序的一个练习
    利用openssl进行base64的编码与解码
  • 原文地址:https://www.cnblogs.com/qingyaxuan/p/6522995.html
Copyright © 2011-2022 走看看