Reputation: 11
i want to pass thymeleaf dropdown selected value to the spring boot controller,using javascript or jquery or without that
<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>
Upvotes: 1
Views: 3385
Reputation: 1837
Below is the thymyleaf code and there is a dropdown I'm injecting data from the controller.
<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>
Below is the controller from which I'm injecting the data to the dropdown.
@GetMapping("/addexpense")
public String addExpense(Expense expense,Model model){
model.addAttribute("categories",categoryRepo.findAll());
return "addExpense";
}
Below I have attached the controller to which i'm passing the data.(from form)
@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";
}
Upvotes: 0
Reputation: 11
This is my working solution
<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>
Upvotes: 1
Reputation: 51
${camera.cameraId}
in th:selected
attribute will not pass the value to the controller.
th:selected
is a boolean field and needs a condition. The condition depends on case to case but you can do something like this to start with:
th:selected="true"
Upvotes: 0
Reputation: 3059
In your controller just create method to handle your request:
@Controller
YourController {
@RequestMapping(value = "/action", method = RequestMethod.POST)
public String seach(@ModelAttribute Camera camera) {
System.out.println("camera: " + camera);
return "redirect_page";
}
}
and in your view page, make a form
with proper action
and method="post"
params.
Upvotes: 0