我想将百万美元下拉选择值传递给spring引导控制器,使用javascript或jquery或不使用
<select id="cameraList" name="cameraListId" th:field="*{cameraid}" >
<option th:each="camera : ${camera}" th:value="${camera.cameraid}"
th:text="${camera.name}" th:selected="${camera.cameraid}">Camera</option>
</select>
答案 0 :(得分:1)
这是我的工作解决方案
<div class="form-group">
<select class="form-control" id="labelprofppayment" name="paymentfield">
<option th:value="'cash'" th:text="Cash" th:selected="${profpayment == 'cash'}"></option>
<option th:value="'transfer'" th:text="Transfer" th:selected="${profpayment == 'transfer'}"></option>
</select></div>
答案 1 :(得分:0)
在你的控制器中,只需创建处理你的请求的方法:
@Controller
YourController {
@RequestMapping(value = "/action", method = RequestMethod.POST)
public String seach(@ModelAttribute Camera camera) {
System.out.println("camera: " + camera);
return "redirect_page";
}
}
在您的视图页面中,使用正确的form
和action
参数创建method="post"
。
答案 2 :(得分:0)
${camera.cameraId}
不会将值传递给控制器。
th:selected
是一个布尔字段,需要一个条件。条件取决于具体情况,但你可以这样做:
th:selected
答案 3 :(得分:0)
下面是胸腺代码,有一个下拉列表,我正在从控制器注入数据。
<form action="#" th:action="@{/expense/category}" th:object="${expense}" method="post">
<div class="row">
<div class="form-group col-md-8">
<label for="name" class="col-form-label">Expense Name</label>
<input type="text" th:field="*{name}" class="form-control" id="name" placeholder="Name">
</div>
<div class="form-group col-md-8">
<label for="name" class="col-form-label">Expense Description</label>
<input type="text" th:field="*{description}" class="form-control" id="description" placeholder="Description">
</div>
<div class="form-group col-md-8">
<label for="name" class="col-form-label">Amount</label>
<input type="text" th:field="*{amount}" class="form-control" id="amount" placeholder="Amount">
</div>
<div class="form-group col-md-8">
<label class="col-form-label">Category </label>
<select id="categorydropdown" name="categorydropdown" th:field="*{category.id}" >
<option th:each="category : ${categories}" th:value="${category.id}" th:utext="${category.name}"/>
</select>
</div>
<div class="col-md-6">
<input type="submit" class="btn btn-primary" value="Add Expense">
</div>
</div>
</form>
下面是我将数据注入到下拉菜单中的控制器。
@GetMapping("/addexpense")
public String addExpense(Expense expense,Model model){
model.addAttribute("categories",categoryRepo.findAll());
return "addExpense";
}
在下面,我已经将数据传递到的控制器连接了。(来自表单)
@PostMapping("/expense/category")
public String createExpense( @RequestParam(name = "category.id") Long categoryid,@Valid Expense expense, BindingResult result) {
categoryRepo.findById(categoryid).map(expense1 -> {
expense.setCategory(expense1);
return expenseRepo.save(expense);
});
return "redirect:expenses";
}