Spring Boot(3)之 Controller 参数传递

Spring Boot(3)之 Controller 参数传递,第1张

Spring Boot(3)之 Controller 参数传递 Controller的参数传递

获取URL中的参数

1、Thymeleaf模板


    
      
      Title
    
    
        mac
    

2、@PathVariable
package com.example.test1.controller;

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

@RestController
public class ParameterUsage {

    // http://localhost:8080/article/12
    @RequestMapping("/article/{id}")
    public ModelAndView getArticle(@PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView("article/show");
        mav.addObject("content", id);
        return mav;
    }
}

2.1、URL自带参数 2.1.1、方法一
package com.example.test1.controller;

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

@RestController
public class ParameterUsage {
    
    // http://localhost:8080/addUser/?userName=zhangsan
    @RequestMapping("/addUser") 
    public ModelAndView addUser(String userName) {
        ModelAndView mav = new ModelAndView("article/show");
        mav.addObject("content", userName);
        return mav;
    }
}

2.1.2、方法二
package com.example.test1.bean;

import lombok.Data;

@Data
public class UserModel {
    private String name;
    private Integer age;
}

package com.example.test1.controller;

import com.example.test1.bean.UserModel;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class ParameterUsage {

    // http://localhost:8080/addUsers/?name=zhaosi&age=34
    @RequestMapping("/addUsers")
    public ModelAndView addUsers(UserModel user) {
        ModelAndView mav = new ModelAndView("article/show");
        mav.addObject("article", user);
        return mav;
    }
}

3、@ModelAttribute
package com.example.test1.controller;

import com.example.test1.bean.UserModel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class ParameterUsage {

    @RequestMapping("addUserm")
    public String addUserm(@ModelAttribute("user") UserModel user) {
        return user.toString();
    }
}

可以获取三种方式的数据:

FormPostGet

4、HttpServletRequest
package com.example.test1.controller;

import com.example.test1.bean.UserModel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RestController
public class ParameterUsage {

    @RequestMapping("addUserh")
    public ModelAndView addUserh(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("article/show");
        UserModel user = new UserModel();
        user.setName(request.getParameter("name"));
        user.setAge(Integer.parseInt(request.getParameter("age")));
        mav.addObject("article", user);
        return mav;
    }
}

获取方式同:1.3、@ModelAttribute

5、@RequestParam
package com.example.test1.controller;

import com.example.test1.bean.UserModel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RestController
public class ParameterUsage {

    @RequestMapping("addUserr")
    public ModelAndView addUserh(@RequestParam(value = "name", required = false) String name) {
        ModelAndView mav = new ModelAndView("article/show");
        mav.addObject("article", name);
        return mav;
    }
}

获取方式同:1.3、@ModelAttribute

6、@RequestBody(接收JSON格式)
package com.example.test1.controller;

import com.example.test1.bean.UserModel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RestController
public class ParameterUsage {

    @RequestMapping("saveUser")
    public String saveUser(@RequestBody List users) {
        return users.toString();
    }
}
7、MultipartFile(上传文件
@RestController
public class ParameterUsage {

    @RequestMapping("fileUpload")
    public String fileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "请选择文件");
            return "redirect:请选择文件";
        }
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(Objects.requireNonNull(file.getOriginalFilename()));
            Files.write(path, bytes);
            redirectAttributes.addFlashAttribute("message", "成功上传 '"+file.getOriginalFilename() +"'");
            return "redirect:成功上传 '"+file.getOriginalFilename() +"'";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:上传异常";
    }
}

完整JAVA文件如下
package com.example.test1.controller;

import com.example.test1.bean.UserModel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

@RestController
public class ParameterUsage {

    @RequestMapping("/article/{id}")
    public ModelAndView getArticle(@PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView("article/show");
        mav.addObject("article", id);
        return mav;
    }

    @RequestMapping("/addUser")
    public ModelAndView addUser(String userName) {
        ModelAndView mav = new ModelAndView("article/show");
        mav.addObject("article", userName);
        return mav;
    }

    @RequestMapping("/addUsers")
    public ModelAndView addUsers(UserModel user) {
        ModelAndView mav = new ModelAndView("article/show");
        mav.addObject("article", user);
        return mav;
    }

    @RequestMapping("addUserm")
    public String addUserm(@ModelAttribute("user") UserModel user) {
        return user.toString();
    }

    @RequestMapping("addUserh")
    public ModelAndView addUserh(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("article/show");
        UserModel user = new UserModel();
        user.setName(request.getParameter("name"));
        user.setAge(Integer.parseInt(request.getParameter("age")));
        mav.addObject("article", user);
        return mav;
    }

    @RequestMapping("addUserr")
    public ModelAndView addUserh(@RequestParam(value = "name", required = false) String name) {
        ModelAndView mav = new ModelAndView("article/show");
        mav.addObject("article", name);
        return mav;
    }

    @RequestMapping("saveUser")
    public String saveUser(@RequestBody List users) {
        return users.toString();
    }

    @RequestMapping("fileUpload")
    public String fileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "请选择文件");
            return "redirect:请选择文件";
        }
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(Objects.requireNonNull(file.getOriginalFilename()));
            Files.write(path, bytes);
            redirectAttributes.addFlashAttribute("message", "成功上传 '"+file.getOriginalFilename() +"'");
            return "redirect:成功上传 '"+file.getOriginalFilename() +"'";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:上传异常";
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5707108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存