基于Spring MVC实现HttpServletRequest、基本数据类型、Java Bean、数组、List、Map、JSON方式的参数传递。
选择Maven快速构建web项目,项目名称为case13-springmvc02。
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
src.main路径下,执行new – Directory操作,选择java、resources后,执行回车键。
在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
/
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 +
'}';
}
}
在src.main.java.com.wfit.param目录下创建ParamController.java。
@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();
}
}
基本数据类型以及他们的包装类。
/**
* 使用基本数据类型和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;
}
@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();
}
}
@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";
}
}
@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";
}
}
@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";
}
}
在src.main.java.com.wfit目录下创建JsonController类,实现JSON参数转成Java Bean、Map、List等引用类型。
@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;
}
}
@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;
}
}
@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;
}
}