我想知道如何在Struts2中將String
轉換爲Date
。我有一個簡單的表格,用戶在這種格式下提供這種格式的日期"yyyy-MM-dd"
。將Sturts2地圖表單提交給bean。我在日期轉換中出錯。我谷歌它很多,每一個地方,我們必須使用自定義類型轉換器。我不想爲日期轉換編寫自定義類型轉換器。我認爲在Struts2中應該有一個簡單的數據轉換機制,因爲數據轉換是非常常見的功能。Struts2中的日期轉換
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>
這是JSP如果用戶輸入的輸入。
模型類
@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類
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;
}
}
struts可以同時轉換日期和時間嗎?例如:'30/06/09 07:03' –
您應該問這是一個問題,因爲答案是明確的。 TL; DR是的 –