Controller中方法的返回值(上)

Controller中方法的返回值类型

在我们之前写的Controller的方法中,返回值都写的是ModelAndView,其实还可以返回其他类型的对象,在实际应用中需要根据不同的情况来使用不同的返回值:

  • ModelAndView
  • String
  • void
  • 自定义类型

返回ModelAndView

先来看下ModelAndView,这个是我们之前一直使用的返回值,如果Controller的方法执行完毕后,需要跳转到前台页面或其他资源,且又要传递数据, 此时方法返回ModelAndView比较方便。
如果只传递数据,或者只跳转前台页面或其他资源的话,使用ModelAndView就显得有些多余了。

返回String类型

如果controller中的方法在执行完毕后,需要跳转到前台页面或者其他资源上,此时就可以让该方法返回String类型。

下面代码中已经提前配置好了视图解析器。

返回内部资源

这里指定是跳转到我们自己的域名中的页面上。

创建一个Controller,方法返回String类型:

package com.monkey1024.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 方法返回String类型
 */
@Controller
public class ReturnStringController01 {

    @RequestMapping("/welcome")
    public String welcome() throws Exception{

        //直接填写要跳转的页面的名称
        return "welcome";
    }
}

编写welcome页面:

<html>
<head>
    <title>Title</title>
</head>
<body>

欢迎你学习java!
</body>
</html>

在浏览器的地址栏中输入:

localhost:8080/welcome

则可以看到welcome.html中的内容。

返回外部资源

这里指的是跳转到其他域名的页面上。

如果你需要在controller的方法中跳转到外部资源,比如跳转到:

http://www.monkey1024.com/

此时需要在springmvc.xml文件中配置一个BeanNameViewResolver类,这个类被称作是视图解析器。在springmvc.xml文件中添加下面内容:

<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

<!--定义外部资源view对象-->
<bean id="monkey1024" class="org.springframework.web.servlet.view.RedirectView">
    <property name="url" value="http://www.monkey1024.com/"/>
</bean>

其中id是controller中的方法返回值,value是要跳转的外部资源的地址。

之后修改controller中的方法返回值:

@RequestMapping("/welcome")
public String welcome() throws Exception{

    //直接填写要跳转的页面的名称
    return "monkey1024";
}

此时在浏览器中输入:

localhost:8080/welcome

页面会跳转到

http://www.monkey1024.com/

这个就是跳转到外部资源的方式。

Model对象

之前简单说过这个Model,它是一个接口,写在controller的方法中的时候,spring mvc会为其进行赋值。我们可以使用Model对象来传递数据,也就是说我们可以使用Model传递数据并且将方法返回值设置为String类型,通过这种方式实现与方法返回ModelAndView一样的功能。

@RequestMapping("/welcome1")
public String welcome1(String name,Model model) throws Exception{

    //这种写法spring mvc会自动为传入的参数取名
    model.addAttribute(name);
    model.addAttribute("username", name);

    //直接填写要跳转的页面的名称
    return "welcome";
}

在welcome.jsp中添加下面内容:

${username}<br>
${string}<br>

从浏览器的url中输入:

http://localhost:8080/welcome1?name=jack

可以看到页面中会显示两个jack

上面controller中使用了下面的写法:

model.addAttribute(name);

这种写法spring mvc会根据传入的参数对其进行取名,此时传入的参数name是一个String类型,因此会给他取名为string,即类似如下写法:

model.addAttribute("string", name);

这里面spring mvc的取名方式就是根据传入参数的类型来取名的,例如:

传入com.monkey1024.Product类型,会将其命名为"product"
com.monkey1024.MyProduct 命名为 "myProduct"

另外在Model接口中还有两个方法:

  • addAllAttributes(Collection<?> attributeValues);
    会将传入的list中的数据对其进行命名,例如:

    List integerList = new ArrayList<>();
    integerList.add(1);
    integerList.add(5);
    integerList.add(3);
    model.addAllAttributes(integerList);

上面代码相当于:

model.addAttribute("1", 1);
model.addAttribute("5", 5);
model.addAttribute("3", 3);
  • addAllAttributes(Map<String, ?> attributes);
    会将map中的key作为名字,value作为值放入到model对象中,例如:

    Map<String, Integer> integerMap = new HashMap<>();
    integerMap.put("first", 1);
    integerMap.put("second", 2);
    integerMap.put("third", 3);
    model.addAllAttributes(integerMap);

上面代码相当于:

model.addAttribute("first", 1);
model.addAttribute("second", 2);
model.addAttribute("third", 3);