Spring MVC的Controller接收請求參數(shù)的方式有多種,本節(jié)主要介紹Spring MVC下的HttpServletRequest、基本數(shù)據(jù)類型、Java Bean、數(shù)組、List、Map、JSON參數(shù)傳遞方式,同時(shí)解決POST請求中文亂碼問題。
1. HttpServletRequest參數(shù)傳遞
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* 通過HttpServletRequest接收參數(shù)
*/
@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();
}
}
2. 基本數(shù)據(jù)類型參數(shù)傳遞
基本數(shù)據(jù)類型以及他們的包裝類。
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* 使用基本數(shù)據(jù)類型和String類型
*/
@RequestMapping("/simple")
@ResponseBody
public String simple(int id, String name) throws IOException {
String str = "id = " + id + ",name = " + name;
System.out.println(str);
return str;
}
}
3. Java Bean參數(shù)傳遞
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* JavaBean傳遞參數(shù)
*/
@RequestMapping(value = "/bean",
produces = "text/plain;charset=UTF-8",
method = RequestMethod.POST)
@ResponseBody
public String paramBean(Student student){
System.out.println(student);
return student.toString();
}
}
4. 數(shù)組參數(shù)傳遞
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* 數(shù)組參數(shù)
*/
@RequestMapping(value = "/array",method = RequestMethod.POST)
@ResponseBody
public String array(String[] hobbies){
for(String hobby : hobbies){
System.out.println(hobby);
}
return "success";
}
}
5. List參數(shù)傳遞
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* List參數(shù)
*/
@RequestMapping(value = "/list",method = RequestMethod.POST)
@ResponseBody
public String list(@RequestParam List<String> hobbyList){
hobbyList.forEach(hobby -> System.out.println(hobby));
return "success";
}
}
6. Map參數(shù)傳遞
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* Map參數(shù)
*/
@RequestMapping(value = "/map",method = RequestMethod.POST)
@ResponseBody
public String map(@RequestParam Map<String,Object> map){
System.out.println(map);
return "success";
}
}
7. JSON參數(shù)傳遞
JSON格式采用鍵值對的方式來表示數(shù)據(jù),由花括號 {} 包含對象,由方括號 [] 包含數(shù)組,可通過JSON在線解析工具進(jìn)行校驗(yàn)/格式化,具體格式如下:
-
對象
對象是一個(gè)無序的鍵/值對集合,每個(gè)鍵后面需要添加一個(gè)冒號(:),鍵/值對之間使用逗號(,)分隔。
{
"id": "1",
"name": "zhangsan",
"age":20
}
-
數(shù)組
數(shù)組可以包含多個(gè)元素,每個(gè)元素之間使用逗號(,)分隔,可以包含簡單值、對象或者其他數(shù)組。
[
{
"id": "1",
"name": "zhangsan"
},
{
"id": "2",
"name": "zhangsan"
}
]
Spring MVC可以使用Jackson框架作為JSON的轉(zhuǎn)換器。
通過@RequestBody可以將前端的JSON參數(shù)轉(zhuǎn)成Java Bean、Map、List等引用類型。
通過@ResponseBody可以將后端的Java Bean、Map、List等引用類型轉(zhuǎn)成JSON結(jié)果輸出。
使用Jackson時(shí)需要三個(gè)jar包jackson-databind.jar、jackson-core.jar、jackson-annotations.jar。如果Spring MVC創(chuàng)建的是Maven項(xiàng)目,僅需要依賴jackson-databind,因?yàn)樗鼈鬟f依賴了jackson-core和jackson-annotations。
@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<String,Object> map = new HashMap<String,Object>();
map.put("code",200);
map.put("msg","success");
map.put("data",student.toString());
return map;
}
/**
* json map
*/
@RequestMapping(value = "/map",method = RequestMethod.POST)
@ResponseBody
public Object map(@RequestBody Map<String,Object> jsonMap){
System.out.println(jsonMap);
Map<String,Object> map = new HashMap<String,Object>();
map.put("code",200);
map.put("msg","success");
map.put("data",jsonMap);
return map;
}
/**
* json list
*/
@RequestMapping("/list")
@ResponseBody
public Object list(@RequestBody List<Student> studentList){
System.out.println(studentList);
Map<String,Object> map = new HashMap<String,Object>();
map.put("code",200);
map.put("msg","success");
map.put("data",studentList);
return map;
}
}
8. Spring MVC解決POST請求中文亂碼問題
Spring提供了CharacterEncodingFilter解決POST請求中的中文亂碼問題,在web.xml中配置CharacterEncodingFilter。
<web-app>
<!--解決POST中文亂碼問題 過濾器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
9. Spring MVC參數(shù)傳遞案例
基于Spring MVC實(shí)現(xiàn)HttpServletRequest、基本數(shù)據(jù)類型、Java Bean、數(shù)組、List、Map、JSON方式的參數(shù)傳遞。文章來源:http://www.zghlxwxcb.cn/news/detail-642308.html
案例實(shí)現(xiàn)詳見鏈接:案例13 Spring MVC參數(shù)傳遞案例文章來源地址http://www.zghlxwxcb.cn/news/detail-642308.html
到了這里,關(guān)于3.5 Spring MVC參數(shù)傳遞的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!