I am wondering how to convert a String
to Date
in Struts2. I have a simple form in which user provide the date in this format "yyyy-MM-dd"
. Upon submit the Sturts2 maps form that to the bean. I got error in date conversion. I Google it a lot and every where it is stated that we have to use custom type converter for this. I don't want to write a custom type converter for date conversion. I think there should be an easy mechanism in Struts2 for data conversion because data conversion is very common functionality.
JSP
<s:form action="AddDomain">
<s:push value="idp">
<s:textfield name="domainName" label="Domain Name" />
<s:textfield name="url" label="Domain URL" />
<s:textfield name="noOfLicense" label="License Purchased" />
<s:textfield name="licenseExpireDate" label="License Expire Date"
title="YYYY-MM-DD like 2013-01-21" />
<s:textfield name="userActiveDuration" label="Active User Duration"
title="please mention in days" />
<s:textarea name="notes" label="Note" cols="30" rows="5" ></s:textarea>
<s:submit value="Add" />
</s:push>
</s:form>
This is the JSP Where user enter the input.
Model Class
@Entity
@Table(name = "Domain")
public class IdentityProvider implements Serializable {
@Id
@Basic(optional = false)
private String url;
private String domainName;
private int noOfLicense;
private int userActiveDuration;
private int activeUsers;
private Date licenseExpireDate;
private String notes;
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String domainIdCode;
public IdentityProvider(String name, String url, int nol, int time,Date d,String notes) {
this.setDomainName(name);
this.setUrl(url);
this.setNoOfLicense(nol);
this.setUserActiveDuration(time);
this.setLicenseExpireDate(d);
this.setNotes(notes);
}
public IdentityProvider() {
}
// Getter Setter
}
Action Class
public class DomainManagementAction extends ActionSupport
implements ModelDriven<IdentityProvider> {
private IdentityProvider idp = new IdentityProvider();
public IdentityProvider getIdp() {
return idp;
}
public void setIdp(IdentityProvider idp) {
this.idp = idp;
}
public String saveDomain() {
IDPBroker broker = new IDPBroker();
broker.saveDomain(idp);
return ActionSupport.SUCCESS;
}
@Override
public IdentityProvider getModel() {
// TODO Auto-generated method stub
return idp;
}
}
Struts2 Type Conversion
SHORT format in JAVA:
This means that it already works, but in SHORT format only, and it is not configurable. Try it.
Then you can alter the value clientside with some javascript hack before sending it, or copy and paste this small converter, or use a jQuery datetimepicker (recommended), and your only problem will be which theme is the coolest :)
EDIT
After some crypto comment, I've tried and obviously it's like the documentations states.
en_US
, you need to send a String data in the formatMM/dd/yy
.it_IT
, you need to send a String data in the formatdd/MM/yy
.dd/MM/yy
withen_US
Locale, you will get validation error and INPUT result.-
instead of/
, it will fail the same.dd/MM/yyyy
(different from SHORT but right for your Locale), your date will be correctly set the same.Then, as said above, if you need to let the user input the date manually, tell the user to respect the right format for your Locale (eg.
Or alter it through javascript after inserted, or use a custom converter.