欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springmvc請求參數類型轉換器及原生api代碼實例

 更新時間:2020年10月09日 08:39:07   作者:一路繁花似錦繡前程  
這篇文章主要介紹了Springmvc請求參數類型轉換器及原生api代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一、springmvc的xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!--掃描組件-->
  <context:component-scan base-package="com.wuxi"></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>
  <!--參數類型裝換器-->
  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
      <set>
        <bean class="com.wuxi.utils.StringToDateConverter"></bean>
      </set>
    </property>
  </bean>
  <!--開啟springmvc框架注解的支持-->
  <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
</beans>

二、轉換的類

package com.wuxi.utils;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date> {
  @Override
  public Date convert(String string) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
      date = sdf.parse(string);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }
}

三、接口

@RequestMapping("/student")
public String student(Student student, HttpServletRequest request, HttpServletResponse response) {
  System.out.println(student);
  return "success";
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論