一、普通字符串参数
@RequestMapping("/test2") @ResponseBody public String test2(String username, String password) { System.out.println(username); System.out.println(password); return "success"; }
二、数组参数
@RequestMapping("/test1") @ResponseBody public String test1(@RequestParam Map<String, Object> map, @RequestParam(required = false)List<String> hobby) { System.out.println(map); for (String s : hobby) { System.out.println(s); } return "success"; }
三、文件上传参数
- 需要引入三方库
<!--文件上传的三方库 解析:multipart参数--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency>
@RequestMapping("/test3") @ResponseBody public String test3(String username, MultipartFile photo1, MultipartFile photo2, HttpServletRequest request) throws Exception { System.out.println(username); // 将文件数据写入到具体的数据 // 文件名最好不要这么些,如果一样的话会覆盖,自己生成文件名 String filename = photo1.getOriginalFilename(); String path = request.getServletContext().getRealPath("upload/img" + filename); File file = new File(path); photo1.transferTo(file); filename = photo2.getOriginalFilename(); path = request.getServletContext().getRealPath("upload/img" + filename); file = new File(path); photo2.transferTo(file); return "success"; } @RequestMapping("/test4") @ResponseBody public String test4(String username, MultipartFile[] photos, HttpServletRequest request) throws Exception { System.out.println(username); for (MultipartFile photo : photos) { System.out.println(photo.getOriginalFilename()); } return "success"; }
配置文件:
<!--解析multipart参数--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--保证请求参数,文件名不乱码--> <property name="defaultEncoding" value="UTF-8"/> </bean>
四、日期参数(两种方式)
- 用系统自带的处理
@RequestMapping("/test5") @ResponseBody public String test5(@DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) throws Exception { System.out.println(birthday); return "success"; }
- 手动配置转换器
转换类:
package com.mj.converter; import org.springframework.core.convert.converter.Converter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateConverter implements Converter<String, Date> { public Date convert(String source) { try { return new SimpleDateFormat("yyyy-MM-dd").parse(source); } catch (ParseException e) { e.printStackTrace(); return null; } } }
配置文件:
<mvc:annotation-driven conversion-service="conversionService"> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.mj.converter.DateConverter"/> </set> </property> </bean>
五、测试jsp文件如下:
<%-- Created by IntelliJ IDEA. User: majiancheng Date: 2022/10/9 Time: 上午9:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <div> 普通参数 <form action="test0" method="post"> <input name="username"> <input name="password"> <button type="submit">提交</button> </form> </div> <div> 多个参数 <form action="test1"> <input name="name"> <input name="age"> <div> 足球<input type="checkbox" value="1" name="hobby"> </div> <div> 篮球<input type="checkbox" value="2" name="hobby"> </div> <div> 台球<input type="checkbox" value="3" name="hobby"> </div> <button type="submit">提交</button> </form> </div> <div> <form action="test2" method="post" enctype="multipart/form-data"> <input name="username"> <input name="password"> <button type="submit">提交</button> </form> </div> <div> <form action="test3" method="post" enctype="multipart/form-data"> <input name="username"> <input name="photo1" type="file"> <input name="photo2" type="file"> <button type="submit">提交</button> </form> </div> <div> <form action="test4" method="post" enctype="multipart/form-data"> <input name="username"> <input name="photos" type="file"> <input name="photos" type="file"> <button type="submit">提交</button> </form> </div> <div> 日期处理5: <form action="test5" method="post" > <input name="birthday" type="date"> <button type="submit">提交</button> </form> </div> <div> 日期处理6: <form action="test6" method="post" > <input name="birthday" type="date"> <button type="submit">提交</button> </form> </div> </body> </html>