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?
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?
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.
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.
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.
I wrote a sample code which works fine. It's structure is as above.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With