栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springmvc获取前台请求数据过程解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springmvc获取前台请求数据过程解析

1)基本数据类型或String,在方法参数中定义参数,参数名与请求传递数据名一致即可自动封装;

// RequestMapping:指定方法对应的请求地址
  //return:页面地址,表示方法执行完成之后跳转到对应的页面(转发)
  //springmvc:接收请求参数,直接在方法的参数中定义名称与传递参数名一致的形参即可
  //name:会自动接收请求传递的name值
  @RequestMapping("/hello")
  public String hello(String name,Integer age){
    System.out.println("name:"+name+",age:"+age);
    return "index.jsp";
  }

启动tomcat,在浏览器地址栏输入

http://localhost:8086/hello?name=tom&age=18

即可查看结果;

2)对象类型的,在方法参数中定义对象,与对象属性名一致的数据,会自动封装进对象;

public class User {

  private Integer id;
private String name;
private Integer age;
private String sex;
private String addr;
}

并对其get和set,再写一个form表单提交信息的jsp页面;


再在controller中测试结果;

    @RequestMapping("/saveUser")
    public String saveUser(User user){
System.out.println("User:"+user);
return "index.jsp";
    }

3)接收数组的情况。在方法中定义数组;

4)对象中的数组和集合可以接收与集合属性同名的多个请求数据;

  //通过对象来接收前台的多个同名数据,两种方式,一种是数组,一种是List集合;
  //private String[] hobbies;
   private List hobbies;
  public String[] getHobbies() {
    return hobbies;
  }

  public void setHobbies(String[] hobbies) {
    this.hobbies = hobbies;
  }

  public List getHobbies() {
    return hobbies;
  }

  public void setHobbies(List hobbies) {
    this.hobbies = hobbies;
  }

jsp页面中添加爱好属性:

爱好: 打篮球
打游戏
敲代码
学习

再在controller中测试结果;

@RequestMapping("/saveUser")
  public String saveAdmin(User user, String[] hobbies){
    System.out.println("user:"+user);
    System.out.println(Arrays.toString(hobbies));
    return "index.jsp";
  }

5)对象中的对象。 前台指定name 时,属性.属性;

要测试对象中的对象,那我们要另外再建立一个对象,然后把这个对象当成属性添加到User.java中去;

public class Role {
  private String name;
  private Integer id;
  @Override
  public String toString() {
    return "Role{" +
 "name='" + name + ''' +
 "id='" + id + ''' +
 '}';
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
}

User.java中:

 //用来接收前台的数据
  private Role role;
  public Role getRole() {
    return role;
  }

  public void setRole(Role role) {
    this.role = role;
  }

jsp文件中:

<%-- 将属性封装到对象的对象属性中 属性名.属性--%>
角色:


测试:

public String saveUser(User user, String[] hobbies){
    System.out.println("User:"+user);
    System.out.println(Arrays.toString(hobbies));
    System.out.println("role:"+user.getRole()); 
    return "index.jsp"; 
}

6)将数据封装到map中。在方法中定义HashMap参数,并在前面添加@RequestParam注解;

7)封装到对象中的map,前台指定name为 map属性名[key值];

User.java中:

  //通过map接收前台的值
  private Map conditions;
  public Map getConditions() {
    return conditions;
  }
  public void setConditions(Map conditions) {
    this.conditions = conditions;
  }

jsp文件中:

<%--将数据封装到对象中的map,map属性名[key]--%>
查询条件:

查询条件:


测试:

//map :将所有请求数据封装到map中
  // @RequestParam("name"):指定向请求中获取数据 ==>request.getParameter
  //required = false:是否必传  defaultValue:默认值
  @RequestMapping("/saveUser")
  public String saveUser(User user, String[] hobbies,
@RequestParam(name="name",required = false,defaultValue = "username") String username,
@RequestParam HashMap map){
    System.out.println("User:"+user);
    System.out.println("role:"+user.getRole());
    System.out.println(Arrays.toString(hobbies));
    System.out.println("map:"+map);
    System.out.println("username:"+username);    System.out.println("conditions:"+user.getConditions());    return "index.jsp"; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/133439.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号