案例目录:
关键代码展示:
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>