如何在Spring框架中调用JSP文件中的转换器?
我是Spring框架新手,我设法制作了一个非常简单的CMS,使用Hibernate数据持久化(我使用mySQL数据库)在首页上显示文章。我的数据模型使用一些 Drupalesque 术语(如“节点”),因为我已经习惯了 CMS。 ;)
现在我正在努力将存储在数据库中的 UNIX 时间戳转换为 ISO 日期格式。我决定编写自己的转换器来完成这项任务。
我做了功课,阅读了 Spring 参考指南,在 google 上搜索教程等,但转换器似乎主要是用于从 GET 参数和 POST 查询转换数据的工具。那么如何将控制器绑定到模型的对象中的字段转换为模型呢?我在研究过程中错过了什么?
JSP 代码的一部分 (frontpage.jsp):
<div id="content-wrapper">
<div id="center-column">
<c:forEach items="${NodeCollection}" var="node">
<div class="node">
<h3>${node.title}</h3>
<span class="pub_time">${node.timestamp}</span>
<div class="content">${node.teaser}</div>
</div>
</c:forEach>
</div>
我的控制器:
@Controller
public class FrontpageController {
private NodeRevisionsDAOImpl nodeRevisionsDAO;
@Autowired
void setnodeRevisionsDAO(NodeRevisionsDAOImpl myNodeRevisionsDAOImpl) {
this.nodeRevisionsDAO = myNodeRevisionsDAOImpl;
}
@RequestMapping ( value = "/index.htm", method = RequestMethod.GET )
ModelAndView getMainPage(ModelAndView mav) {
ModelMap modelMap = new ModelMap();
Collection nodes = nodeRevisionsDAO.listNodeRevisions(5);
modelMap.addAttribute("NodeCollection", nodes);
mav.setViewName("frontpage");
mav.addAllObjects(modelMap);
return mav;
}
}
我的虚拟转换器(稍后我将添加实现):
package converters;
import org.springframework.core.convert.converter.Converter;
public class TimestampToDateConverter implements Converter<Long, String> {
public String convert(Long s) {
return "0000-00-00 00:00:00"; //Dummy code
}
}
调度程序的配置:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="converters.TimestampToDateConverter"/>
</list>
</property>
</bean>
I am Spring Framework newbe, and I managed to make a very simple CMS, using Hibernate data persistence (I use mySQL database) to display articles on frontpage. My data model is using some Drupalesque terminology (like "node"), because I am used to that CMS. ;)
Now I'm struggling with the task of converting the UNIX timestamp, that is stored in database, into ISO date format. I decided to write my own converter to do this task.
I did my homework, and I read the Spring reference guides, googled for tutorials etc, but it seems that converters are primarily a tool for converting data from GET parameters and POST queries. And what about converting fields from objects binded into Model by controllers? What did I missed during my research?
Part of JSP's code (frontpage.jsp):
<div id="content-wrapper">
<div id="center-column">
<c:forEach items="${NodeCollection}" var="node">
<div class="node">
<h3>${node.title}</h3>
<span class="pub_time">${node.timestamp}</span>
<div class="content">${node.teaser}</div>
</div>
</c:forEach>
</div>
My controller:
@Controller
public class FrontpageController {
private NodeRevisionsDAOImpl nodeRevisionsDAO;
@Autowired
void setnodeRevisionsDAO(NodeRevisionsDAOImpl myNodeRevisionsDAOImpl) {
this.nodeRevisionsDAO = myNodeRevisionsDAOImpl;
}
@RequestMapping ( value = "/index.htm", method = RequestMethod.GET )
ModelAndView getMainPage(ModelAndView mav) {
ModelMap modelMap = new ModelMap();
Collection nodes = nodeRevisionsDAO.listNodeRevisions(5);
modelMap.addAttribute("NodeCollection", nodes);
mav.setViewName("frontpage");
mav.addAllObjects(modelMap);
return mav;
}
}
My dummy converter (I'll add implementation later):
package converters;
import org.springframework.core.convert.converter.Converter;
public class TimestampToDateConverter implements Converter<Long, String> {
public String convert(Long s) {
return "0000-00-00 00:00:00"; //Dummy code
}
}
Dispacher's config:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="converters.TimestampToDateConverter"/>
</list>
</property>
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用日期数据类型。
例如,使用 JSTL,您可以轻松实现您的案例,无需额外的转换器
Why don't you use Date data type.
Using JSTL you can implement your case easily without additional converter, for example