How to Use JSTL <form:radiobutton> and <form:radiobuttons> tag in Spring MVC

Here We Use :

JDK : Version: 1.8

IDE : Eclipse IDE for Enterprise Java Developers, Version: 2020-06

Build Tool : Maven,Version: 3.2.0

Spring : Version: 5.2.8

Server : apache-tomcat-9.0.34

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.corejava</groupId>
  <artifactId>spring-mvc-java-config-model-jstl-radiobutton-radiobuttons</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-mvc-java-config-model-jstl-radiobutton-radiobuttons</name>
  <url>http://maven.apache.org</url>
  <properties>
    <springframework.version>5.2.8.RELEASE</springframework.version>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>spring-mvc-java-config-model</finalName>
    <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.0</version>          
    </plugin>           
  </plugins>
  </build>
</project>

MyRadiobutton.java

package com.corejava;
import java.util.ArrayList;
import java.util.List;
public class MyRadiobutton  {
private String gender;
private String selectLanguageList;
private List<String> languageList;
public MyRadiobutton() {
	languageList=new ArrayList<String>();
	languageList.add("Core Java");
	languageList.add("JSP");
	languageList.add("Servlet");
	languageList.add("Core Spring");
	languageList.add("Spring MVC");
	languageList.add("Spring Secuiry");
	languageList.add("Core Python");
	languageList.add("Advance Python");
	languageList.add("Linux");
}
public String getGender() {
	return gender;
}
public void setGender(String gender) {
	this.gender = gender;
}
public String getSelectLanguageList() {
	return selectLanguageList;
}
public void setSelectLanguageList(String selectLanguageList) {
	this.selectLanguageList = selectLanguageList;
}
public List<String> getLanguageList() {
	return languageList;
}
public void setLanguageList(List<String> languageList) {
	this.languageList = languageList;
}

@Override
public String toString() {
	return  gender + "|" +selectLanguageList+ "|" + languageList;
}
}

SpringmvcConfig.java

package com.corejava;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.corejava")
public class SpringmvcConfig {
	@Bean
	ViewResolver getViewResolver(){
	InternalResourceViewResolver resolver=new InternalResourceViewResolver("/WEB-INF/view/", ".jsp");
	return resolver;
	}
}

SpringMvcDispatcherServletInitializer.java

package com.corejava;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class[] getRootConfigClasses() {
		return null;
	}

	@Override
	protected Class[] getServletConfigClasses() {
		return new Class[]{SpringmvcConfig.class};
	}

	@Override
	protected String[] getServletMappings() {
		return new String[]{"/"};
	}

}

Controller.java

package com.corejava;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@org.springframework.stereotype.Controller
@RequestMapping("home")
public class Controller {

	@GetMapping("/welcome")
	public String getNavbar() {
		return "navbar";
	}
	
	@GetMapping("/registration-form-with-jstl-radiobutton-and-radiobuttons")
	public String getReg(Model model) {
		MyRadiobutton myRadiobutton=new MyRadiobutton();
		model.addAttribute("myRadiobutton", myRadiobutton);
		return "registration-form-with-jstl-radiobutton-and-radiobuttons";
	}
	
	@PostMapping("/submit-view-registration-form-with-jstl-radiobutton-and-radiobuttons")
	public String submitreg2(@ModelAttribute("myRadiobutton") MyRadiobutton myRadiobutton,Model model) {
		System.out.println(myRadiobutton);
		model.addAttribute("message","Data Save Successfully");
		return "submit-view-registration-form-with-jstl-radiobutton-and-radiobuttons";
	}
}

navbar.jsp

<html>
<head>
<title>Home</title>
</head>
<body>
<a href="registration-form-with-jstl-radiobutton-and-radiobuttons">JSTL radiobutton and radiobuttons Tag</a>
</body>
</html>

registration-form-with-jstl-radiobutton-and-radiobuttons.jsp

 <%@ taglib uri="http://www.springframework.org/tags/form"  prefix="form"%>
<%@ include file="navbar.jsp" %>
<hr>
<form:form action="submit-view-registration-form-with-jstl-radiobutton-and-radiobuttons" modelAttribute="myRadiobutton" method="post">
<table border="2">
  <tr>
    <td>Select Gender:</td>
        <td>
         <form:radiobutton path="gender" value="Male"/>Male
         <form:radiobutton path="gender" value="Female"/>Female
        </td>
  </tr>
  <tr>
       <td>Select One Language</td>
       <td>
        <form:radiobuttons path="selectLanguageList" items="${myRadiobutton.languageList}"/>
       </td>
    </tr>
  <tr>
    <td colspan="1"><input type="submit" value="SUBMIT"></td>
  </tr>
</table>
</form:form>

submit-view-registration-form-with-jstl-radiobutton-and-radiobuttons.jsp

<html>
<head>
<title>Registration</title>
</head>
<%@ include file="navbar.jsp" %>
<hr>
<body>
 <p style="color:green">${ message }</p>
<table border="2">
  <tr>
    <td>Gender</td>
         <td>${myRadiobutton.gender}</td>
  </tr>
  <tr>
    <td>Selected Hobbies: </td>
         <td>${myRadiobutton.selectLanguageList}</td>
    </tr>
</table>
</body>
</html>

index.jsp

<%response.sendRedirect("home/welcome");%>