【问题标题】:how to get the name of a value chosen on previous page to display on linked page如何获取在上一页上选择的值的名称以显示在链接页面上
【发布时间】:2023-03-10 17:40:01
【问题描述】:

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

  <%@ 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";}
  }

【问题讨论】:

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


    【解决方案1】:

    您首先需要提取从 select 标签中选择的值,为此您可以给它一个 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 2014-05-12
      • 2019-01-14
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多