如何在同一jsp页面的下拉列表中保留选定的值? [英] How to retain selected value in a dropdown list in the same jsp page?

查看:274
本文介绍了如何在同一jsp页面的下拉列表中保留选定的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在提交表单后又出于某种原因再次转发到同一页面,下拉列表中的所选选项应保留我第一次输入的所选值.

I want that after submitting my form and then for some reason again forwarding to the same page, the selected option in the drop-down list should retain the selected value I entered first time.

<form action="CitySelection" method="POST">

     <select name="cityname" id="myselect" onchange="this.form.submit()">
         <option value="england">england</option>
         <option value="france">france</option>
         <option value="spain">spain</option>
     </select>

</form>

如何通过此功能增强我上面的表单代码,有什么建议吗?

How I can enhance my above code of form with this feature, any suggestions?

推荐答案

为什么要将硬编码的城市列表放在选择菜单中?为此,使用c taglib:

Why you are putting hard coded city list in your select menu? To do that your jsp page should look like bellow using c taglib:

<%@ taglib prefix="c" uri="java.sun.com/jsp/jstl/core" %>

<form action="<SUBMITTING_URL>" method="post">
      <select name="cityname" id="myselect" onchange="this.form.submit()">
         <c:foreach var="cityname" items="${cityList}">
            <c:choose>
               <c:when test="${not empty selectedCity && selectedCity eq cityname}">
                   <option value="${cityname}" selected = "true">${cityname}</option>
               </c:when>
               <c:otherwise>
                   <option value="${cityname}">${cityname}</option>
               </c:otherwise>
            </c:choose>
         </c:foreach>
      </select>
</form>

在与<SUBMITTING_URL>映射的servlet中,应具有如下的doPost方法:

In your servlet mapped with your <SUBMITTING_URL> should have a doPost method like bellow:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

   String cityName = request.getParameter("cityname"); //retrieving value from post request.
   //do your necessary coding.
   if(validation error or as your wish){
      List<String> cityList = new ArrayList<String>();
      cityList.add("england");
      cityList.add("france");
      cityList.add("spain");
      request.setAttribute("cityList",cityList);
      request.setAttribute("selectedCity",cityName);

      RequestDispatcher dispatcher = request.getRequestDispatcher("cityform.jsp");  
      dispatcher.forward(request, response);
   } else {
      //do other things
   }
}

您的doGet方法应如下所示:

And your doGet method should look like bellow:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      List<String> cityList = new ArrayList<String>();
      cityList.add("england");
      cityList.add("france");
      cityList.add("spain");
      request.setAttribute("cityList",cityList);
      request.setAttribute("selectedCity",null);

      RequestDispatcher dispatcher = request.getRequestDispatcher("cityform.jsp");  
      dispatcher.forward(request, response);
}

现在,当您想第二次转到页面时,将看到已选择的选定值.

Now when you like to go to the page second time, you will see the selected value, you have chosen.

这篇关于如何在同一jsp页面的下拉列表中保留选定的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆