Return Value Spring MVC controller Method

Original link: http://www.yiidian.com/springmvc/return-value.html

Spring MVC controller method return values ​​can support a variety of writing, scenes and effects of each version is different. The following were using to look at each return value.

  • Ordinary string
  • Forwarding string
  • Re-string
  • void
  • ModelAndView
  • Java objects

1 ordinary strings

Return to normal string this situation is more common, mainly used in after we deal with business logic, need to jump to other pages in the application.

Code Example:

/**
 * 1)字符串 - 普通字符串(代表页面名称,不是完整路径,最后经过视图解析器的解析)
 *    优势:写法简单
 *    劣势:只能转发到视图解析器指定的特定目录
 */
@RequestMapping("/string")
public String string(){
    System.out.println("一点教程网-普通字符串....");
    //这里返回页面名称,必须经过视图解析器解析的!!!
    return "index";
}

View resolver configuration:
file

Page address:

file

2 forwarding string

Ordinary strings can only be forwarded to the next page of the catalog view resolver specified prefix, if you want to be forwarded to a page outside the view resolver directory, then you can use the wording forwarded strings.

Code Example:

/**
 * 2)字符串 - 转发字符串
 *     转发字符串格式:
 *        forward:完整页面的路径      例如:forward:/pages/index.jsp
 *
 *    优势:更加灵活,可以转到本项目下的任何页面,可以传递request域对象数据
 *    劣势:写法稍复杂
 */
@RequestMapping("/forward")
public String forward(){
    System.out.println("一点教程网-转发字符串....");
    return "forward:/index.html";
}

Page address:

file

3 redirect string

If you want to use a way to redirect the jump page, then you can use the redirect string is completed.

Code Example:

/**
 * 3)字符串 - 重定向字符串
 *     重定向字符串格式:
 *        redirect:完整页面的路径      例如:redirect:/pages/index.jsp
 *
 *    优势:很灵活,可以重定向到项目内和项目以外的页面
 *    劣势:写法稍复杂,不能转发requesy域对象数据
 */
@RequestMapping("/redirect")
public String redirect(){
    System.out.println("一点教程网-重定向字符串....");
    return "redirect:http://www.yiidian.com";
}

4 returns empty

Generally when we download a file, you do not need a controller method returns nothing, so to void can be.

Code Example:

/**
 * 4)返回void
 *    用于文件下载
 */
@RequestMapping("/void")
public void returnVoid(HttpServletResponse response){
    System.out.println("void....");

    //模拟文件下载
    //1.读取需要下载的文件
    File file = new File("e:/spring.jpg");

    //2.构建文件输入流
    try {
        InputStream in = new FileInputStream(file);

        //3.获取文件输出流(从response对象获取)
        OutputStream out = response.getOutputStream();

        //4.边读边写
        byte[] buf = new byte[1024];
        int len = 0;

        while( (len = in.read(buf))!=-1  ){ 
            out.write(buf,0,len);
        }

        //5.流资源关闭
        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return;
}

5 ModelAndView

Spring MVC provides ModelAndView object data may be stored to the request field, the view may be provided. In fact, after the Spring MVC any processor adapter final executing the controller will return ModelAndView object. So this is a relatively low-level objects.

Code Example:

/**
 * 5)ModelAndView: 封装了Model数据和视图数据的对象
 */
@RequestMapping("/mv")
public ModelAndView mv(){
    ModelAndView mv = new ModelAndView();
    //设置模型数据
    mv.addObject("model","一点教程网");
    //设置视图数据
    mv.setViewName("index");
    return mv;
}

6 Return the Java object

Java objects returned here, may be ordinary JavaBean, Map or List can also be a collection and so on. It is generally desirable to return control of the Java object into a Json string only needs to return a Java object. We focus on the next chapter to explain: "Spring MVC JSON data conversion"

Download Source: https://pan.baidu.com/s/1sINJe9oIuysksT00LK31ng

file

Welcome to my attention that the public number :: tutorial. Get exclusive organize learning resources and push dry goods daily.
If you are interested in my tutorial series, you can focus on my website: yiidian.com

Guess you like

Origin www.cnblogs.com/yiidian/p/12602859.html