【问题标题】:Retaining selected values for drop down link in jsp/servelts保留 jsp/servelts 中下拉链接的选定值
【发布时间】:2017-02-20 03:20:20
【问题描述】:

我正在尝试根据数据库中的数据填充下拉列表的数量,例如,如果我有 6 条记录,我将生成 6 个下拉列表。请指定提交后如何保留下拉列表中的值。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.util.ArrayList,java.util.List" %>
<!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>Course Learning Objectives to Student Outcomes Mapper</title>
</head>
<body>

<h1>Course Learning Objectives to Student Outcomes Mapper</h1>

<c:choose>

<c:when test="${empty id}">

<form method="get" action="CLOtoSO">

<select name="id">
<c:forEach items="${CourseIdMap}" var="Course">
    <option value="${Course.value}">${Course.key}</option>
</c:forEach>
</select>
<input type="submit" value="Select" />
</form>
</c:when>

<c:otherwise>

<table border="1">
<tr>
   <td>&nbsp;</td>
<c:forEach items="${SOIdsMap}" var="SOId">
<td title="${SODescriptionsMap[SOId.value]}">${SOId.key}</td>
</c:forEach>
</tr>
<c:forEach items="${CLODescriptionsMap}" var="CLODescription">
<tr>
<td>${CLODescription.value}</td>
<c:forEach items="${SOIdsMap}" var="SOId">
  <td><select form="CLOtoSOform" name="map-${SOId.value}_${CLODescription.key}">
  
  <option value="-">-</option>
  <option value="R">R</option>
  <option value="I">I</option>
  <option value="A">A</option>
  </select></td>
</c:forEach>
</tr>
</c:forEach>
</table>
<form method="post" action="CLOtoSO" id="CLOtoSOform">
  <input type="hidden" name="id" value="${id}">
  <input type="submit" value="Update" />
</form>
</c:otherwise>
</c:choose>
</body>
</html>

【问题讨论】:

  • 您的意思是提交表单后下拉列表中的选定值吗?
  • 是的@VinothKrishnan
  • 我希望在提交后保留第二个下拉列表中的选定值,即 map-${SOId.value}_${CLODescription.key}
  • 你可以使用相同的代码,用 soldId 代替。
  • 你能建议我使用 servlet 吗

标签: jsp servlets


【解决方案1】:

当您将数据(选定的下拉值)提交到 servlet 时,您也可以将其发送回 JSP。

我正在为一个下拉菜单提供示例,

request.setAttribute("selectedId", request.getParameter("id"));
RequestDispatcher rd = sc.getRequestDispatcher("/jsp/mypage.jsp");
rd.forward(request, response);

request.getAttribute("selectedId")

并在您的 JSP 页面中检查您的 request.getAttribute("selectedId") 是否为真。 如果是,请执行以下操作,

<c:forEach items="${SOIdsMap}" var="SOId">
    <select form="CLOtoSOform" name="map-${SOId.value}_${CLODescription.key}">
        <option value="${SOId.key}" ${SOId.key == selectedId ? 'selected="selected"' : ''}>${SOId.value}</option>
    </select>
</c:forEach>

希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多