【问题标题】:How to make a select box on a form using Spring如何使用 Spring 在表单上制作选择框
【发布时间】: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 所说,您的&lt;form:select&gt; 正在尝试设置表单模型对象的“国家”属性。请张贴您的&lt;form:form&gt; 标签。和/或你的&lt;view-state model=...&gt;

标签: java spring dojo spring-webflow


【解决方案1】:

我不确定,但试试这样的,

<td>
<form:select path="country" id="country">
    <c:forEach items="${countryListing}" var="countryMap" varStatus="status">
    <option value="${countryMap.key}">${countryMap.value}</option>
</c:forEach>
</form:select>

我希望这行得通! :)

【讨论】:

  • 不会解决他的问题。他正在尝试将字段 country 绑定到没有任何 country 字段的对象
猜你喜欢
  • 2012-12-19
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 2010-11-16
  • 2016-02-22
  • 1970-01-01
  • 2015-11-30
相关资源
最近更新 更多