zoukankan      html  css  js  c++  java
  • 分享知识-快乐自己:Struts2 前台日期到后台的日期格式转换

    案例目录:

    关键代码展示:

    DateConverter:

    package com.mlq.util;
    
    import com.opensymphony.xwork2.conversion.TypeConversionException;
    import org.apache.struts2.util.StrutsTypeConverter;
    
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;
    
    /**
     * @author asus
     */
    public class DateConverter extends StrutsTypeConverter {
    
        public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    
        private static final DateFormat[] dateFormats = {
                new SimpleDateFormat("yyyy年MM月dd日"),
                new SimpleDateFormat(DEFAULT_DATE_FORMAT),
                new SimpleDateFormat("MM/dd/yy"),
                new SimpleDateFormat("yyyy/MM/dd"),
                new SimpleDateFormat("yyyy.MM.dd"),
                new SimpleDateFormat("yyyyMMdd")};
    
    
        @Override
        public Object convertFromString(Map map, String[] strings, Class aClass) {
            System.out.println("================DateConverter============1=======");
            for (DateFormat format : dateFormats) {
                try {
                    return format.parse(strings[0]);
                } catch (ParseException e) {
                    continue;
                } catch (RuntimeException e) {
                    continue;
                }
            }
            throw new TypeConversionException();
        }
    
        @Override
        public String convertToString(Map map, Object o) {
            System.out.println("================DateConverter============2=======");
            SimpleDateFormat format = new SimpleDateFormat(
                    DEFAULT_DATE_FORMAT);
            return format.format((Date) o);
    
        }
    }

    login.jsp:

    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%--
      Created by IntelliJ IDEA.
      User: asus
      Date: 2018/10/4
      Time: 10:32
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <head>
        <title>登陆页面</title>
    </head>
    <body>
    <div>
        <fieldset>
            <legend>类型转换</legend>
            <form action="/user/log" method="post">
                <fieldset>
                    <legend>类型转换-日期类型</legend>
                    <input type="text" name="date" value="2018-05-05">
                </fieldset>
                <input type="submit" value="提交">
            </form>
        </fieldset>
    </div>
    </body>
    </html>

    Struts2日期转换Demo下载:

  • 相关阅读:
    手把手教你学Git
    服务器上Mysql的安装与配置
    python 5
    python 4
    python 3
    python 2
    区分命令行模式和Python交互模式
    CUDA编程模型之内存管理
    多目标优化算法-NSGA2
    C# ListView 如何添加列标头
  • 原文地址:https://www.cnblogs.com/mlq2017/p/10021932.html
Copyright © 2011-2022 走看看