您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Springmvc實(shí)現(xiàn)數(shù)據(jù)格式化,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
1、簡(jiǎn)介
2、示例
2.1、實(shí)體類
package com.yl.bean; import java.util.Date; public class User { private String username; private Date date; public User() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", date=" + date + '}'; } }
2.2、控制器
package com.yl.controller; import com.yl.bean.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller public class UserController { @RequestMapping("/stringToDate") public ModelAndView jsonToObject(User user){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("user",user); modelAndView.setViewName("success"); System.out.println(user); return modelAndView; } }
2.3、jsp
<form action="${pageContext.servletContext.contextPath}/stringToDate" method="post"> 請(qǐng)輸入日期(yyy-mm-dd):<input type="text" name="date"><br> <button type="submit">提交</button> </form>
2.4、數(shù)據(jù)格式化類
package com.yl.utils; import org.springframework.format.Formatter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; /** * 日期格式化 */ public class DateFormatter implements Formatter<Date> { /** * 字符串轉(zhuǎn)Date * @param text * @param locale * @return * @throws ParseException */ @Override public Date parse(String text, Locale locale) throws ParseException { SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd"); return sf.parse(text); } /** * Date轉(zhuǎn)字符串 * @param date * @param locale * @return */ @Override public String print(Date date, Locale locale) { SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd"); return sf.format(date); } }
2.5、在配置文件注冊(cè)自定義數(shù)據(jù)格式化類
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--指定要掃描的包--> <context:component-scan base-package="com.yl"></context:component-scan> <!--配置視圖解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--數(shù)據(jù)格式化工廠--> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatters"> <list> <!--自定義格式化類--> <bean class="com.yl.utils.DateFormatter"/> </list> </property> </bean> <!-- 設(shè)置靜態(tài)資源不過(guò)濾--> <mvc:default-servlet-handler/> <!--開啟springmvc注解支持,注冊(cè)自定義數(shù)據(jù)格式化類--> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> </beans>
3、使用注解實(shí)現(xiàn)數(shù)據(jù)格式化
package com.yl.bean; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; public class User { private String username; @DateTimeFormat(pattern = "yyyy-MM-dd") private Date date; public User() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", date=" + date + '}'; } }
以上就是Springmvc實(shí)現(xiàn)數(shù)據(jù)格式化,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。