SpringMVC的Controller的返回值与接收的参数

内容参考自博客:

http://blog.csdn.net/u011001084/article/details/52846791

http://blog.csdn.net/xuxiaoyinliu/article/details/50349011

https://www.cnblogs.com/best/p/5669010.html#_lab2_1_0

Controller接收的参数:

  基本数据类型像int,spring是可以直接转换的方法,当参数名与http中请求的参数名称相同时会进行自动映射。

  String也是一样的。

  然后是非String的引用型,像Integer,传个3过来Spring也是会转换的。

  当参数为实体类的时候,像 

   @RequestMapping("xxx")public String sent(Book s)     (Book有String bookName float price 两个属性)

  这种情况服务器会将Book类实例化(所以如果没有@RequestParam(required=false)实体引用类参数在服务器端得到的结果不可能为null),在该类中找到其set方法所对应的所有属性,如果地址中有对应的属性就会对其赋值,反之采用默认值,这时参数相当于

  @RequestMapping("xxx")public String sent(String bookName, Float price)     

  甚至可以是复杂数据类型,就是指一个自定义的类里面还有其他的对象类型。具体的自己尝试。

  还可以是List,但不能直接在action中指定List<T>类型,要定义一个类把list集合包装在其中。看个例子:

  

package com.zhangguo.springmvc03.entities;

import java.util.List;

//产品集合
public class ProductList {
    private List<Product> items;

    public List<Product> getItems() {
        return items;
    }

    public void setItems(List<Product> items) {
        this.items = items;
    }
}

  action的代码:

  // 集合类型
    @RequestMapping("/action03")
    public String action03(Model model, ProductList products) {
        model.addAttribute("message", products.getItems().get(0) + "<br/>" + products.getItems().get(1));
        return "foo/index";
    }

 

  还有Map也是类似的道理

  

  再讲讲这个@RequestParam注解,简单的参数可以用自动映射,复杂的可以考虑用这个注解来解决问题 ,像我们刚才的List是不能直接完成自动绑定的,但通过这个注解就可以了   @RequestParam有四个参数:

  1、String name

  2、String value

  3、boolean required

  4、String defaultValue

  其中name和value都是一样的,就是url里面请求参数为什么名字才传进来,相当于以前的getParameter(name);然后这个required就是是否需要;sefaultValue是如果客户没有请求这个数据,那么它的默认值为多少。

  看个list绑定的例子:

  

// List集合与数组类型
    @RequestMapping("/action05")
    public String action05(Model model, @RequestParam("u") List<String> users) {
        model.addAttribute("message", users.get(0) + "," + users.get(1));
        return "foo/index";
    }

 

  表单也同样可行:

  

<form action="bar/action11" method="post">
    <p>
        <label>爱好:</label> 
        <input type="checkbox" value="15" name="id" />阅读
         <input type="checkbox" value="20" name="id" />上网
         <input type="checkbox" value="73" name="id" />电游
    </p>
    <button>提交</button>
</form>

  action代码:

 // List与数组绑定基本数据类型
    @RequestMapping("/action11")
    public String action11(Model model, @RequestParam("id") List<Integer> ids) {
        model.addAttribute("message", Arrays.deepToString(ids.toArray()));
        return "bar/index";
    }

  

  其他的继续在参考博客看吧。。

Controller的返回值:

  ModelView,可以包括数据模型和视图路径。

  view,这个时候如果在渲染页面的过程中模型的话,就会给处理器方法定义一个模型参数,然后在方法体里面往模型中添加值。可以返回pdf excel等,暂时没详细了解。

  Map

@RequestMapping(method=RequestMethod.GET)

   public Map<String, String> index(){

        Map<String, String> map = new HashMap<String, String>();

        map.put("1", "1");

        //map.put相当于request.setAttribute方法

        return map;

   }

   PS:响应的view应该也是该请求的view。等同于void返回。

  

@RequestMapping("/demo2/show")  
    publicMap<String, String> getMap() {  
        Map<String, String> map = newHashMap<String, String>();  
        map.put("key1", "value-1");  
        map.put("key2", "value-2");  
        returnmap;  
 }  

   在jsp页面中可直通过${key1}获得到值, map.put()相当于request.setAttribute方法。写例子时发现,key值包括 - . 时会有问题.

  String,默认如果action(Controller)返回String,此时的String为视图名称,会去视图解析器设定的目录下查找。                                                                                                                                             String还可以表示redirect重定向:

  public String testController(Model model){

    return "redirect:path";//path代表重定向的地址

  }                             

  如果你想直接写内容到网页中,那么就要用@ResponseBody,这时就会将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMessageConverter的Adapter转换对象,写入输出流。些时的String不再是路径而是内容,示例脚本如下:

                                                                                                                                                

  

@RequestMapping("/action32")
    @ResponseBody
    public String action32()
    {
        return "not <b>path</b>,but <b>content</b>";
    }

  

  void,void在普通方法中是没有返回值的意思,但作为请求处理方法并非这样,存在如下两种情况:

    当方法没有返回值时,方法中并未指定视图的名称,则默认视图的名称为方法名,如下代码所示:

 @RequestMapping("/action33")
    public void action33()
    {
    }

    直接会去访问的路径是:url=/WEB-INF/views/bar/action33.jsp,bar是当前控制器映射的路径,action33是方法名,上面的代码等同于:

@RequestMapping("/action33")
    public String action33()
    {
        return "bar/action33";  //bar是控制器的路径
    }

    当方法的返回值为void,但输出流中存在输出内容的时候,不会去查找视图,而是将输入流中的内容直接响应到客户端,响应的内容类型是纯文本,如下代码所示:

 @RequestMapping("/action34")
    public void action34(HttpServletResponse response) throws IOException
    {
        response.getWriter().write("<h2>void method</h2>");
    }

    

   

   返回对象实例,当返回值为自定义类型时Spring会把方法认为是视图名称,与返回值为void的类似办法处理URL,但页面中获得数据比较麻烦,示例代码如下:

 @RequestMapping("/action39")
    public Product action39()
    {
        return new Product(1,"iPhone",1980.5);
    }

  跟void一样的解决方式,说明以方法名为视图路径名称

  如果在action上添加@ResponseBody注解则返回的是Product本身,而非视图,Spring会选择一个合适的方式解析对象,默认是json。示例代码如下:

@RequestMapping("/action39")
    @ResponseBody
    public Product action39()
    {
        return new Product(1,"iPhone",1980.5);
    }

   

 附: 

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


  Model model,HttpServletRequest request, ModelMap map声明变量

  request.getSession().setAttribute("test", "haiwei2Session");
  request.setAttribute("test", "haiwei1request");
  map.addAttribute("test", "haiweiModelMap");
  model.addAttribute("test", "haiweiModel");

  我通过${test}这个方式取值,优先取Model和ModelMap的,Model和ModelMap是同一个东西,谁最后赋值的就取谁的,然后是request,最后是从session中获取

原文地址:https://www.cnblogs.com/wangshen31/p/8477563.html