jstl實現在jsp中動態添加下拉式清單項

來源:互聯網
上載者:User

 使用下面這個jsp的前,你需要寫一個Stu類,產生set和get方法。

Public Class Stu{    private int id;    private String name;    public void setId(int id){       this.id=id;    }    public int getId(){       return this.id;    }    public void setName(String name){       this.name=name;    }    public String getName(){       return this.name;    }}

寫個簡單的servlet

public class gotoMall extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {ArrayList<Stu> stus=new ArrayList<Stu>();Stu stu1=new Stu();stu1.setId(1);stu1.setName("aa");stu2.setId(2);stu2.setName("bb");stus.add(stu1);stus.add(stu2);request.setAttribute("stus", stus);request.getRequestDispatcher("/WEB-INF/admin/my.jsp").forward(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}}

my.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!-- 引入jstl標籤庫 --><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>  <head>    <script type="text/javascript" language="javascript" src="js/js_delete.js" charset="gbk"></script>    <link href="css/main.css" rel="stylesheet" type="text/css" />       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     <title>My JSP page</title>  </head>    <body>  LIST:<select>  <!-- 前提是已經把stus這個list放到了request.attribute中. 在servlet或action中  request.setAttribute(stus); -->  <c:forEach items="${stus}" var="stu">  <!-- Stu類中必須有set和get方法 -->     <option>${stu.name}</option>     </c:forEach>  </select>  </body></html>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.