struts2中的下拉列表

时间:2012-03-06 10:20:40

标签: struts2

如何在数据库中填充struts2中网页的下拉列表。下拉列表的项目将从数据库中获取。

3 个答案:

答案 0 :(得分:2)

您需要在动作类中使用getter和setter创建一个List,而您所要做的就是在Action的execute方法中填充列表。

行动类

public class MyListAction extends ActionSupport{

  private List<String> country;
  getters and setters 

  public String execute() throws Exception{

    country=new ArrayList<String>();
    countr=fill country from database;
    return SUCCESS;
  }

}

现在你需要有一个带有以下条目的S2选择标签

<s:select label="Select Country"
       name="country"
       headerKey="-1" headerValue="Select Country"
       list="%{country}"

/>

此处 list 是一个可填充的Iterable源。如果列表是Map(键,值),则Map键将成为选项&#39; value&#39;参数和Map值将成为选项体。 因此,list=%{country}将被OGNL评估为动作类中的getCountry()方法,并将从值堆栈中获取所需的列表以填充下拉列表

答案 1 :(得分:2)

为了在struts2中创建列表,我们可以使用实现Preparable。

public class CountryListAction extends ActionSupport implements Preparable {

 private List<String> country;
 getters and setters 

 public String prepareCountryList() throws Exception{

  country=new ArrayList<String>();
  country=fill country from database;

 }

 public String countryList() throws Exception{
  return SUCCESS;
 }

} 

对于jsp,请使用:

<s:select label="Select Country"
   name="country"
   headerValue="Select Country"
   list="%{country}"
/>

可调参数接口将在调用countryList之前调用prepareCountryList方法,并且将预先填充jsp。您可以使用struts2中的单个方法填充尽可能多的下拉列表。

答案 2 :(得分:0)

您可以使用以下代码:

   <s:select name="someName" id="someId" 
list="someMap_or_List" onchange="someFunction();"/>

这里有一些Map_or_List是您可以用来填充下拉列表的列表或地图。

相关问题