【发布时间】:2012-11-24 19:52:00
【问题描述】:
我是 Java 和 Springs 框架的新手。这是我的问题,由 n00b 提出(对不起,如果我听起来像二年级学生)。我试图让一个选择下拉列表出现在我的表单上,其中包含可选国家列表。数据存储在 MySQL 表中,并包含带有国家名称的单个列。我显然做错了什么。我希望这只是一个我缺少的简单事情引起的错误。提前感谢您的帮助。
我们正在使用 WebFlow。我的流 XML 中有以下内容:
<on-start>
<evaluate expression="flowControllerActions.retrieveMBSAddressInfo()" result="address" />
<evaluate expression="flowControllerActions.retrieveCountryInfo()"
result="flowScope.countryListing" />
</on-start>
retrieveCountryInfo() 如下所示:
public Map<String, String> retrieveCountryInfo() throws IOException {
LOGGER.debug("inside retrieveMBSAddressInfo");
Map<String, String> countryInfo = (Map<String, String>) coaService.getCountryInfo();
return countryInfo;
}
coaService.getCountryInfo() 如下所示:
public Map<String, String> getCountryInfo() {
ArrayList <Country> cList = coaDao.getSelectableCountries();
LinkedHashMap<String, String> retval = new LinkedHashMap<String, String>();
for ( Country c : cList){
retval.put(c.getName(), c.getName());
log.debug(c.getName());
}
return retval;
}
coaDao.getSelectableCountries(),填充列表如下所示:
@SuppressWarnings("unchecked")
@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
public ArrayList<Country> getSelectableCountries() {
String myLike = "%";
Session mySession = sessionFactory.getCurrentSession();
ArrayList<Country> countries = (ArrayList<Country>) mySession
.createCriteria(Country.class)
.add(Restrictions.like("name", myLike)).list();
return countries;
}
最后,JSP 页面 尝试像这样呈现选择元素:
<td>
<form:select path="country" id="country">
<form:options items="${countryListing}"/>
</form:select>
</td>
【问题讨论】:
-
这是我得到的错误:
EL1008E:(pos 0): Field or property 'country' cannot be found on object of type 'java.util.HashMap' -
似乎在您的 jsp 页面上您的 modelAttribute 是一个 HashMap,它没有您在
path="country"中指定的国家/地区字段。您要将此字段绑定到什么对象? -
你需要一个带有
String country字段的对象来放入你的modelAttribute表单 -
正如@rptmat57 所说,您的
<form:select>正在尝试设置表单模型对象的“国家”属性。请张贴您的<form:form>标签。和/或你的<view-state model=...>。
标签: java spring dojo spring-webflow