Spring MVC Tutorial
Core Spring MVC
Spring MVC - Annotation
Spring MVC - Form Handling
Spring MVC with JSTL
Spring MVC with REST API
Spring MVC with Database
In Spring MVC, the form
tag library simplifies form bindings. To create a listbox (a dropdown where users can select multiple options), you can use the form:select
tag with the multiple
attribute set to true
.
Let's set up an example:
Assume you have a simple model class User
:
public class User { private List<String> languages; // Getter, Setter }
Here's a controller that sets up available languages and binds them to the User
model:
@Controller @RequestMapping("/user") public class UserController { @ModelAttribute("allLanguages") public List<String> getAllLanguages() { return Arrays.asList("English", "Spanish", "French", "German", "Chinese"); } @GetMapping("/create") public String createUserForm(Model model) { model.addAttribute("user", new User()); return "userForm"; } @PostMapping("/save") public String saveUser(@ModelAttribute User user, Model model) { model.addAttribute("selectedLanguages", user.getLanguages()); return "userResult"; } }
To display the listbox:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <form:form action="/user/save" method="post" modelAttribute="user"> <form:select path="languages" items="${allLanguages}" multiple="true" size="5" /> <input type="submit" value="Submit" /> </form:form>
In this JSP view, the form:select
tag with multiple="true"
creates a listbox. The size
attribute specifies the number of visible items in the listbox.
To display the selected languages:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <h2>Selected Languages:</h2> <c:forEach var="language" items="${selectedLanguages}"> <div>${language}</div> </c:forEach>
When you navigate to /user/create
, you should see the userForm
view with a listbox containing languages. After selecting multiple languages and submitting the form, you should be taken to the userResult
view, where the selected languages are displayed.
Note: The form
tag library automatically binds the selected items from the listbox to the languages
attribute of the User
model.
Spring MVC Listbox Example:
Description: This is a basic example demonstrating the usage of a listbox in a Spring MVC form.
Code Snippet: (JSP Page)
<!-- JSP Page --> <form:form method="post" action="/submitForm"> <!-- Other form fields --> <form:select path="selectedItems" multiple="true"> <form:options items="${allItems}" /> </form:select> <input type="submit" value="Submit" /> </form:form>
Using Listbox in Spring MVC Forms:
Description: This example shows how to use a listbox in a Spring MVC form for multiple item selection.
Code Snippet: (JSP Page)
<!-- JSP Page --> <form:form method="post" action="/submitForm"> <!-- Other form fields --> <form:select path="selectedItems" multiple="true"> <form:options items="${allItems}" /> </form:select> <input type="submit" value="Submit" /> </form:form>
Listbox Binding in Spring MVC:
Description: This example illustrates binding a listbox to a model attribute in a Spring MVC form.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @ModelAttribute("allItems") public List<String> getAllItems() { // Logic to fetch all available items } @RequestMapping("/showForm") public String showForm(Model model) { model.addAttribute("command", new MyForm()); return "myForm"; } @PostMapping("/submitForm") public String submitForm(@ModelAttribute("command") MyForm form) { // Process form submission } }
Populating Listbox in Spring MVC:
Description: This example demonstrates populating a listbox in a Spring MVC form with options fetched from the controller.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @ModelAttribute("allItems") public List<String> getAllItems() { // Logic to fetch all available items } @RequestMapping("/showForm") public String showForm(Model model) { model.addAttribute("command", new MyForm()); return "myForm"; } @PostMapping("/submitForm") public String submitForm(@ModelAttribute("command") MyForm form) { // Process form submission } }
Spring MVC Listbox Validation:
Description: This example showcases how to perform validation on a listbox in a Spring MVC form.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @InitBinder protected void initBinder(WebDataBinder binder) { binder.addValidators(new MyFormValidator()); } // Other controller methods }
Listbox with Thymeleaf in Spring MVC:
Description: This example demonstrates using Thymeleaf to create a listbox in a Spring MVC form.
Code Snippet: (HTML Template)
<!-- Thymeleaf HTML Template --> <form method="post" action="/submitForm"> <!-- Other form fields --> <select th:field="*{selectedItems}" multiple="true"> <option th:each="item : ${allItems}" th:value="${item}" th:text="${item}"></option> </select> <input type="submit" value="Submit" /> </form>
Dynamic Listbox in Spring MVC:
Description: This example showcases creating a dynamic listbox in a Spring MVC form, where options change based on user input or other factors.
Code Snippet: (JSP Page)
<!-- JSP Page --> <form:form method="post" action="/submitForm"> <!-- Other form fields --> <form:select path="selectedItems" multiple="true"> <form:options items="${dynamicItems}" /> </form:select> <input type="submit" value="Submit" /> </form:form>
Spring MVC Listbox Size and Multiple Selection:
Description: This example demonstrates configuring the size and allowing multiple selections in a Spring MVC listbox.
Code Snippet: (JSP Page)
<!-- JSP Page --> <form:form method="post" action="/submitForm"> <!-- Other form fields --> <form:select path="selectedItems" size="5" multiple="true"> <form:options items="${allItems}" /> </form:select> <input type="submit" value="Submit" /> </form:form>
Selecting Default Value in Spring MVC Listbox:
Description: This example illustrates selecting a default value in a Spring MVC listbox.
Code Snippet: (JSP Page)
<!-- JSP Page --> <form:form method="post" action="/submitForm"> <!-- Other form fields --> <form:select path="selectedItems" multiple="true"> <form:options items="${allItems}" /> </form:select> <input type="submit" value="Submit" /> </form:form>
Processing Listbox Values in Spring MVC Controller:
Description: This example shows how to process the selected values from a listbox in a Spring MVC controller.
Code Snippet: (Controller)
// Controller class @Controller public class MyController { @PostMapping("/submitForm") public String submitForm(@ModelAttribute("command") MyForm form) { List<String> selectedItems = form.getSelectedItems(); // Process selected items } }