Struts 2 中的 bean 属性和模型驱动 [英] beans properties and modelDriven in struts 2

查看:23
本文介绍了Struts 2 中的 bean 属性和模型驱动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 struts 2 的新手.我创建了一个操作类,该类使用 ModelDriven 接口将数据从 JSP 页面插入到 bean 中.

I am new to struts 2. I created an action class that insert data from JSP page to a bean using ModelDriven interface.

问题是我在 bean 中有一些非 'String 属性,如LongDate` ...等问题是:当我在 jsp 页面中按下提交按钮时,我收到一条错误消息,指出它没有找到该特定属性的字符串设置器.例如,如果我在我的豆子里<代码>

The problem is that i have some non 'Stringproperties in the bean likeLong,Date` ... etc The problem is: when i press submit button in the jsp page i get an error saying that it did not find the a string setter for that particular property. for example if i have in my bean

   package com.hsms.aseza.enteringApproval

   Class EnteringApproval

       private Date approvalDate

      Date getApprovalDate()
      {
         return employeeId;
      }

     void setApprovalDate(Date employeeId)
     {
         this.employeeId = employeeId;
     }

当实现模型驱动的动作类被执行时,它会触发一个运行时异常

when the action class that implement the model driven is executed, it fires a run time exception

    java.lang.NoSuchMethodException:  com.hsms.aseza.enteringApproval.EnteringApproval.setApprovalDate([Ljava.lang.String;)

有没有办法在不为我的 Date 属性编写 String setter 的情况下解决这个问题?

Is there anyway to solve this problem without writing a String setter for my Date property?

推荐答案

我认为您的问题是转换,即从 String 转换为您的 java.util.Date 对象.这个扩展 this 负责将 String 转换为 Long 等其他类型,Double 等.如果您检查 DefaultTypeConverter 的源代码,您不会看到 java.util.Datejava.sql.Date 的任何转换.所以我认为你应该做的是为 approvalDate 编写一个转换器.我的上一篇 这将指导您完成该过程,您只需要编辑代码以满足您的需要.

I think that your problem is conversion i.e. conversion from String to your java.util.Date Object. This class extends this which is responsible for converting from String to other types like Long, Double etc. If you check the source code for DefaultTypeConverter, you won't see any conversion for either java.util.Date or java.sql.Date. So i think what you should do is to write a converter for approvalDate. My previous post on this will guide you on the procedure, all you will need is to edit the code to suit your needs.

在你的jsp上,按照这个日期选择器示例使用与 SimpleDateFormat 在转换器的 convertFromString 方法中进行转换并返回转换后的 java.util.Datejava.sql.Date.

On your jsp, follow this datepicker example Use the same format i.e date format used to display your date in your jsp with SimpleDateFormat to do your conversion in convertFromString method of your converter and return the converted java.util.Date or java.sql.Date.

如果您在执行此操作时遇到问题,请告诉我.

Let me know if you have issues implementing this.

这篇关于Struts 2 中的 bean 属性和模型驱动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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