1. 程式人生 > >fmt標籤實現日期格式化,與型別轉換Converter

fmt標籤實現日期格式化,與型別轉換Converter

一.頁面展示時間型別資料

 1.引入標頭檔案:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

 2.將從後臺取出的時間型別資料按yyyy-MM-dd格式,格式化;

<td><fmt:formatDate value="${u.birthday }" pattern="yyyy-MM-dd"/> </td>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function deleteUser(userId,userName){
		if(window.confirm("您確定要刪除使用者["+userName+"]嗎?")){
			location.href="${pageContext.request.contextPath }/user/"+userId+"/delete.action";
		}
	}
</script>
</head>
<body>
<h1>使用者列表</h1>
<table border="1" cellpadding="0" cellspacing="0">
	<tr>
		<th><input type="checkbox"/></th>
		<th>編號</th>
		<th>姓名</th>
		<th>生日</th>
		<th>性別</th>
		<th>地址</th>
		<th>操作</th>
	</tr>
	<c:forEach items="${users }" var="u" varStatus="status">
		<c:if test="${status.index%2==0 }">
			<tr style="background-color: #ccc">
		</c:if>
		<c:if test="${status.index%2!=0 }">
			<tr>
		</c:if>
			<td><input type="checkbox" value="${u.id }" name="uid"/></td>
			<td>${u.id }</td>
			<td>${u.username }</td>
			<td><fmt:formatDate value="${u.birthday }" pattern="yyyy-MM-dd"/> </td>
			<td>${u.sex==1?'男':'女' }</td>
			<td>${u.address }</td>
			<td>
				<a href="">檢視</a>/
				<a href="${pageContext.request.contextPath }/editUser.action?id=${u.id }">修改</a>/
				<a onclick="deleteUser(${u.id},'${u.username} }')" href="javascript:void(0)">刪除</a>
			</td>
		</tr>
	</c:forEach>
</table>
</body>
</html>

 

二.後臺時間型別欄位接收前臺傳來的時間字串

1.自定義時間字串轉換類DateConverter繼承 Converter<String, Date>

註明:Converter這個類可以轉換任何型別,用的最多的是時間轉換;

package com.igeek.ssm.ex.converters;

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

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

/**
 * @author www.igeehome.com
 * 
 * TODO
 *
 * 2018年10月24日下午7:57:37
 */
public class DateConverter implements Converter<String, Date> {

	@Override
	public Date convert(String dateStr) {
		Date date = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			 date = dateFormat.parse(dateStr);
		} catch (ParseException e) {
			//e.printStackTrace();
			dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			 try {
				date = dateFormat.parse(dateStr);
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return date;
	}

}

2.springMVC.xml中配置:


	<!-- 配置註解驅動 -->
	<!-- 如果配置此標籤,可以不用配置... -->
	<mvc:annotation-driven conversion-service="conversionService" />


	<!-- 配置型別轉換器 -->
	<bean id="conversionService"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<bean class="com.igeek.ssm.ex.converters.DateConverter" />
			</set>
		</property>
	</bean>

 3.頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增使用者</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/addUser.action" method="post">
		<p>使用者名稱:<input type="text" name="username"/></p>
		<p>生日:<input type="date" name="birthday"/></p>
		<p>性別:<input type="radio" name="sex" value="1"/>男
			<input type="radio" name="sex" value="2"/>女</p>
		<p>地址:<input type="text" name="address"/></p>
		<p><input type="submit" value="新增"/></p>
	</form>
</body>
</html>

4.後臺接收:

	@RequestMapping("/addUser")
	public String addUser(User user) {
		userService.saveUser(user);
		// 無論是否成功,進入列表頁面
		// 重定向進入url:userList.action
		return "redirect:userList.action";
	}

5.實體類:

package com.igeek.ssm.ex.pojo;

import java.util.Date;

/**
 * @author www.igeehome.com
 * 
 * TODO
 *
 * 2018年10月29日下午7:48:30
 */
public class User implements java.io.Serializable{
	private int id;
	private String username;
	private Date birthday;
	private int sex;
	private String address;
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}
	/**
	 * @param birthday the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	/**
	 * @return the sex
	 */
	public int getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(int sex) {
		this.sex = sex;
	}
	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}
	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}
	
}