Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

struts2

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?

like image 693
Vladimir Makhnovsky Avatar asked Jan 23 '13 15:01

Vladimir Makhnovsky


People also ask

What types of data can you have in struts?

Everything on a HTTP request is treated as a String by the protocol. This includes numbers, booleans, integers, dates, decimals and everything else. However, in the Struts class, you could have properties of any data types. How does Struts autowire the properties for you?

What is the use of type converters in struts?

Struts uses a variety of type converters under the covers to do the heavy lifting. For example, if you have an integer attribute in your Action class, Struts automatically converts the request parameter to the integer attribute without you doing anything.

How do I enable XWork-conversion for struts?

To do this, create a property file called xwork-conversion.properties in the WEBINF/classes folder with the following line This simply registers the converter globally, so that Struts can automatically do the conversion every time when it encounters an object of the type Environment.

What is environmentconverter in struts?

The EnvironmentConverter extends the StrutsTypeConverter class and tells Struts how to convert Environment to a String and vice versa by overriding two methods which are convertFromString () and convertToString (). Let us now register this converter before we use it in our application.


1 Answers

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

like image 188
Yasser Zamani Avatar answered Oct 21 '22 18:10

Yasser Zamani