熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

jstl實現在jsp中動態添加下拉列表項

2022-06-13   來源: JSP教程 

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

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

  寫個簡單的servlet

  public class gotoMall extends HttpServlet { public void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException { ArrayList<Stu> stus=new ArrayList<Stu>(); Stu stu=new Stu(); stusetId(); stusetName("aa"); stusetId(); stusetName("bb"); stusadd(stu); stusadd(stu); requestsetAttribute("stus" stus); requestgetRequestDispatcher("/WEBINF/admin/myjsp")forward(request response); } public void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException { thisdoGet(request response); } }

  myjsp

  <%@ page language="java" import="javautil*" pageEncoding="utf"%> <! 引入jstl標簽庫 > <%@ taglib uri="%> <!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" " <html> <head> <script type="text/javascript" language="javascript" src="js/js_deletejs" charset="gbk"></script> <link href="css/maincss" rel="stylesheet" type="text/css" /> <meta httpequiv="ContentType" content="text/html; charset=utf" /> <title>My JSP page</title> </head> <body> LIST:<select> <! 前提是已經把stus這個list放到了requestattribute中 在servlet或action中 requestsetAttribute(stus); > <c:forEach items="${stus}" var="stu"> <! Stu類中必須有set和get方法 > <option>${stuname}</option> </c:forEach> </select> </body> </html>


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19829.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.