首页 > 代码库 > SpringMVC06以对象的方式获取前台的数据

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

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

技术分享
public class School {
    private String sName;
    private String address;

    public String getsName() {
        return sName;
    }

    public void setsName(String sName) {
        this.sName = sName;
    }

    public String getAddress() {
        return address;
    }

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

    public School(String sName, String address) {
        super();
        this.sName = sName;
        this.address = address;
    }

    public School() {
        super();
    }

    @Override
    public String toString() {
        return "School [sName=" + sName + ", address=" + address + "]";
    }

}
school实体类
技术分享
public class Student {
    private String name;
    private Integer age;
    private School school;

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

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

    public Student(String name, Integer age, School school) {
        super();
        this.name = name;
        this.age = age;
        this.school = school;
    }

    public Student() {
        super();
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", school=" + school
                + "]";
    }

}
Student实体类

 

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

技术分享
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="http://www.mamicode.com/">
    
    <title>My JSP ‘index.jsp‘ starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">
    -->
  </head>
  
  <body>
  
  
   <form  action="user/userLogin"  method="post">
    <input  type="text" name="name">
    <input  type="text" name="age">
    <!-- 给关联的school对象赋值  需要加上域属性的名称. -->
        <input  type="text" name="school.sName">
    <input  type="text" name="school.address">
        <button type="submit">提交</button>
    </form>
  
  <!--
    中文的乱码 解决!
    01.get治本的: 在config文件下有server.xml中配置 URIEcoding="utf-8"
    02.post治本的: 创建一个过滤器!
  
  springMVC:给我们提供了一个Filter,只需要在web.xml中配置这个Filter即可!
    
    <form  action="user/login3"  method="post">
    <input  type="text" name="userName">
    <input  type="text" name="age">
        <button type="submit">提交</button>
    </form>-->
  </body>
</html>
index.jsp页面
技术分享
 <body>
   student========> ${student} <br>
  </body>
success.jsp页面
技术分享
@Controller
@RequestMapping("/user")
public class MyController {
    // 登录方法 01 通过请求 获取参数
    @RequestMapping(value = "http://www.mamicode.com/login")
    public ModelAndView login(HttpServletRequest request,
            HttpServletResponse response) {
        System.out.println("进入了 login.....");
        // 获取前台输入的值
        String name = request.getParameter("userName");
        String age = request.getParameter("age");
        ModelAndView mv = new ModelAndView();
        mv.addObject("name", name);
        mv.addObject("age", age);
        /**addObject返回值 就是一个ModleAndView
         * mv.addObject("name", name).addObject("age", age);
         */
        mv.setViewName("/WEB-INF/jsp/success.jsp");
        return mv;
    }

    /**
     * 登录方法 02 请求中的携带的参数
     * @param userName   index.jsp页面中的name属性值  必须一致
     * @param age   index.jsp页面中的name属性值 必须一致
     * @return
     */
    @RequestMapping(value = "/login2")
    public ModelAndView login2(String userName, int age) {
        System.out.println("进入了 login2.....");
        ModelAndView mv = new ModelAndView();
        /**
         * name ,age 是保存在modle中的数据,和success.jsp页面中
         * el表达式对应
         */
        mv.addObject("name", userName);
        mv.addObject("age", age);
        mv.setViewName("/WEB-INF/jsp/success.jsp");
        return mv;
    }

    /**
     * 请求中的携带的参数和后台接收属性名不一致
     * @RequestParam("userName")
     * 校正 参数,必须放在需要校正的参数之前
     * springmvc07/user/login3?userName=xx&age=xx
     */
    @RequestMapping(value = "/login3")
    public ModelAndView login3(@RequestParam("userName") String name, int age) {
        System.out.println("进入了 login3.....");
        ModelAndView mv = new ModelAndView();
        mv.addObject("name", name);
        mv.addObject("age", age);
        mv.setViewName("/WEB-INF/jsp/success.jsp");
        return mv;
    }

    /**
     * 对象整体的传递  而且还包含了域属性的赋值
     */
    @RequestMapping(value = "/userLogin")
    public ModelAndView userLogin(Student student) {
        System.out.println("进入了 userLogin.....");
        ModelAndView mv = new ModelAndView();
        mv.addObject("student", student);
        mv.setViewName("/WEB-INF/jsp/success.jsp");
        return mv;
    }
}
对应的controller页面

 

 

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

创建对应的页面

技术分享
  <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

 

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