【问题标题】:Refreshing Page With A Parameter in JSPJSP中带有参数的刷新页面
【发布时间】:2016-10-11 20:59:51
【问题描述】:

我的 Web 应用程序中有一个 JSP 页面,我想用 option 的参数刷新页面。

<select class="form-control combobox"  name="education" >
    <option value="" disabled selected hidden>Choose an education</option>
    <option>English</option>
    <option>French</option>
    <option>Dutch</option>
    <option>Spanish</option>
</select>

我想用这个参数再次查询我的数据库。例如;

 <%

    BookDAO bdao = new BookDAO();
    for (TblBooks book : bdao.getAllBooks()) {%>
    <tr>
        <td><%=book.getTittle()%></td>
        <td><%=boek.getAuthor()%></td>
        <td><%=boek.getCategory()%></td>
        <td><%=boek.getEducation()%></td>
        <td><input type='checkbox' name ='chk1' /></td>
    </tr>

<% }%>

我可以使用 request.getParameter("education") 获取参数,但是如何刷新页面并使用此参数再次查询?

【问题讨论】:

  • 是用原来的参数还是新选择的参数刷新页面?
  • @Jason 使用新参数。
  • 当控件中的选择发生变化时,这个页面刷新是否应该发生?
  • @Jason 是的。谢谢你的回答。我想用javascript做,但我想不通。

标签: java jsp jakarta-ee


【解决方案1】:

在 Javascript 中,您可以通过以下方式要求网页重新加载(完全相同的 URL):

location.href = location.href;

您可以要求网页使用相同的机制加载不同的位置:

location.href = 'http://www.google.com'; // or whatever destination url you want

可以获取select控件的值(需要有id属性):

var ctrl = document.getElementById("idOfSelectControl");
var selectedOption = ctrl.options[ctrl.selectedIndex].value;

所以,如果你更新你的 JSP 给控件一个 id:

<select class="form-control combobox" id="education" name="education" >

您应该能够重新加载页面:

var educationControl = document.getElementById("education");
var selectedOption = educationControl.options[educationControl.selectedIndex].value;
location.href = "http://mysite.example/mypage?education=" + selectedOption;

【讨论】:

    【解决方案2】:

    在 Jason 的帮助下,我解决了这个问题。我在我的选择标签中添加了一个 onchange 事件

    onchange="changeThePage()"
    

    然后我定义了一个javascript函数。

    function changeThePage() {
    
    var selectedOption = "book.jsp?education=" + $("#education option:selected").text();
    
    location.href =  selectedOption;
    
    }
    

    然后我得到了参数

    String education = request.getParameter("education");
    

    然后我将String education 作为参数添加到我的bdao.getAllBooks(education)) 方法中进行查询。

    【讨论】:

      【解决方案3】:

      '

      <script>
      function abc(){
          var yourSelect = document.getElementById( "ddlViewBy" );
          var val = yourSelect.options[ yourSelect.selectedIndex ].value;
          //window.location.href=window.location.href;
          alert(val);
          window.location.replace("index.jsp?name="+val);
      }
      </script>
      
      <select id="ddlViewBy" onchange="abc()">
        <option>select</option>
        <option value="1">test1</option>
        <option value="2">test2</option>
        <option value="3">test3</option>
      </select>
      <%
      String name=request.getParameter("name");
      if(name!=null){
          %>
          <table>
          <tr>
          <td>author<%=name%></td>
          <td>book name<%=name%></td>
          </tr>
          </table>
      
          <%
      }
      %>
      

      嗨, 试试这个,这会有所帮助, 当您选择一个值时,它将调用 abc,函数,然后您在 scriptlet 中分配该值..并且页面也在刷新。在 scriptlet 中,您可以使用 request.getParameter("name") 访问该值。 现在您可以在 scriptlet 中编写代码了..希望对某人有所帮助.. :) 谢谢...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-28
        • 1970-01-01
        • 2021-02-18
        • 1970-01-01
        • 1970-01-01
        • 2014-11-15
        • 2015-09-17
        • 1970-01-01
        相关资源
        最近更新 更多