对象属性赋值方式

1:Bean封装前台提交数据

index.jsp前台数据提交页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>

<form action="/regist.do" method="post">
  姓名:<input type="text" name="username">
  <br>
  年龄:<input type="text" name="age">
  <br>
  <input type="submit" value="提交">
</form>
</body>
</html>

Bean Class文件,参数名称必须与前台数据提交参数一致

package com.davis.bean;

public class Student {
  private String username;
  private int age;

  public String getUsername() {
      return username;
  }

  public void setUsername(String username) {
      this.username = username;
  }

  public int getAge() {
      return age;
  }

  public void setAge(int age) {
      this.age = age;
  }
}

Controller方法带Bean参数,前台参数自动匹配并赋值Bean参数

package com.davis.controller;

import com.davis.bean.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Params {
  @RequestMapping("/regist.do")
  public ModelAndView getParams03(Student student)throws Exception{
      //方法带Bean参数,前台参数自动匹配并赋值Bean参数
      ModelAndView mv=new ModelAndView();
      mv.addObject("username",student.getUsername());
      mv.addObject("age",student.getAge());
      mv.setViewName("first");
      return mv;
  }
}

first.jsp前台响应页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>FirstJsp</title>
</head>
<body>
${hello}
姓名:${username}
<br>
年龄:${age}
</body>
</html>

2:Bean封装前台提交数据

index.jsp前台数据提交页面 (加多了学校信息),注意学校信息的命名方式

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>

<form action="/regist.do" method="post">
  姓名:<input type="text" name="username">
  <br>
  年龄:<input type="text" name="age">
  <br>
  学校名称:<input type="text" name="school.schoolname">
  <br>
  学校地址:<input type="text" name="school.address">
  <br>
  <input type="submit" value="提交">
</form>
</body>
</html>

Bean Class文件,参数名称必须与前台数据提交参数一致,另外加了个school对向,school是我们封装的bean学校信息

package com.davis.bean;

public class Student {
  private String username;
  private int age;
  private school school;

  public com.davis.bean.school getSchool() {
      return school;
  }

  public void setSchool(com.davis.bean.school school) {
      this.school = school;
  }

  public String getUsername() {
      return username;
  }

  public void setUsername(String username) {
      this.username = username;
  }

  public int getAge() {
      return age;
  }

  public void setAge(int age) {
      this.age = age;
  }
}

Bean Class文件,参数名称必须与前台数据提交学校参数一致

package com.davis.bean;

public class school {
  private String schoolname;
  private String address;

  public String getSchoolname() {
      return schoolname;
  }

  public void setSchoolname(String schoolname) {
      this.schoolname = schoolname;
  }

  public String getAddress() {
      return address;
  }

  public void setAddress(String address) {
      this.address = address;
  }
}

Controller方法带Bean参数,前台参数自动匹配并赋值Bean参数

package com.davis.controller;

import com.davis.bean.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Params {
  @RequestMapping("/regist.do")
  public ModelAndView getParams04(Student student)throws Exception{
      //方法带Bean参数,前台参数自动匹配并赋值Bean参数
      ModelAndView mv=new ModelAndView();
      mv.addObject("username",student.getUsername());
      mv.addObject("age",student.getAge());
      mv.addObject("schoolname",student.getSchool().getSchoolname());
      mv.addObject("address",student.getSchool().getAddress());
      mv.setViewName("first");
      return mv;
  }
}

first.jsp前台响应页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>FirstJsp</title>
</head>
<body>
姓名:${username}
<br>
年龄:${age}
  <br>
学校名称:${schoolname}
<br>
学校地址:${address}
<br>
</body>
</html>