我使用的是ssh框架  我在action层的时候把值放入了session中  我跳转到了指定的页面  我想把session中的值动态的添加到下拉列表框中 请问有什么方法,最好写出demo

解决方案 »

  1.   

    。。
    方法很多。
    1.struts标签
    2.javascript
    3.jquery-select.js
    随便了。。
      

  2.   

    <script language="JavaScript" type="text/JavaScript">
    var obj=document.getElementById('testCaseName'); //你的select标签的ID
    obj.options.add(new Option("","")); //如果第一个是空,则这么填,否则不要这行
    <%
    String[] testCaseLst = new String{"aa","bb"};//假设这里的testCaseLst 是你的session取出来的对象列表
    if (testCaseLst!=null){ 
    for (int i = 0; i < testCaseLst.length; i++) {
    String existName=testCaseLst[i]; //这里分别取到label和value。
    %>   
    obj.options.add(new Option("<%=existName%>","<%=existName%>"));
        <%
    }
    }
        %>
    </script>
      

  3.   

    对了上面那段代码得放到你的select标签的下面,保证页面加载的时候,var obj=document.getElementById('testCaseName'); 能取到你的标签,否则会出错
      

  4.   


    <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    </head> <body>
    <a href="Select_conn.action">select</a> <select name="selectid">
    <%
    if (session.getAttribute("abc") != null) {
    String str = session.getAttribute("abc").toString();
    String[] _str = str.split(",");
    for (int i = 0; i < _str.length; i++) {
    %>
    <option><%=_str[i]%></option>
    <%
    }
    }
    %>
    </select>
    </body>
    </html>
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts> <package name="StrutsAction" namespace="/" extends="struts-default">
      <action name="Select_*" class="com.xlh.struts.action.ConnectionAction"
      method="{1}">
        <result name="success">index.jsp</result>
      </action>
    </package></struts>package com.xlh.struts.action;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;public class ConnectionAction extends ActionSupport { String msg = "fail" ;

    public String conn() {

              ActionContext context = ActionContext.getContext() ;
              HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST) ;
              request.getSession().setAttribute("abc","a,b,d") ;
              
              msg = "success" ;
              
              return msg ;
              
    }
    }
      

  5.   

    你既然都用ssh框架了 你这个表现层的问题使用jstl的核心库就可以OK
    想把你的Bean放入作用域
    之后在页面显示
    demo
    <c:choose>
     <c:when test="${requestScope.admin.roleID == 1}">
      <select name="role">
       <option title="role" value="1" selected="selected">--------------1--------------   </option>
       <option title="role" value="2">--------------2--------------</option>
       </select>
     </c:when>
     <c:otherwise>
      <select name="role">
       <option title="role" value="1" >--------------1--------------</option>
       <option title="role" value="2" selected="selected">--------------2--------------   </option>
      </select>
     </c:otherwise>
    </c:choose>
      

  6.   


           <%
                    if (session.getAttribute("abc") != null) {
                        String str = session.getAttribute("abc").toString();
                        String[] _str = str.split(",");
                        for (int i = 0; i < _str.length; i++) {
                %>
                <option><%=_str[i]%></option>
                <%
                    }
                    }
                %>
      这种代码出现在页面是非常的不舒服。
        O(∩_∩)O哈哈~