SSM学习10Spring-MVC学习数据绑定02
5.自定义踉据绑定
Spring 框架提供了一个 Converter 用于将一种类型的对象转换为另一种类型的对象 例如, 用户输入的曰期形式可能是 "2017-04-08"或 "2017/04/08" 的字符串,而要 Spring 将输入 的日期与后台的 Date 进行绑定,则需要将字符串转换为日期,此时就可以自定义一个 Converter 类来进行曰期转换。
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。新建com.zyk.convert包,新建DateConverter.java文件:
1 package com.zyk.convert; 2 3 import org.springframework.core.convert.converter.Converter; 4 5 import java.text.ParseException; 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 public class DateConverter implements Converter<String,Date> { 10 private String datePattern = "yyyy-MM-dd HH:mm:ss"; 11 @Override 12 public Date convert(String source){ 13 SimpleDateFormat sdf=new SimpleDateFormat(datePattern); 14 try { 15 return sdf.parse(source); 16 }catch (ParseException e){ 17 throw new IllegalArgumentException("无效的日期格式,使用该格式"+datePattern); 18 } 19 } 20 }
配置applicationContext.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 <context:component-scan base-package="com.zyk.controller"></context:component-scan> 13 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 14 <property name="prefix" value="/WEB-INF/jsp/"></property> 15 <property name="suffix" value=".jsp"></property> 16 </bean> 17 <mvc:annotation-driven conversion-service="conversionService"> 18 </mvc:annotation-driven> 19 <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> 20 <property name="converters"> 21 <set> 22 <bean class="com.zyk.convert.DateConverter"></bean> 23 </set> 24 </property> 25 </bean> 26 <!-- <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">--> 27 <!-- <property name="formatters">--> 28 <!-- <set>--> 29 <!-- <bean class="com.zyk.convert.DateFormatter"></bean>--> 30 <!-- </set>--> 31 <!-- </property>--> 32 <!-- </bean>--> 33 </beans>
在com.zyk.controller中新建DateConvert
package com.zyk.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Date; @Controller public class DateController { @RequestMapping("/customDate") public String customDate(Date date){ System.out.println("date="+date); return "success"; } }
启动tomcat,输入http://localhost:8080/springMvc02_war_exploded/customDate?date=2019-4-17%2010:29:55,控制台显示了另一种时间格式,页面也跳转到了success.jsp:
6.Formatter
在com.zyk.convert包中新建DateFormatter;
1 package com.zyk.convert; 2 3 import org.springframework.format.Formatter; 4 5 import java.text.ParseException; 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 import java.util.Locale; 9 10 public class DateFormatter implements Formatter<Date> { 11 private String datePattern = "yyyy-MM-dd HH:mm:ss"; 12 private SimpleDateFormat simpleDateFormat; 13 @Override 14 public String print(Date date , Locale locale){ 15 return new SimpleDateFormat().format(date); 16 } 17 @Override 18 public Date parse(String source,Locale locale)throws ParseException{ 19 simpleDateFormat=new SimpleDateFormat(datePattern); 20 return simpleDateFormat.parse(source); 21 } 22 }
修改配置文件applicationContext:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 <context:component-scan base-package="com.zyk.controller"></context:component-scan> 13 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 14 <property name="prefix" value="/WEB-INF/jsp/"></property> 15 <property name="suffix" value=".jsp"></property> 16 </bean> 17 <mvc:annotation-driven conversion-service="conversionService"> 18 </mvc:annotation-driven> 19 <!-- <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">--> 20 <!-- <property name="converters">--> 21 <!-- <set>--> 22 <!-- <bean class="com.zyk.convert.DateConverter"></bean>--> 23 <!-- </set>--> 24 <!-- </property>--> 25 <!-- </bean>--> 26 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 27 <property name="formatters"> 28 <set> 29 <bean class="com.zyk.convert.DateFormatter"></bean> 30 </set> 31 </property> 32 </bean> 33 </beans>
依旧时启动tomcat,输入http://localhost:8080/springMvc02_war_exploded/customDate?date=2019-4-17%2010:29:55,控制台显示了另一种时间格式,页面也跳转到了success.jsp,这里的截图一模一样就不放了。
二者区别:
Formatter与Converter 的作用相同,只是 Formatter 的源类型必须是一个 String 类型,而 Converter 可以是任意类型。
7.绑定数组
在上一个博客中创建了普通绑定的pojo--User,这里用他来演示绑定数组。
编写UserController:
package com.zyk.controller; import com.zyk.po.User; import com.zyk.vo.UserVO; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; @Controller public class UserController { @RequestMapping("/toUser") public String toUser() { return "user"; } @RequestMapping("/deleteUser") public String deleteUsers(Integer[] ids) { if (ids != null) for (Integer id : ids) System.out.println("删除了id为" + id + "的用户!"); else System.out.println("ids=null"); return "success"; } }
在jsp下面新建user.jsp:
1 <%-- 2 Created by IntelliJ IDEA. 3 User: ZYK 4 Date: 2019/4/17 5 Time: 11:02 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>用户列表</title> 12 </head> 13 <body> 14 <form action="${pageContext.request.contextPath}/deleteUser" method="post"> 15 <table border="1"> 16 <tr> 17 <td>选择</td> 18 <td>用户名</td> 19 </tr> 20 <tr> 21 <td><input name="ids" value="l" type="checkbox"></td> 22 <td>tom</td> 23 </tr> 24 <tr> 25 <td><input name="ids" value="2" type="checkbox"></td> 26 <td>jack</td> 27 </tr> 28 <tr> 29 <td><input name="ids" value="3" type="checkbox"></td> 30 <td>rose</td> 31 </tr> 32 </table> 33 <input type="submit" value="删除"> 34 </form> 35 </body> 36 </html>
启动tomcat,输入:http://localhost:8080/springMvc02_war_exploded/toUser,页面跳转至user.jsp:
点击删除,可以看到控制台的输出和页面跳转到success.jsp。
8.绑定集合
新建com.zyk.vo包,在包中新建UserVO:
package com.zyk.vo; import com.zyk.po.User; import java.util.List; public class UserVO { private List<User> users; public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } }
在jsp下新建toUserEdit.jsp
1 <%-- 2 Created by IntelliJ IDEA. 3 User: ZYK 4 Date: 2019/4/17 5 Time: 18:53 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>修改用户</title> 12 </head> 13 <body> 14 <form action="${pageContext.request.contextPath}/editUsers" method="post" id="formid"> 15 <table width="30%" border="1"> 16 <tr> 17 <td>选择</td> 18 <td>用户名</td> 19 </tr> 20 <tr> 21 <td><input type="checkbox" name="users[0].id" value="1"></td> 22 <td><input type="text" name="users[0].username" value="tom"></td> 23 </tr> 24 <tr> 25 <td><input type="checkbox" name="users[1].id" value="2"></td> 26 <td><input type="text" name="users[1].username" value="jack"></td> 27 </tr> 28 29 </table> 30 <input type="submit" value="修改"> 31 </form> 32 </body> 33 </html>
UserController:
package com.zyk.controller; import com.zyk.po.User; import com.zyk.vo.UserVO; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; @Controller public class UserController { @RequestMapping("/toUserEdit") public String toUserEdit() { return "toUserEdit"; } @RequestMapping("/editUsers") public String editUsers(UserVO userVO) { List<User> users = userVO.getUsers(); for (User user : users) if (user.getId() != null) System.out.println("修改id为" + user.getId() + "的用户名为" + user.getUsername()); return "success"; } }
启动tomcat,浏览器输入:http://localhost:8080/springMvc02_war_exploded/toUserEdit:
点击修改后,控制台:
绑定数据中出现的问题:
1.原来为了方便把pojo中的integer类型写成了int类型,导致后面写controller类型以及从jsp传值时报错。
