jstl実装jspにおけるドロップダウンリスト項目の動的追加
次のjspを使用する前に、Stuクラスを書いてsetとgetメソッドを生成する必要があります.
簡単なservletを書く
my.jsp
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>