案例13 Spring MVC参数传递案例

基于Spring MVC实现HttpServletRequest、基本数据类型、Java Bean、数组、List、Map、JSON方式的参数传递。

1. 创建项目

选择Maven快速构建web项目,项目名称为case13-springmvc02。

案例13 Spring MVC参数传递案例_第1张图片

2. 配置Maven依赖



  4.0.0
  com.wfit.springmvc
  springmvc02
  1.0-SNAPSHOT
  war
  
  
    UTF-8
    1.8
    1.8
  

  
    
    
      org.springframework
      spring-webmvc
      5.3.25
    
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.12.3
    
  

3. 更新Maven仓库

案例13 Spring MVC参数传递案例_第2张图片

4. 创建java和resources目录

src.main路径下,执行new – Directory操作,选择java、resources后,执行回车键。

案例13 Spring MVC参数传递案例_第3张图片

5. 创建Spring MVC配置文件



    
    
    
    

6. 配置web.xml

在web.xml中配置DispatcherServlet。



  
  
    encoding
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
  
  
    encoding
    /*
  
  
  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    
    1
  
  
    dispatcherServlet
    
    /
  

7. 创建Student实体类

src.main.java.com.wfit.entity目录下创建Student.java。

public class Student {

    public String id;
    public String name;
    public int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

8. 创建ParamController类

在src.main.java.com.wfit.param目录下创建ParamController.java。

HttpServletRequest参数传递

@Controller
@RequestMapping("/param")
public class ParamController {
    /**
     *  通过HttpServletRequest接收参数
     */
    @RequestMapping("/servlet")
    public void servlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        System.out.println("name=" + name);
        PrintWriter writer = response.getWriter();
        writer.print("hello " + name);
        writer.flush();
        writer.close();
    }
}

案例13 Spring MVC参数传递案例_第4张图片

基本数据类型参数传递

基本数据类型以及他们的包装类。

/**
 * 使用基本数据类型和String类型
 */
@RequestMapping(value = "/simple",produces = "text/plain;charset=UTF-8")
@ResponseBody
public String simple(String id,String name,int age){
    String str = id + "," + name + "," + age;
    System.out.println(str);
    return str;
}

案例13 Spring MVC参数传递案例_第5张图片

Java Bean参数传递

@Controller
@RequestMapping("/param")
public class ParamController {
    /**
     * JavaBean传递参数
     */
    @RequestMapping(value = "/bean",method = RequestMethod.POST,
            produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String bean(Student student){
        System.out.println(student);
        return student.toString();
    }
}

案例13 Spring MVC参数传递案例_第6张图片

数组参数传递

@Controller
@RequestMapping("/param")
public class ParamController {
    /**
     * 数组参数
     */
@RequestMapping(value = "/array",method = RequestMethod.POST)
@ResponseBody
public String array(String[] hobbies){
    for(String hobby : hobbies){
         System.out.println(hobby);
    }
    return "success";
}
}

案例13 Spring MVC参数传递案例_第7张图片

List参数传递

@Controller
@RequestMapping("/param")
public class ParamController {
    /**
     * List参数
     */
@RequestMapping(value = "/list",method = RequestMethod.POST)
@ResponseBody
public String list(@RequestParam List hobbyList){
    hobbyList.forEach(hobby -> System.out.println(hobby));
    return "success";
}
}

案例13 Spring MVC参数传递案例_第8张图片

Map参数传递

@Controller
@RequestMapping("/param")
public class ParamController {
    /**
     * Map参数
     */
@RequestMapping(value = "/map",method = RequestMethod.POST)
@ResponseBody
public String map(@RequestParam Map map){
    System.out.println(map);
    return "success";
}
}

案例13 Spring MVC参数传递案例_第9张图片

9. 创建JsonController类

在src.main.java.com.wfit目录下创建JsonController类,实现JSON参数转成Java Bean、Map、List等引用类型。

Json Bean参数传递

@Controller
@RequestMapping("/json")
public class JsonController {
    /**
     * json bean
     */
    @RequestMapping(value = "/bean",method = RequestMethod.POST)
    @ResponseBody
    public Object bean(@RequestBody Student student){
        System.out.println(student);
        Map map = new HashMap();
        map.put("code",200);
        map.put("msg","success");
        map.put("data",student.toString());
        return map;
    }
}

案例13 Spring MVC参数传递案例_第10张图片

Json Map参数传递

@Controller
@RequestMapping("/json")
public class JsonController {
    /**
     * json map
     */
    @RequestMapping(value = "/map",method = RequestMethod.POST)
    @ResponseBody
    public Object map(@RequestBody Map jsonMap){
        System.out.println(jsonMap);
        Map map = new HashMap();
        map.put("code",200);
        map.put("msg","success");
        map.put("data",jsonMap);
        return map;
    }
}

案例13 Spring MVC参数传递案例_第11张图片

Json List参数传递

@Controller
@RequestMapping("/json")
public class JsonController {
    /**
     * json list
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody List studentList){
        System.out.println(studentList);
        Map map = new HashMap();
        map.put("code",200);
        map.put("msg","success");
        map.put("data",studentList);
        return map;
    }
}

案例13 Spring MVC参数传递案例_第12张图片

你可能感兴趣的:(SpringBoot入门实战,java,spring,mvc)