我最近正在开发一个 spring mvc Web 应用程序,使用 thymeleaf 作为 View 引擎,使用 hibernate for dao 和 spring security。
当我制作 Controller 时,我遇到了一种情况:
我有一个Notification
实体,它有一个复合主键NotificationPK(CreationDate+UserID)
:creationDate是创建的时间通知内容
我想使用 URL /notif/{creationDate} 显示通知的内容。 (我不需要传递用户 ID,因为我可以使用 Spring Security 检索它)
问题是创建日期是时间戳,所以我必须使用 Spring 转换器(我被告知)
但问题是,我了解了转换器的一般概念,但我不知道在这种情况下如何制作它,我不明白这个转换器在 Controller 中如何工作
有人可以帮忙提供一个示例并解释一下如何将该转换器添加到 Controller 以及该转换器如何工作
编辑
此代码是否正确:
@RequestMapping(value = "/doctor/notification/{creationDate}")
public ModelAndView getNotification(@PathVariable( "creationDate" ) @org.springframework.format.annotation.DateTimeFormat( pattern = "YYYY-mm-dd HH:mm:ss" ) Timestamp creationDate){
if (doctor==null) setCurrentUser();
if (creationDate==null) throw new NoSuchNotificationException();
NotificationPK pk = new NotificationPK();
pk.setRecipient(doctor.getIdPers());
pk.setCreationDate(creationDate); //.....
最佳答案
在 Spring 中你可以使用这个:
@RequestMapping(value="/notif/{creationDate}")
public String getnotification(@PathVariable( "creationDate" ) @org.springframework.format.annotation.DateTimeFormat( pattern = "MMMM dd, yyyy" ) java.util.Calendar creationDate){
//here creation Date is your Calendar object
}
关于java - 使用 Spring 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666106/