spring mvc环境值参数传递的方式(三)

spring mvc环境值参数传递的方式

1.传统的参数传递

  通过给控制器方法添加参数HttpServletRequest request,通过request.getParameter("参数名")获取,再封装到bean中。

  (如果没有HttpServletRequest 类,pom.xml文件加入)

    <!-- ServletAPI -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
@RequestMapping("/test01")
public ModelAndView test01(HttpServletRequest request){
      String username = request.getParameter("username");
      String password = request.getParameter("password");

      System.out.println(username);
      System.out.println(password);
      return null;
}

2.简单类型参数和RequestParam注解

  •   如果请求参数名和控制器形参同名,可直接接受。
  •       如果请求参数名和控制器形参不同,可以如下在注解中设置.
    @RequestMapping("index02")
    public String index02(@RequestParam(name="name",defaultValue = "123") String name) {
        System.out.println(name);
        return "index01";
    }

 

3.对象传递

可以把参数自动赋值到对象上

注意: 1.参数名必须和对象的属性名一致

    2.此时对象会直接放入request作用域中,名称为类型首字母小写

    3.@ModelAttribute设置请求参数绑定到对象中并传到视图页面,设置key值

    @RequestMapping("index03")
    public ModelAndView index03(@ModelAttribute("user") UserEntity user){
    /*可以使用对象作为方法的形参,同时接受接受前台的多个请求参数
      SpringMVC会基于同名匹配,将请求参数的值注入对应的对象中的属性中
      @ModelAttribute起名字key值*/
        System.out.println(user);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index01");
        return mv;
    }
<form action="./index/index03" method="get"  >
    <input type="text" name="id" />
    <input type="text" name="name" />
    <input type="text" name="age" />
    <input type="submit" value="submit" />
</form>

4.数组和List集合类型参数

  当前台页面传来的参数是参数名相同,参数值不同的多个参数时,可以直接封装到方法的数组类型的形参中,也可以直接封装到对象的集合属性中。

  比如批量删除时传来的参数。

    @RequestMapping("index04")
    public String test04(String id[], Model model){
    /*对于参数名相同的多个请求参数,可以直接使用数组作为方法的形参接收
      可以使用对象中的集合属性接收*/
        for (String i : id) {
            System.out.println(i);
        }
        model.addAttribute("id", id);
        return "index01";
    }
<form action="./index/index04" method="get"  >
    <input type="text" name="id" />
    <input type="text" name="id" />
    <input type="submit" value="submit" />
</form>
  •  如果spring mvc配置了支持json,在接收json数据时,可以转化为list
    @RequestMapping("index05")
    public String test05(@RequestBody List<UserEntity> users, Model model){
    /*对于参数名相同的多个请求参数,可以直接使用数组作为方法的形参接收
      可以使用对象中的集合属性接收*/
        System.out.println(users);
        for (UserEntity user: users) {
            System.out.println(user);
        }
        model.addAttribute("users", users);
        return "index01";
    }

请求字符串和实体类

[
    {"id":1,"name":"54"},
    {"id":2,"name":"5485"}
]

public class UserEntity {
    private  int id;
    private  String name;
    private  int age;
    ...
}

 

5、使用Restful

 

Restful是一种软件架构风格,严格上说是一种编码风格,其充分利用 HTTP 协议本身语义从而提供了一组设计原则和约束条件。

  主要用于客户端和服务器交互类的软件,该风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。 在后台,RequestMapping标签后,可以用{参数名}方式传参,同时需要在形参前加注解@PathVarible,假如前台的请求地址为localhost:8080/delete/1

    @RequestMapping("/delete/{id}")
    public ModelAndView test06(@PathVariable("id")Long id){
        System.out.println("delete");
        System.out.println(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index01");
        return mv;
    }

 

 

 

https://www.cnblogs.com/xfdhh/p/11517534.html

https://blog.csdn.net/qq_43842093/article/details/121318783

https://www.cnblogs.com/zytcomeon/p/13409975.html

 

posted @ 2022-11-29 15:46  与f  阅读(45)  评论(0编辑  收藏  举报