spring MVC controller中的返回值

介绍


 

controller中的返回值包括下面四种:

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

那么我们将在这篇文章中介绍这几个返回值的使用方法.

参考文献:http://www.monkey1024.com/framework/1281

 

返回值的使用方法


 

1.返回ModelAndView

ModelAndView在controller中主要有以下几个小动作:

1)添加数据

2)完成资源的跳转

2.返回String类型

  • 跳转jsp或者完成资源跳转
  • 如果我们要跳转到内部的资源即项目中的jsp页面,还是跟前面的一样.配置文件不需要改动.
  • 如果我们要跳转到外部的资源上,比如www.baidu.com这个页面,那么,我们在配置文件上就需要做出改变了,添加如下代码:(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.baidu.com/"/>
</bean>

 

注:其中,id是controller中的返回值.value则是我们要跳转的页面.一定要把外部资源view对象的设置放在内部资源的配置的上面,不然会导致访问外部资源时仍然被默认为访问内容部的资源.

package com.dss.controller;

import org.springframework.web.bind.annotation.RequestMapping;

@org.springframework.stereotype.Controller
public class Controller {

    @RequestMapping("/baidu.do")
    public String baidu()throws Exception{

        return "baidu";
    }
}

 

3.返回值为Model对象

  Model对象的返回值可以被用来传递数值

package com.dss.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ModelController {

    @RequestMapping("/modeldata.do")
    public String modelData(Model model,String username){
        model.addAttribute("username",username);

        return "welcome";
    }
}

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${username}

</body>
</html>

 

4.自定义类型

很少用,不说

5.Object类型

  返回字符串

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 方法返回Object类型
 */
@Controller
public class ReturnObjectController01 {

    @RequestMapping(value = "/returnString.do" ,produces = "text/html;charset=utf-8")
    @ResponseBody
    public Object returnString() throws Exception{

        return "小猴子1024";

    }
}

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>Title</title>
    <script src="/js/jquery-3.3.1.js"></script>
</head>
<body>
    <button id="ajaxRequest">提交</button>
</body>
<script>
    $(function () {
       $("#ajaxRequest").click(function () {
           $.ajax({
               method:"post",
               url:"/returnString.do",
               success:function (result) {
                   alert(result);
               }
           });
       });
    });

</script>
</html>

 

  返回map类型

@Controller
public class ReturnObjectController01 {

    @RequestMapping(value = "/returnString.do")
    @ResponseBody
    public Object returnString() throws Exception{

        Map<String, String> map = new HashMap<>();
        map.put("hello", "你好");
        map.put("world", "世界");

        return map;

    }
}

 

$(function () {
       $("#ajaxRequest").click(function () {
           $.ajax({
               method:"post",
               url:"/returnString.do",
               success:function (result) {
                   alert(result.hello);
               }
           });
       });
    });

 

  还有其他很多类型可以返回的呢.

 

posted on 2018-08-01 17:16  董大志  阅读(243)  评论(0)    收藏  举报

导航