JAVA springmvc 轉換器

來源:互聯網
上載者:User

標籤:ring   tor   binding   app   location   springmvc   handle   Servle   toms   

一、有時候springmvc給咱們提供的資料轉換並不能轉換所有類型比如說由字串類型轉換Date類型,這時候需要我們自訂轉換器,協助springmvc轉換我們需要的類型。

二、1)定義我們的轉換器:

 1 package jd.com.contronller.jd.com.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 CustomStringToDate implements Converter<String,Date> {10     @Override11     public Date convert(String s) {12         try {13             Date date= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);14             return date;15         } catch (ParseException e) {16             e.printStackTrace();17         }18         return null;19     }20 }

 2)在springmvc設定檔添加我們的轉換器:

1         <!--自訂轉換器 注意在註解驅動裡指明引用-->2     <bean id="conversionService"3     class="org.springframework.format.support.FormattingConversionServiceFactoryBean">4     <property name="converters">5     <set>6         <bean class="jd.com.contronller.jd.com.convert.CustomStringToDate"/>7     </set>8         </property>9         </bean>

 3)需要注意:在註解驅動的添加我們的轉換器id

1 <mvc:annotation-driven conversion-service="conversionService"  />

 

因為轉換器是處理器適配器進行處理的,如果我們不採用註解的方式,傳統的引用如下:

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <!-- 掃描帶Controller註解的類 -->    <context:component-scanbase-package="cn.itcast.springmvc.controller"/>        <!-- 轉換器配置 -->    <beanid="conversionService"        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <propertyname="converters">            <set>                <beanclass="jd.com.contronller.jd.com.convert.CustomStringToDate"/>            </set>        </property>    </bean>    <!-- 自訂webBinder -->    <beanid="customBinder"    class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">        <propertyname="conversionService"ref="conversionService"/>    </bean>    <!--註解適配器 -->    <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <propertyname="webBindingInitializer"ref="customBinder"></property>    </bean>    <!-- 註解處理器映射器 -->    <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

 注意的:

前端頁面想顯示日期格式需要定義下面標籤:

1  <td><fmt:formatDate value="${n.create_time}" pattern="yyyy-MM-dd HH:mm:ss" /> </td>

 需要注意格式需要和後面轉換器類的格式一致,否則報錯!!!!

JAVA springmvc 轉換器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.