如何获取在上一页中选择的值的名称以显示在链接页面上

时间:2019-02-26 22:55:36

标签: java html spring-mvc jsp model-view-controller

我希望能够在下一页的下拉列表中看到用户选择的选项的值。 我正在努力做到这一点。 因此,用户有一个下拉列表,可以在其中选择要去的国家/地区列表,其点击的国家/地区将显示在另一页上 我该怎么办可以有人指导我

  <%@ page language="java" contentType="text/html;   charset=UTF-8"
     pageEncoding="UTF-8"%>
  <!DOCTYPE html>
  <html>
 <head>
  <meta charset="UTF-8">
 <form action ="some.jsp" class="form">
  <form:form method = "POST"  action=/Country/  model attribute = "model"> 



 </div>
 <a id="ToProductsBtn" href="/Country/" class="btn btn-default">lets go</a>


  </form:form>


 <title>choose a country</title>
</head>
<body>
<h1>choose your next destination</h1>
 <select> 
    <option value="" disabled="disabled" selected="selected">Please select a country</option>
     <option value="England">England</option>
    <option value="Scotland">Scotland</option>
  <option value ="ireland">Ireland</option>
   <option value="northern Ireland">Northern Ireland</option>
  <option value="spain">Spain</option>
  <input type="submit" href="/Country/">


  <a id="ToProductsBtn" href="/Country/" class="btn btn-default">lets go</a>

 </select>
 <!DOCTYPE html>
 <html>
 <head>


</body>
</html>


  </select>

</form>
 </body>
   </html>

,然后从他们选择的国家/地区显示在此页面上:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Welcome</title>
 </head>
 <h1></h1>

 <h2> ${name}</h2>
 <body>

 </body>
</html>

我的控制器类:

package travel.controller;

 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.servlet.view.RedirectView;

   import travel.domain.Order;


   @Controller

  public class Place {
      @RequestMapping(value="/addcountry")

   public String place(@RequestParam(value="name", required=false, defaultValue="PLACE") String name, Model model) {
    model.addAttribute("name", name);
     return "form/Place";}

  @RequestMapping (value="/Country")
  public String Country(@RequestParam(value="Mycountry",   required=false,   defaultValue="myCountry") String name, Model model) {
     model.addAttribute("name", name);
    return "form/Country";}
  }

1 个答案:

答案 0 :(得分:0)

您首先需要提取从选择标签中选择的值,为此,您可以为其指定一个ID,然后访问所选的项目值。例如

<select id="country-selection">
    <option>Please select a country</option>
    <option value="England">England</option>
    <option value="Scotland">Scotland</option>
    <option value ="ireland">Ireland</option>
    <option value="northern Ireland">Northern Ireland</option>
    <option value="spain">Spain</option>
</select>

不需要,无需在选择中添加链接或输入。

然后,您需要将选定的参数传递给控制器​​,可以创建按钮或简单链接,并添加事件监听器,该事件监听器在用户单击时执行功能:

<script>
    $(document).ready(function () {
        $('#redirect-country-bttn').on('click', function (e) {
            e.preventDefault();
            var country = $("#country-selection option:selected").val();
            var url = /country + "?country=" + country;
            if( country !== "Please select a country"){
                window.open(url,"_self")
            }
        });
    })
</script>

最后,您可以在控制器上使用其名称访问查询参数:

@RequestParam(name="country", required=false, defaultValue="myCountry") String name

更好的是,如果将变量命名为查询参数,那么spring mvc可以直接推断出该变量:

@RequestParam(required=false, defaultValue="myCountry") String country