SpringMVC06以对象的方式获取前台的数据

========创建需要的两个实体类================

/**
 * @author 小豆腐
 */
public class Student {

      private  String  name;
      private  int  age;
      //学生的老师
      private  Teacher teacher;
       
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Student() {
        super();
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
    
}
public class Teacher {  //教师的实体类
      private  String  name;
      private  int  age;
      
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Teacher(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Teacher() {
        super();
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}

=======需要的两个页面================

  <body>
   <form action="student/addStudent"  method="post">
     学生姓名:<input type="text" name="name"/>
    年龄:  <input type="password" name="age"/>
<%--  teacher.name :teacher是  student类中的域属性 --%>
  老师姓名:<input type="text" name="teacher.name"/>
      <input type="submit" value="新增"/>
   </form> 
    
  </body>

springmvc.xml文件的内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
  <!-- 配置springmvc的组件  -->
  <context:component-scan base-package="cn.bdqn.controller"/>
  
  
  <!--  视图解析器      后台返回的是  success!  应该拿到的是  /WEB-INF/jsp/success.jsp   -->
  <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/jsp/"/>
     <property name="suffix" value=".jsp"/>
  </bean>
  
</beans>

controller中的内容

@Controller
@RequestMapping("/student")
public class StudentController {
    
    /**
     * 新增student
     * 01.通过request获取前台
     * @return   视图 加上  数据
     */
    @RequestMapping("/add")
    public  ModelAndView add(HttpServletRequest request,HttpServletResponse response){
         //获取前台用户的输入
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        //创建一个MV
        ModelAndView  mv=new ModelAndView();
        //增加数据  之后  前台使用 el表达式 接收
        mv.addObject("name", name).addObject("age",age);
        //设置返回页面
        mv.setViewName("success");
        return  mv;
    }
    
    /**
     * 02.从请求中获取参数
     *   注意点:
     *    参数名称 必须和前台页面中的name属性值 一致!
     */
    @RequestMapping("/add2")
    public  ModelAndView  add2(String name,int age){
        System.out.println("进入了add2...............");
        //创建一个MV
        ModelAndView  mv=new ModelAndView();
        //增加数据  之后  前台使用 el表达式 接收
        mv.addObject("name", name).addObject("age",age);
        //设置返回页面
        mv.setViewName("success");
        return  mv;
    }
    
    /**
     * 校正参数
     *student/add3?names=xxx&age=xxx
     * @RequestParam("names")names:就是前台传递来的参数! 必须写在校正参数之前!
     */
    @RequestMapping("/add3")
    public  ModelAndView  add3(@RequestParam("names")String name,int age){
        System.out.println("进入了add3...............");
        //创建一个MV
        ModelAndView  mv=new ModelAndView();
        //增加数据  之后  前台使用 el表达式 接收
        mv.addObject("name", name).addObject("age",age);
        //设置返回页面
        mv.setViewName("success");
        return  mv;
    }
    
    /**
     * 对象整体的传递!   但是 需要注意的是!
     *  前台页面中的name属性值要和对象的属性名 一致!
     */
    @RequestMapping("/addStudent")
    public  ModelAndView  addStudent(Student student){
        System.out.println("进入了addStudent...............");
        System.out.println("老师的姓名:"+student.getTeacher().getName());
        //创建一个MV
        ModelAndView  mv=new ModelAndView();
        //增加数据  之后  前台使用 el表达式 接收
        mv.addObject("student",student);
        //设置返回页面
        mv.setViewName("success");
        return  mv;
    }
    
    
    
    
    
}

成功页面

  <body>
     成功界面!
     <br/>
   用户名:    ${name}<br/>
   年龄:   ${age}
   <hr/>
   学生的用户名:${student.name}<br/>
   学生的年龄:${student.age}<br/>
 老师的姓名:${student.teacher.name}  
   
  </body>

================路径变量=====================

创建对应的页面

  <body>
   <a href="user/2/张三/add">add</a>
  </body>
index.jsp页面
  <body>
  id========> ${id} <br>
  name========> ${name} <br>
  </body>
success.jsp页面
@Controller
@RequestMapping("/user")
public class MyController {
    /**
     * @PathVariable 这个注解使用来获取 路径变量的!
     * 不同于之前的?参数
     * 想获取路径变量 必须使用@PathVariable  
     */
    @RequestMapping(value = "/{id}/{name}/add")
    public ModelAndView add(@PathVariable int id, @PathVariable String name) {
        System.out.println("进入了 add.....");
        System.out.println(name);
        System.out.println(id);

        // 获取前台输入的值
        ModelAndView mv = new ModelAndView();
        mv.addObject("id", id).addObject("name", name);
        mv.setViewName("/WEB-INF/jsp/success.jsp");
        return mv;
    }

}
对应的controller
原文地址:https://www.cnblogs.com/999-/p/6133441.html