如何用Struts2标签<s:select></s:select>编写一个下拉列表呢?
下面这个是接口
public interface NewsTypeService {     public void insertNewsTpye(NewsType newsType);
  
    public void deleteNewsType(NewsType newsType);     public NewsType findNewsTypeById(Integer id);     public List<NewsType> findAllNewsTypes();     public void updateNewsType(NewsType newsType);
}而下面是ListNewsTypeAction中的execute方法
public String execute() throws Exception {

      Map request=(Map)ActionContext.getContext().get("request");

      request.put("listType", service.findAllNewsTypes());

      return SUCCESS;
 }
根据我提供的这些,能不能帮我写出下拉列表的代码呢?

解决方案 »

  1.   

    <s:select name="theotherId" list="#listType" listKey="id" listValue="username" headerKey="None" headerValue="--请选择--" />仅供参考
      

  2.   

    不行呢,这样会报错, The requested list key '#listType' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
      

  3.   

    看下我的做法吧
    1。action实现ServletRequestAware
    public class MessageAction extends BaseAction implements ModelDriven,ServletRequestAware{
    private HttpServletRequest request;
    2.
    request.put("listType", service.findAllNewsTypes());3。页面
    <s:select list#request.listType" listKey="id" listValue="username" headerKey="None" headerValue="--请选择--" />其中id,username是假定的2个属性名(字段)
    对应option的key和value
      

  4.   

    <s:select list="#request.listType" listKey="id" listValue="username" headerKey="None" headerValue="--请选择--" />少写了="你再试试,我的下拉框 就是这么实现的
      

  5.   

    我整个系统是采用SSH整合的方法呢,数据库的表的属性写在一个bean中,还编写了一个NewsType.hbm.xml文件,增,删,查,改都对应一个action,我上面就提供的一个ListNewsTypeAction,具体的是
    package org.com.action.newsType;import java.util.Map;import org.com.service.NewsTypeService;import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;public class ListNewsTypeAction extends ActionSupport { private NewsTypeService service;
        
        public NewsTypeService getService() {
    return service;
    } public void setService(NewsTypeService service) {
    this.service = service;
    } @SuppressWarnings("unchecked")
    @Override
    public String execute() throws Exception {

    Map request=(Map)ActionContext.getContext().get("request");

    request.put("listType", service.findAllNewsTypes());

    return SUCCESS;
    }
        
    }list="#request.listType"我之前也试过的,一样是报错,如果改成"{#request.listType}",不会报错,页面也会显示下拉列表,都是只显示   --请选择--。但是不能从数据库获取值。我想说,能不能按照我做系统的方法,看下这个下拉列表要怎么写呢
      

  6.   

    1。页面,红色标注的属性只是示例
    <s:select list="#request.listType" listKey="id" listValue="username" headerKey="None" headerValue="--请选择--" />2。action
    request要声明为成员变量public class ListNewsTypeAction extends ActionSupport {
    Map request;execute方法里
    request=(Map)ActionContext.getContext().get("request");
      

  7.   

    确保你在action中已经得到service.findAllNewsTypes()
      

  8.   

    <td>
      <select id="typeId">
        <s:iterator value="listType" id="listType">
          <option value="${id}">${desc}</option>
        </s:iterator>
      </select>
    </td>参考一下
      

  9.   

    <s:select
                list="#request.listType"
                listKey="id"
                listValue="typeName"
                name="typeid"
                id="typeid"
                emptyOption="false"
                headerKey="0"
                headerValue="请选择分类"
                value="#request.listType.typeid"  这个是决定select初始显示的项目
    />www.50dn.cn