Flat Preloader Icon

Spring MVC Form Text Field

  • In a Spring MVC application, you can create a text field in a form using the form: input tag from the Spring Form Tag Library. This tag generates an HTML input element for text input. You can use it to capture user input in a form and bind it to a model attribute. Here’s how to create a text field in a Spring MVC form:
  • Assuming you have a form backing bean (a model) with a field named name, you can create a text field as follows:

Include the Spring Form Tag Library:

  • Make sure to include the Spring Form Tag Library at the top of your JSP file where you want to create the form:
				
					<%@ taglib prefix="form" 
uri="http://www.springframework
.org/tags/form" %>

				
			

Create the Form:

  • Use the tag to create the form, specifying the model attribute to which the form data should be bound:
				
					<form:form modelAttribute="myForm" 
method="POST" action
="/processForm">
    <!-- Text Field -->
<label for="name">Name:</label>
<form:input path="name" id="name" />
    <br>
<input type="submit" value="Submit" />
</form:form>

				
			
  • In this example:
    • modelAttribute=”myForm” specifies that the form data should be bound to a model attribute named myForm.
    • generates an HTML input element for the name field of the myForm model. The path attribute specifies the field to bind to, and the id attribute associates the label with the input for accessibility.
    • The form submits data to the /processForm URL using the HTTP POST method when the “Submit” button is clicked.

Controller Method:

  • In your Spring MVC controller, create a method to handle the form submission. This method should map to the action URL specified in the form.
				
					@Controller
public class MyController {

    @GetMapping("/showForm")
    public String showFor
    (Model model) {
 model.addAttribute("myForm", new MyForm()); 
        
return "my-form-page"; 
// The view name
    }

    @PostMapping("/processForm")
    public String processForm
    (@ModelAttribute("myForm")
    MyForm myForm) {
        
        return "confirmation-page"; 
       
    }
}

				
			
  • In this example:
    • The showForm method initializes the form backing bean and returns the view name (my-form-page) where the form is displayed..
    • The processForm method handles the form submission and receives the form data bound to the myForm model attribute.

Form Backing Bean (Model):

  • Ensure you have a form backing bean (in this case, MyForm) with a field named name, along with getter and setter methods for that field.
				
					public class MyForm {
    private String name;

    // Getter and Setter for the 'name' field
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

				
			
  • With these steps, you’ve created a Spring MVC form with a text field for capturing user input. When the form is submitted, the input data will be bound to the name field of the MyForm model object. You can then process and use this data in your controller.