🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1. 基本数据类型, 类型定义为包装类型,否则报400错误 2. 对象类型, ~~~ public class Userinfo { private String username; private String password; private Integer age; private String sex; private String education; private String birthday; } ~~~ ~~~ <form action="register" method="post"> <input type="text" name="username"/> <input type="password" name="password"/> <input type="text" name="age"/> <input type="date" name="birthday"/> <input type="radio" name="sex" value="M"/> 男 <input type="radio" name="sex" value="F"/> 女 <select name="education"> <option>博士</option> <option>硕士</option> <option>学士</option> </select> <input type="checkbox" name="hobby" value="1"/> 看书 <input type="checkbox" name="hobby" value="2"/> 编程 <button type="submit">注册</button> </form> ~~~ ~~~ @RequestMapping("/register") public String reg(Userinfo u, String[] hobby) { System.out.println("username:"+u.getUsername()); System.out.println("password:"+u.getPassword()); System.out.println("age:"+u.getAge()); System.out.println("sex:"+u.getSex()); System.out.println("education:"+u.getEducation()); System.out.println("birthday:"+u.getBirthday()); for(int i=0; i<hobby.length;i++) { System.out.println(hobby[i]); } return "redirect:/login.html"; } ~~~ 3. 数组类型 3.1 表单提交 ~~~ @RequestMapping("/test7") @ResponseBody //public MyResult test7(@RequestParam(value = "ids") String[] ids) public String test7(String[] ids) { for(int i=0; i<ids.length;i++) { System.out.println(ids[i]); } return "{\"result\":true}"; } ~~~ 3.2 ajax提交 ~~~ public String test5(@RequestParam(value = "ids[]") String[] ids) { for(int i=0; i<ids.length;i++) { System.out.println(ids[i]); } return "{\"result\":true}"; } ~~~ 4. 日期类型转换:定义转换器(个人不建议用,理由见下面vo和po分离) ~~~ public class CustomDateConverter implements Converter<String,Date>{ @Override public Date convert(String source) { // TODO Auto-generated method stub SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return sdf.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } ~~~ 日期类型转换:配置转换器 ~~~ <!-- 自定义参数绑定 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <!-- 转换器 --> <property name="converters"> <list> <!-- 日期类型转换 --> <bean class="com.ttc.ssm.controller.converter.CustomDateConverter"/> </list> </property> </bean> <mvc:annotation-driven conversion-service="conversionService"> </mvc:annotation-driven> ~~~ 日期类型转换:测试日期转换 ~~~ @RequestMapping("/test8") @ResponseBody public String test8(Date birthday) { System.out.println(birthday); return "{\"result\":true}"; } ~~~ 5. 配置过滤器,解决中文乱码问题 ~~~ <filter> <filter-name>CharacterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ~~~ vo和po分离: 页面显示的内容和数据库的内容和数据类型不一定一致,应该采用不同的对象。 dto(vo), data transfer object 数据传输对象,对应页面 bo(po),business object 业务对象,对应数据库 当使用了mybatis或者是hibernate这样的框架时,习惯使用以下说法: vo: value object 值对象,对应页面 po: persistent object 持久化对象,对应数据库 vo: 对应页面:String birthday po: 对应数据库: Date birthday View(vo) -> Controller(vo) -> Service(vo<->po) -> Mapper(po)