SpringMVC-数据提交

数据提交

1. 前端的参数与controller中的参数名一致

可以直接用

package com.wang.controller;

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

@Controller
@RequestMapping("/user")
public class UserController {

    //localhost: 8080/user/t1?name=xxx
    //前端的参数名与此处一致,可以直接用
    @GetMapping("/t1")
    public String test(String name, Model model) {
        //1. 接受前端参数
        System.out.println("接受到前端的参数为: " + name);
        //2. 将返回的结果传递给前端
        model.addAttribute("msg", name);
        return "test";
    }
}

2. 前端的参数与controller中的参数名不一致

推荐无论一不一样, 都加上@RequestParam("前端的参数名")

package com.wang.controller;

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

@Controller
@RequestMapping("/user")
public class UserController {

//    //localhost: 8080/user/t1?name=xxx
//    //前端的参数名与此处一致,可以直接用
//    @GetMapping("/t1")
//    public String test(String name, Model model) {
//        //1. 接受前端参数
//        System.out.println("接受到前端的参数为: " + name);
//        //2. 将返回的结果传递给前端
//        model.addAttribute("msg", name);
//        return "test";
//    }

    //localhost: 8080/user/t1?username=xxx
    //前端的参数名与此处不一致, 需要加注解
    @GetMapping("/t1")
    public String test(@RequestParam("username") String name, Model model) {
        //1. 接受前端参数
        System.out.println("接受到前端的参数为: " + name);
        //2. 将返回的结果传递给前端
        model.addAttribute("msg", name);
        return "test";
    }
}

3. 前端接收的是一个对象

package com.wang.controller;

import com.wang.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/user")
public class UserController {

    //前端接收的是一个对象 : id, name, age
    //localhost: 8080/user/t1?id=XXX&name=xxx&age=xxx
    /*
    1. 接受前端用户传递的参数, 判断参数的名字, 假设名字直接在方法上, 可以直接使用
    2. 假设传递的是一个对象User, 匹配User对象中的字段名: 如果名字一致则OK, 否则, 匹配不到
     */

    @GetMapping("/t3")
    public String test3(User user, Model model) {

        System.out.println(user);

        model.addAttribute("msg", user.toString());

        return "test";
    }
}

4. 总结

1. 接受前端用户传递的参数, 判断参数的名字, 假设名字直接在方法上, 可以直接使用
2. 假设传递的是一个对象User, 匹配User对象中的字段名: 如果名字一致则OK, 否则, 匹配不到

5. 数据显示到前端

1. 通过ModelAndView

public class ControllerTest1 implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //返回一个模型视图对象
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","ControllerTest1");
        mv.setViewName("test");
        return mv;
    }
}

2. 通过Model

@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name, Model model){
    //封装要显示到视图中的数据
    //相当于req.setAttribute("name",name);
    model.addAttribute("msg",name);
    System.out.println(name);
    return "test";
}

3. 通过ModelMap

graph LR id1[LinkedHashMap] id2[ModelMap] id3[Model] id1 --继承了LinkedHashMap, 拥有LinkedHashMap的全部功能--> id2 id2 --精简版, 大部分情况下我们直接使用Model--> id3
@GetMapping("/t4")
public String test4 (@RequestParam("username") String name, ModelMap map) {

    map.addAttribute("msg", name);
    System.out.println(name);
    
    return "test";
}

4. 对比

  • Model 只有寥寥几个方法只适合用于储存数据,简化了新手对于Model对象的操作和理解

  • ModelMap 继承了 LinkedMap ,除了实现了自身的一些方法,同样的继承 LinkedMap 的方法和特性

  • ModelAndView 可以在储存数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转

原文地址:https://www.cnblogs.com/wang-sky/p/13633773.html