1. 程式人生 > 實用技巧 >SpringMVC-資料提交

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 可以在儲存資料的同時,可以進行設定返回的邏輯檢視,進行控制展示層的跳轉