【发布时间】:2020-04-20 01:57:15
【问题描述】:
我想从 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;
}*/
}
【问题讨论】:
-
您在选择周围没有任何形式。您想向服务器发布请求,然后使用选定的值吗?你知道网页 HTML 表单是如何工作的吗?
-
抱歉,为该类发布了错误的代码。我更新了,希望你现在可以帮助我
-
你应该从阅读文档开始:thymeleaf.org/doc/tutorials/2.1/…
-
我做到了,但我仍然卡住了
标签: java spring-boot thymeleaf