Struts2中的日期转换 [英] Date Conversion in Struts2

查看:170
本文介绍了Struts2中的日期转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Struts2中将 String 转换为 Date 。我有一个简单的表单,其中用户以这种格式yyyy-MM-dd提供日期。将Sturts2贴图提交给bean。日期转换时出错。我的谷歌很多,每一个地方都说我们必须使用自定义类型转换器。我不想为日期转换编写自定义类型转换器。我认为Struts2应该有一个简单的机制来进行数据转换,因为数据转换是非常常见的功能。



JSP

 < s:form action =AddDomain> 
< s:push value =idp>
< s:textfield name =domainNamelabel =域名/>
< s:textfield name =urllabel =域网址/>
< s:textfield name =noOfLicenselabel =已购买许可/>

< s:textfield name =licenseExpireDatelabel =许可证到期日
title =YYYY-MM-DD like 2013-01-21/>

< s:textfield name =userActiveDurationlabel =活动用户持续时间
title =请在天内提供/>

< s:textarea name =noteslabel =注意cols =30rows =5>< / s:textarea>

< s:submit value =添加/>
< / 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
}

动作类

  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自动生成的方法存根
返回idp;
}

}


解决方案

Struts2 类型转换


内置类型转换支持



类型转换由XWork实现。



XWork将自动处理
最常见的类型转换。这包括支持以下每个
的字符串转换:



String

boolean / Boolean
char /字符
int / Integer,float / Float,long / Long,double / Double $ $ $ $ $ $ $ $ $ $与当前请求相关联

数组 - 假设个别字符串可以被覆盖到单独的项目中
集合 - 如果不是对象类型可以确定,它是假设是一个String并创建一个新的ArrayList


SHORT格式在JAVA中:


 风格美国地方法语地区

SHORT 6/30/09 30/06/09


< blockquote>

这意味着它已经可以工作了,但是在SHORT格式中,它不是配置可以尝试一下。



然后,您可以在发送之前使用一些javascript hack修改客户端值,或者复制并粘贴这个小转换器,或使用jQuery datetimipicker(推荐),你唯一的问题将是最酷的主题:)



< hr>

编辑



经过一些加密评论,我已经尝试了,显然这就像文档说明。




  • 如果您的区域设置为(例如) en_US ,则需要在格式 MM / dd / yy

  • 如果您的区域设置为(例如) it_IT ,您需要以格式 dd / MM / yy 发送一个String数据。

  • 如果输入 dd / MM / yy en_US 区域设置,您将得到验证错误和INPUT结果。

  • 如果您尝试使用 - 而不是 / ,则它将失败。

  • 如果您在此输入日期呃格式,例如(不同于SHORT,但适用于您的语言环境),您的日期将被正确设置为相同的。 dd / MM / yyyy

  • 如果您在JSP中阅读Action日期,而不进行格式化,则将始终以SHORT的形式显示。



然后,如上所述,如果您需要让用户手动输入日期,请​​告诉用户尊重您的区域设置的正确格式(例如,

 < label>输入日期(dd / mm / yyyy):< / label> 
< s:textfield name =aDate/>
< s:fielderror fieldName =aDate/>

或通过javascript更改插入或使用自定义转换器。


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

Built in Type Conversion Support

Type Conversion is implemented by XWork.

XWork will automatically handle the most common type conversion for you. This includes support for converting to and from Strings for each of the following:

String
boolean / Boolean
char / Character
int / Integer, float / Float, long / Long, double / Double
dates - uses the SHORT format for the Locale associated with the current request
arrays - assuming the individual strings can be coverted to the individual items
collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is created

SHORT format in JAVA:

Style     U.S. Locale   French Locale

SHORT       6/30/09       30/06/09

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.

  • If your Locale is (eg.) en_US, you need to send a String data in the format MM/dd/yy.
  • If your Locale is (eg.) it_IT, you need to send a String data in the format dd/MM/yy.
  • If you input dd/MM/yy with en_US Locale, you will get validation error and INPUT result.
  • If you try using - instead of /, it will fail the same.
  • If you input a date in another format, eg. dd/MM/yyyy (different from SHORT but right for your Locale), your date will be correctly set the same.
  • If you read your Action date in the JSP without formatting it, it will be always displayed in SHORT.

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.

<label>Input a date (dd/mm/yyyy):</label> 
<s:textfield name="aDate" />
<s:fielderror fieldName="aDate" />

Or alter it through javascript after inserted, or use a custom converter.

这篇关于Struts2中的日期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆