我想从HTML下拉菜单中获取选定的值。我确实填写了“下拉菜单”,但是需要从“ handlebar.html”中获取选定的值来填写我的下一个下拉菜单。如果您能告诉我如何获取价值,那会很好,我不太在乎是否必须使用javascript或其他任何方法。非常感谢您的帮助^^
HTML代码:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="#" th:action="@{/schaltung}" th:object="${String}" method="post">
<select class="form-control" id="handlebar" name="testOrder">
<option value="">Select Type</option>
<option th:each="test : ${type}"
th:value="${test}"
th:text="${test}">
</option>
</select>
<a href="/schaltung">
<button>Next Step</button>
</a>
</form>
<a href="index.html">Zur Startseite</a>
</body>
</html>
我的Java控制器:
package com.example.servingwebcontent;
import org.apache.commons.collections4.MultiValuedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@Controller
public class WebController {
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "handlebar";
}
@GetMapping("/handlebar")
public String handlebar(@RequestParam(name="name", required=false) String name, Model model) throws IOException {
ProductsWeb productsWeb = new ProductsWeb();
List<String> list = new ArrayList<String>();
list=productsWeb.lenkertyp();
model.addAttribute("type", list);
return "handlebar";
}
@GetMapping("/schaltung")
public String schaltung(@RequestParam(name="name", required=false) String name, Model model) throws IOException {
ProductsWeb productsWeb = new ProductsWeb();
List<String> list = new ArrayList<String>();
list=productsWeb.schaltung("");
model.addAttribute("type", list);
return "schaltung";
}
@GetMapping("/griff")
public String griff(@RequestParam(name="name", required=false) String name, Model model) throws IOException {
ProductsWeb productsWeb = new ProductsWeb();
List<String> list = new ArrayList<String>();
list=productsWeb.griff("");
model.addAttribute("type", list);
return "griff";
}
@GetMapping("/material")
public String material(@RequestParam(name="name", required=false) String name, Model model) throws IOException {
ProductsWeb productsWeb = new ProductsWeb();
List<String> list = new ArrayList<String>();
list=productsWeb.material("");
model.addAttribute("type", list);
return "material";
}
/*String s12 =null;
@PostMapping("/handlebar")
public String addNewType(@ModelAttribute("type") @Valid @RequestBody String type, Model model) {
model.addAttribute("type", type);
s12=type;
System.out.println(s12+"1213645");
return s12;
}*/
}
最佳答案
看来您希望按钮“下一步”将表单发布到Java控制器。现在,有一个带有href的锚标记,该标记包裹了按钮,这与JavaScript中的window.location ='/ schaltung'实质上是相同的(它更改了浏览器URL,但未发布表单)。
要在您的控制器中接收表单值,您需要提交表单。尝试从按钮周围删除<a href>
标记,并将按钮类型设置为“提交”,以使您的按钮看起来像:
<button type="submit">Next Step</button>
那应该提交您的表单并将选定的值发布到Java控制器中。
为了使您的控制器从表单接收参数,您需要将接收函数注释为:@PostMapping或@RequestMapping(以在POST请求中接收HTTP正文的内容)。
在您的情况下,将@GetMapping(“ / schaltung”)更改为@PostMapping(“ / schaltung”)或@RequestMapping(“ / schaltung”)。
在功能“ schaltung”中,为“ testOrder”(要发布的选择标记的名称)添加一个RequestParam:
@RequestMapping("/schaltung")
public String schaltung(@RequestParam(name="name", required=false) String name, @RequestParam String testOrder, Model model) throws IOException {
ProductsWeb productsWeb = new ProductsWeb();
List<String> list = new ArrayList<String>();
list=productsWeb.schaltung("");
model.addAttribute("type", list);
model.addAttribute("testOrder", testOrder);
return "schaltung";
}