使用jsp将项添加到列表框

时间:2014-10-24 06:09:49

标签: javascript jsp servlets

我是Servlets和Java的初学者我想帮助将servlet中的项添加到列表框中。我已经厌倦了但没有成功。我想在列表框中添加多个项目。

  this my servlet section

 String[] myStringArray = {"Motorola Solutions TC55","k35"};
      response.setIntHeader("default", 5);
        request.setAttribute("name",":Motorola Solutions TC55");
        request.setAttribute("item","myStringArray");

        request.getServletContext().getRequestDispatcher("/default.jsp").forward(request, response);

这是myjsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Include Manufacture name</title>
</head>
<body  >
<p id ="L1">Name :- <%=request.getAttribute("name")%></p>
<select>
<c:forEach items="${items}" var="item">
    <option value='${item}'>${item}</option>
</c:forEach>

</select>
<button type="button" >Change Content</button>
</body>
</html>

我只想将数组中的值添加到列表框

1 个答案:

答案 0 :(得分:0)

您可以使用像

这样的smth
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

....

<c:forEach items="${items}" var="item">
    <option value='${item}'>${item}</option>
</c:forEach>

$ {items}数组包含选项列表。

如果他们有不同的ID和标题字段,它将看起来像

<c:forEach items="${items}" var="item">
    <option value='${item.id}'>${item.title}</option>
</c:forEach>
相关问题