zoukankan      html  css  js  c++  java
  • springMvc把client传过来一个String类型,转换为日期类型为例

    springMvc--接受日期类型参数处理

     

    这个问题,也即是springMvc如何进行参数类型的转换 , 以把client传过来一个String类型,转换为日期类型为例

    步骤

    1.controller

    复制代码
    /**
         * 接收日期类型参数
         *     注意:
         *         springmvc 在接收日期类型参数时,如不做特殊处理 会出现400语法格式错误
         *  解决办法
         *      1.全局日期处理
         * 
         */
        
        @RequestMapping("/test")
        public String test(Date birthday){
            System.out.println(birthday);
            return "index";
        }
    复制代码

    2.自定义类型转换规则

    SpringMvc提供了Converter接口,它支持从一个Object转换为另一个Object

    复制代码
    /**
     * 全局日期处理类
     * Convert<T,S>
     *         泛型T:代表客户端提交的参数 String
     *         泛型S:通过convert转换的类型
    */ public class DateConvert implements Converter<String, Date> { @Override public Date convert(String stringDate) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { return simpleDateFormat.parse(stringDate); } catch (ParseException e) { e.printStackTrace(); } return null; } }
    复制代码

    3.注册自定义的类型转换类

    复制代码
      <!-- 第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性-->
        <mvc:annotation-driven conversion-service="conversionService"/>
       
        <!-- 第二步: 创建convertion-Service ,并注入dateConvert-->
        <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
            <property name="converters">
                <set>
                    <ref bean="dateConvert"/>
                </set>
            </property>
        </bean>
        <!-- 第一步:  创建自定义日期转换规则 -->   
        <bean id="dateConvert" class="zpark.convert.DateConvert"/>
    复制代码

    4.地址栏访问

    http://localhost:9999/date/test2?birthday=1990-01-02

    参考:http://blog.csdn.net/renhui999/article/details/9837897

    ps:测试了很久才成功,失败的次数太多就不记录下来了,以上只是记录了测试正确的步骤

    本文转自http://www.cnblogs.com/liuconglin/p/5777879.html 感谢作者

    jsp页面String类型转Controller后台Date类型

     

    方法1.在实体中加入日期格式化注解

    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birthday;

     

    方法2.在controller中加入数据绑定代码

    复制代码
    package com.fyh.www.pojo.user;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.InitBinder;
    
    public class LoginController {
    
        @InitBinder
        public void initBinder(WebDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值,false:不能为空值
    
        }
    }
    复制代码

    方法3.注册一个全局日期类型转化器

    注册全局转化器

    复制代码
        <mvc:annotation-driven conversion-service="conversionService"/>
    
    
        <!-- 设置Converter转换器 -->
            <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
                <!-- 设置多个转换器 -->
                <property name="converters">
                    <list>
                        <bean class="com.fyh.www.common.mvcConverter.CustomTrimConverter"></bean>
                    </list>
                </property>
            </bean>
    复制代码

    具体的实现代码

    复制代码
     public class DateConverter implements Converter<String, Date> {    
    @Override public Date convert(String source) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); try { return dateFormat.parse(source); } catch (ParseException e) { e.printStackTrace(); } return null; }
    复制代码

    后台date类型到前台String类型

    JSP模版引擎方法:

        <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>   
        <fmt:formatDate value="${job.jobtime }" pattern="yyyy-MM-dd HH:mm:ss"/>  

    Freemarker模版引擎方法:

        <input id="receiveAppTime" name="receiveAppTime" type="text" value="${(bean.receiveAppTime?string('yyyy-MM-dd'))!}" />  


    本文转自http://www.cnblogs.com/woms/p/6037902.html 感谢作者
  • 相关阅读:
    蓝桥网试题 java 基础练习 特殊的数字
    蓝桥网试题 java 基础练习 杨辉三角形
    蓝桥网试题 java 基础练习 查找整数
    蓝桥网试题 java 基础练习 数列特征
    蓝桥网试题 java 基础练习 字母图形
    蓝桥网试题 java 基础练习 01字串
    蓝桥网试题 java 基础练习 回文数
    蓝桥网试题 java 基础练习 特殊回文数
    Using text search in Web page with Sikuli
    each of which 用法
  • 原文地址:https://www.cnblogs.com/panxuejun/p/7279969.html
Copyright © 2011-2022 走看看