0

我在 jsp 文件中有一个下拉列表

<select name="PID" id="PID" style="min-width: 100px;" >

<% 
for(somelist pm : List) { %>
<option value="<%=pm.getsmthing()%>" selected><%=pm.getName()%></option>
<option  value="-1">Select a Property</option>

这是在一个表格里面。我想在提交表单后记住选定的项目名称。

4

2 回答 2

1

Just print selected conditionally whenever the associated request parameter has exactly the same value.

<option value="<%=pm.getsmthing()%> <%=(pm.getsmthing().equals(request.getParameter("PID")) ? "selected" : "")%>><%=pm.getName()%></option>

Do not store it in the session. It would affect all pages in all browser tabs/windows in the same session which may result in "wtf?" experiences of the enduser.


Unrelated to the concrete problem, that's pretty an oldschool way of writing JSPs. Learn JSTL and EL. Your code would then look like this:

<select name="PID" id="PID" style="min-width: 100px;">
  <c:forEach items="${List}" var="pm">
    <option value="${pm.smthing}" ${pm.smthing == param.PID ? 'selected' : ''}>${pm.name}</option>
    <option value="-1">Select a Property</option>
  </c:forEach>
</select>
于 2012-04-27T16:13:13.510 回答
0

将您选择的值添加到 HTTP 会话并在您需要显示表单时随时检索它。

于 2012-04-27T10:53:20.773 回答