如何使用条带框架将日期从JSP传递到Java?

时间:2022-07-12 11:59:33

I'm using the Stripes Framework and am trying to get a DateTime from the jsp but for some reason the setter always gets null passed into it.

我正在使用Stripes Framework并且我试图从jsp获取DateTime但是由于某种原因,setter总是将null传递给它。

JSP snippet:

JSP片段:

<stripes:form name="dateForm" action="some.actionBean.url">
    <stripes:hidden name="myDate" value="12-23-2015 12:00" />
</stripes:form>

ActionBean snippet:

ActionBean片段:

private DateTime myDate;

public void setMyDate(DateTime date){
  //when the setter gets called date is null, but why?
  this.myDate = date;
}
public DateTime getMyDate(){
  return this.myDate;
}

I tried many things already, like

我已经尝试了很多东西,比如

  • tried to set the value to different time formats
  • 试图将值设置为不同的时间格式
  • tried to have the setter take in a string and then convert to a DateTime(this didn't work because the string is also null)
  • 试图让setter接受一个字符串,然后转换为DateTime(这不起作用,因为该字符串也为null)
  • tried different stripes tags
  • 试过不同的条纹标签

no luck yet, What am I doing wrong?

没有运气,我做错了什么?

I'm basically tapping in the dark because I can't find the Tag Lib documentation. On the official site it is linked to a broken page.

我基本上是在黑暗中攻击,因为我找不到Tag Lib文档。在官方网站上,它链接到一个损坏的页面。

1 个解决方案

#1


3  

You are binding into a DateTime object. Stripes has a built in TypeConverter for Date objects but not for DateTime.

您绑定到DateTime对象。 Stripes有一个用于Date对象的内置TypeConverter,但没有用于DateTime。

When you change myDate to java.util.Date Stripes' DateTypeConverter will pick it up.

当你将myDate更改为java.util.Date时,Stripes的DateTypeConverter会将其取出。

Otherwise, if for instance you need support for joda.time.DateTime you'll need to write your own custom TypeConverter (which you don't need because its printed below):

否则,如果你需要支持joda.time.DateTime,你需要编写自己的自定义TypeConverter(你不需要它,因为它打印在下面):

public class JodaDateTimeTypeConverter implements TypeConverter<DateTime> {

    @Override
    public DateTime convert(String input, Class<? extends DateTime> type, Collection<ValidationError> errors) {

        DateTimeFormatter formatter = DateTimeFormat.forPattern("MM-dd-yyyy HH:mm");
        DateTime datetime = formatter.parseDateTime(input);
        return datetime;
    }

    @Override
        public void setLocale(Locale arg0) {
    }
}

And put this custom TypeConverter class in (one of) your Stripes Extension package(s) which can be defined in web.xml under the filter named StripesFilter.:

并将此自定义TypeConverter类放入(一个)Stripes Extension包中,该包可以在名为StripesFilter的过滤器下的web.xml中定义:

<init-param>
    <param-name>Extension.Packages</param-name>
    <param-value>path.to.my.extensionpackage</param-value>
</init-param>

#1


3  

You are binding into a DateTime object. Stripes has a built in TypeConverter for Date objects but not for DateTime.

您绑定到DateTime对象。 Stripes有一个用于Date对象的内置TypeConverter,但没有用于DateTime。

When you change myDate to java.util.Date Stripes' DateTypeConverter will pick it up.

当你将myDate更改为java.util.Date时,Stripes的DateTypeConverter会将其取出。

Otherwise, if for instance you need support for joda.time.DateTime you'll need to write your own custom TypeConverter (which you don't need because its printed below):

否则,如果你需要支持joda.time.DateTime,你需要编写自己的自定义TypeConverter(你不需要它,因为它打印在下面):

public class JodaDateTimeTypeConverter implements TypeConverter<DateTime> {

    @Override
    public DateTime convert(String input, Class<? extends DateTime> type, Collection<ValidationError> errors) {

        DateTimeFormatter formatter = DateTimeFormat.forPattern("MM-dd-yyyy HH:mm");
        DateTime datetime = formatter.parseDateTime(input);
        return datetime;
    }

    @Override
        public void setLocale(Locale arg0) {
    }
}

And put this custom TypeConverter class in (one of) your Stripes Extension package(s) which can be defined in web.xml under the filter named StripesFilter.:

并将此自定义TypeConverter类放入(一个)Stripes Extension包中,该包可以在名为StripesFilter的过滤器下的web.xml中定义:

<init-param>
    <param-name>Extension.Packages</param-name>
    <param-value>path.to.my.extensionpackage</param-value>
</init-param>