【问题标题】:Input Date to Struts2 in JSP在 JSP 中向 Struts2 输入日期
【发布时间】:2013-04-10 08:58:26
【问题描述】:

我正在使用带有 Hibernate ORM 的 Struts2。我在this 问题中发现我必须创建一个转换器并使用该转换器注册我的bean Date 属性。我尝试过其他类似的方法:

private Date myDate;
private SimpleDateConverter format = new SimpleDateConverter("MM/dd/yyyy");

public String getMyDate()
{
    return myDate.toString();
}

public setMyDate(String myDate)
{
    try{
        this.myDate = format.parse(myDate);
    } Catch(Exception e) {
        e.printStackTrace();
    }
}

但它不起作用。有人可以解释我为什么吗?由于我上面的代码可以很好地进行转换

【问题讨论】:

  • 你的意思是它不起作用?需要更多解释
  • @UmeshAwasthi 当我尝试打开它时会引发转换异常,但我根据 Kevin Bowersox 的建议修复了它。感谢您的关注,先生。

标签: java struts2


【解决方案1】:

代码使用toString的默认实现返回getter中的类,它应该使用格式化程序进行格式化。

public String getMyDate()
{
    return format.format(myDate);
}

这部分代码还有一个大小写问题:

public setMyDate(String myDate)
{
    try{
        this.MyDate = format.parse(myDate); //should be this.myDate = ...
    } Catch(Exception e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 我按照您的建议进行了尝试,打开运行 jsp 文件时出现此错误Exception thrown by getter for property myDate of bean customer
  • 查看更新,并确保在获取之前设置日期。
  • 已解决。我按照您的建议进行了更改,仍然有错误,所以我将<html:text /> 更改为<input type="text" />,它工作正常。先生非常感谢您。但是请您解释一下为什么<html:text /> 不起作用但<input type="text" /> 起作用?
  • @Doan Cuong,鉴于我对问题的有限看法,我不太确定为什么 struts 标记不起作用,可能是 struts 配置或 Struts 未正确连接 bean。
  • 原因是:<html:text /> 不是 Struts2 tag。它是一个Struts1 tag,您可能正在与Struts1 plugin 一起使用。请不要:)
猜你喜欢
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 2014-09-24
  • 2013-05-16
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
  • 2022-01-26
相关资源
最近更新 更多