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
To create a basic Spring MVC application using JavaServer Pages Standard Tag Library (JSTL), follow the steps below:
Add necessary dependencies to your pom.xml
:
<!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.x.x</version> </dependency> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.x.x</version> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>javax.servlet.jsp.jstl-api</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency>
AppConfig.java:
@Configuration @ComponentScan(basePackages = "com.example.demo") public class AppConfig { // Other beans and configurations if required }
WebConfig.java:
@Configuration @EnableWebMvc @ComponentScan(basePackages = "com.example.demo.controller") public class WebConfig implements WebMvcConfigurer { @Override public void configureViewResolvers(ViewResolverRegistry registry) { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); registry.viewResolver(resolver); } // Other MVC configurations... }
Implement the WebApplicationInitializer
:
public class WebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(AppConfig.class, WebConfig.class); servletContext.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
Create a basic controller:
@Controller public class HelloController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello from Spring MVC with JSTL!"); return "hello"; } }
Create a JSP file hello.jsp
in /WEB-INF/views/
:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Hello</title> </head> <body> <h2><c:out value="${message}" /></h2> </body> </html>
In the JSP file, the <c:out>
tag is used to display the message
attribute added to the model in the controller. This is a basic usage of JSTL.
With the above configuration, when you run the application and navigate to /hello
, it should render the message "Hello from Spring MVC with JSTL!" using the JSTL tag.
Spring MVC JSTL example:
JSTL (JavaServer Pages Standard Tag Library) is often used in Spring MVC for rendering dynamic content in JSP views. Here's a basic example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Spring MVC JSTL Example</title> </head> <body> <c:out value="Hello, ${user}" /> </body> </html>
In this example, the ${user}
is an EL (Expression Language) expression that gets replaced with the actual value.
Spring MVC JSTL core tags example:
Here's an example using JSTL core tags in a Spring MVC JSP:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:if test="${condition}"> This is displayed if the condition is true. </c:if>
JSTL fmt tags in Spring MVC:
JSTL fmt
tags are used for formatting and localization. Here's an example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <fmt:formatDate value="${date}" pattern="yyyy-MM-dd" />
This tag formats the date according to the specified pattern.
Spring MVC JSTL form handling:
JSTL can be used for form handling in Spring MVC views. For example, handling a form submission:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <form action="submitForm" method="post"> <c:forEach items="${formFields}" var="field"> <input type="text" name="${field}" /> </c:forEach> <input type="submit" value="Submit" /> </form>
This example uses a c:forEach
tag to iterate over form fields.
JSTL expression language in Spring MVC:
EL (Expression Language) is commonly used with JSTL in Spring MVC views. It allows you to access and manipulate data directly in the JSP pages.
<c:out value="${user.name}" />
This example outputs the name
property of the user
object.