Vladimir Makhnovsky
Vladimir Makhnovsky

Reputation: 291

Struts 2, type conversion of Dates in a List, non-default format

I have an action, that action has List of Dates property. The request parameters will come in "yyyy.MM.dd" format. In have "-coversion.properties" file (I don't want to use global type converter for this purpose).

I am not using annotations.

If it was just a single Date, not a collection, property-specific conversion would be easy. Is it possible to do apply a property-specific type conversion to the elements of a List?

Upvotes: 11

Views: 1443

Answers (1)

Yasser Zamani
Yasser Zamani

Reputation: 2500

I wrote a sample code which works fine. It's structure is as above. enter image description here

At first I wrote a converter in /util/MyDateConverter.java as below:

package util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.conversion.TypeConversionException;

import ognl.DefaultTypeConverter;

public class MyDateConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map context, Object object, Class type) {
    if (type == Date.class) {
        String datePattern = "yyyy.MM.dd";
        DateFormat format = new SimpleDateFormat(datePattern);
        format.setLenient(false);
        try {
            String[] dateString = (String[]) object;
            return format.parse(dateString[0]);
        } catch (Exception e) {
            throw new TypeConversionException("Given Date is Invalid");
        }
    }
    return null;
}
}

Then, to use struts2 collection conversion support, I wrote /util/MyDate.java as below:

package util;
import java.io.Serializable;
import java.util.Date;

public class MyDate implements Serializable {

    public MyDate(int myId, Date value) {
        super();
        this.myId = myId;
        this.value = value;
    }

    int myId;
    Date value;

    public Date getValue() {
        return value;
    }

    public void setValue(Date value) {
        this.value = value;
    }

    public int getMyId() {
        return myId;
    }

    public void setMyId(int myId) {
        this.myId = myId;
    }

    @Override
    public String toString() {
        return value.toString();
    }
}

Then I told struts2 how to support collection by writing /action/Action1-conversion.properties as below:

KeyProperty_dates=myId
Element_dates=util.MyDate
CreateIfNull_dates=true

Then told him how to convert MyDate.value by writing /util/MyDate-conversion.properties as below:

value=util.MyDateConverter

Then I prepared /index.jsp as below:

<%@ taglib uri="/struts-tags" prefix="s"%>

<s:form>
    <s:date name="dates(1).value" format="yyyy.MM.dd" id="dateSD1" />
    <s:textfield name="dates(1).value" value="%{dateSD1}" label="New Date 1" />

    <s:date name="dates(2).value" format="yyyy.MM.dd" id="dateSD2" />
    <s:textfield name="dates(2).value" value="%{dateSD2}" label="New Date 2"/>

    <s:submit value="Update" />
    <pre>
    Current Date 1: <s:date name="dates(1).value" />
    Current Date 2: <s:date name="dates(2).value" />
    </pre>
</s:form>

and /action/Action1.java as below:

package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import util.MyDate;

import com.opensymphony.xwork2.ActionSupport;

public class Action1 extends ActionSupport {

    public Action1() {
        super();
        dates.add(new MyDate(1, new Date()));
        dates.add(new MyDate(2, new Date()));
    }

    public String execute() {
        return SUCCESS;
    }

    List<MyDate> dates = new ArrayList<MyDate>();

    public void setDates(List<MyDate> dates) {
        this.dates = dates;
    }

    public List<MyDate> getDates() {
        return dates;
    }
}

After running application, the Update button works well as expected which shows that conversions have been working well.

I hope this sample code can help you.

Reference: Apache Struts2 Type Conversion

Upvotes: 1

Related Questions