1

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>
DoktorNo
  • 13
  • 3

1 Answers1

1

Why don't you use Date data type. Using JSTL you can implement your case easily without additional converter, for example

<fmt:formatDate pattern="yyyy-MM-dd hh:mm:ss" value="${node.time}" />
Adi Sembiring
  • 5,798
  • 12
  • 58
  • 70
  • Thanks for answer, but it is still not going well. I have this kind of conversion in setter for "timestamp" (Long datatype) field in my Hibernate bean: public Date getTimestamp() { Date date = new Date(); date.setTime(timestamp.longValue()); return date; } In rendered html I get dates like 1970-01-16 02:41:33, it looks like it doesn't transfers proper value... Timestamps in Hibernate beans are correct, though. – DoktorNo Apr 09 '11 at 14:31
  • what is the type of that column in your table ? – Adi Sembiring Apr 09 '11 at 14:42
  • I use BIGINT(20) for storing timestamps. I made a simple System.out output in getter for timestamp, and it was correct. Date value also was correct. – DoktorNo Apr 09 '11 at 15:41
  • Ups, I forget to multiply the timestamp variable by 1000, as is said here: http://stackoverflow.com/questions/3371326/java-date-from-unix-timestamp Not I can play with JSTL's fmt:formatDate freely, because I have proper data to work with. :) Now my getter for timestamp looks like this: public Date getTimestamp() { return new Date(timestamp * 1000); } – DoktorNo Apr 09 '11 at 16:17