Struts:Exception thrown by getter for property

下午学着在利用Stuts提供的MapForm机制来存储和检索字段。在网上google了一下,很多网站都提供了这样的例子:
public class MapForm extends ActionForm
{
private Map map = null;
public void setMap(Map map) {
this.map = map;
}
public Map getMap() {
return this.map;
}
同时增加一个属性方法去设置和获取Map中的数据。
public void setAttribute(String attributeKey, Object attributeValue)
{
getMap().put(attributeKey, attributeValue);
}
public Object getAttribute(String attributeKey)
{
Object keyValue = getMap().get(attributeKey);
return keyValue;
}
这样我们在jsp页面中,我们就可以使用Struts的标签接触这些Map数据。
<html:text property="attribute(key)"/>
于是照着提供的这个例子在本机允许了一下,发现总是报Exception thrown by getter for property formvalue(password) of bean org.apache.struts.taglib.html.BEAN异常。初步估计是getFormvalue方法有错误,可一想网上都是这么做的,而我对java也刚熟悉,所以用了1个小时还是没有解决。后来把这代码仔仔细细的看了一篇,还真被我发现问题了。问题就出在private Map map = null; 这句话上,查了一些java资料,Map是一个接口,而且代码中也没有代码来实例化它,因此像getMap().get(attributeKey)这样的方法当然就会报错。因此对代码做了以下修改:private Map map=new HashMap()。果然,改好程序就能正常运行了。

posted on 2006-07-19 17:15  方崇德  阅读(1402)  评论(1编辑  收藏  举报

导航