• Spring MVC Form Tags Examples

    Spring MVC Form Tags Examples

    Below are some Spring MVC Form Tags Examples. These shows how to use form inputs, radiobutton, checkboxes and error handlers.

    Consider the below class as our DTO:

    public class User {
        private String username;
        private String password;
        private String[] genderArray = {"Male", "Female"};
        private String gender;
        private String[] divisionArray = {"IT", "Finance", "Administration", "Business"};
        private String division;
        private String[] languagesArray = {"Java", "C#", "C", "Python", "Ruby"};
        private String[] languages;
        public String[] getGenderArray() {
            return genderArray;
        public String[] getDivisionArray() {
            return divisionArray;
        public String[] getLanguagesArray() {
            return languagesArray;
        public String getUsername() {
            return username;
        public void setUsername(String username) {
            this.username = username;
        public String getPassword() {
            return password;
        public void setPassword(String password) {
            this.password = password;
        public String getGender() {
            return gender;
        public void setGender(String gender) {
            this.gender = gender;
        public String getDivision() {
            return division;
        public void setDivision(String division) {
            this.division = division;
        public String[] getLanguages() {
            return languages;
        public void setLanguages(String[] languages) {
            this.languages = languages;

    In our controller method, we have added our user form as:

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String registerUser(ModelMap modelMap) {
        modelMap.addAttribute("user_form", new User());
        return "register";

    First, add the form taglib on top of your jsp file:

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

    Using Form Input

    To add input type text for username field:


    Using Form Password

    To add input type password for password field:


    Using Form Radio Buttons

    There are two ways you can add radiobuttons in your form. First, if you have list of items for your radio buttons, you can just:


    this will get the gender array from user dto and populate it with radio buttons. The way is that you can manually create your radio button:


    Using Form Select Dropdown Box

        Select Division

    Using Form Checkbox


    Adding Form Errors

    Form errors are those error texts that are automatically added if there is a validation error in the form. To add form error, add the below code to your jsp where you want to show the error:


    The default element for error is span. To change this, add the attribute element with your preferred element. For example, instead of span, we want to use p element:


    Related Post