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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM