Struts 2下拉示例问题

时间:2011-03-22 17:39:29

标签: struts2

我正在尝试使用struts2标记处理下拉列表。但到目前为止我没有成功:下面是.jsp页面,动作类,struts.xml文件和所有相关代码。任何帮助将不胜感激。谢谢。

注意:我收到此错误: - >标记'select',字段'list':请求的列表键'country'无法解析为collection / array / map / enumeration / iterator类型。例如:人或人。{name} - [unknown location]

...........
Index.jsp:
.............

<%@ page contentType =“text / html”pageEncoding =“UTF-8”%> <%@ taglib uri =“/ struts-tags”prefix =“s”%>

    <h3>Struts 2: UI Tag Example - Registration Page Demo </h3><hr>
    <s:form action="register">
        <s:select label="Country" list="country" listKey="countryAbbr" listValue="countryName" />
        <s:submit/>
    </s:form>

......................
struts.xml中:
......................

<package name="com.uitagdemo" extends="struts-default">
    <action name="register" class="com.uitagdemo.RegisterAction" >
        <result name="success">/success.jsp</result>
        <result name="input">/index.jsp</result>
    </action>
</package>

...........................
RegisterAction.java
...........................
公共类RegisterAction扩展了ActionSupport {

private List<Country> country;

public String execute() {
    return SUCCESS;
}

public List<Country> getCountry(){

    country = new ArrayList<Country>();
    country.add(new Country("IN", "INDIA"));
    country.add(new Country("US", "USA"));
    country.add(new Country("FR", "FRANCE"));
    return country;
}

}

.......................

的success.jsp .......................

&lt;%@ page contentType =“text / html”pageEncoding =“UTF-8”%&gt; &lt;%@ taglib uri =“/ struts-tags”prefix =“s”%&gt;

                      Struts 2:UI标记示例                   

Struts 2:UI标签示例 - 注册页面演示


    Country: <s:property value="country" /><br>

</body>

......................
Country.java
......................

公共类国家{

private String countryAbbr;
private String countryName;

public Country() {
}

public Country(String countryAbbr, String countryName) {
    this.countryAbbr = countryAbbr;
    this.countryName = countryName;
}

public String getCountryName() {
    return countryName;
}

public void setCountryName(String countryName) {
    this.countryName = countryName;
}

public String getCountryAbbr() {
    return countryAbbr;
}

public void setCountryAbbr(String countryAbbr) {
    this.countryAbbr = countryAbbr;
}

}

2 个答案:

答案 0 :(得分:0)

在这种情况下,您使用Country类作为Map。最简单的解决方案是使用适当的getter / setter创建一个名为country的Hashmap属性,并使用您需要的内容填充它。

然后在你的JSP中你可以这样说:

<s:select label="Country" list="country.keys">

或者你可能更喜欢

<s:select label="Country" list="country.values">

答案 1 :(得分:0)

工作流程应该与上述相反。它应该是

action that fills select tag(calls getcountry) -> page with form -> action(to which form will be submitted)

但我们已经

 page with form -> submitted to action that fills the select, but form has already been displayed.