Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaSpring MVC JSTL Configuration

Spring MVC JSTL Configuration

JavaServer Pages Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. Here we will be discussing how to use the Maven build tool to add JSTL support to a Spring MVC application. also, you’ll learn how to activate JSTL tags in a Spring MVC application.

Step 1: JSTL maven dependencies

XML




<dependency>
  <groupid>javax.servlet</groupid>
  <artifactid>jstl</artifactid>
  <version>1.2</version>
  <scope>runtime</scope>
</dependency>
   
<dependency>
  <groupid>taglibs</groupid>
  <artifactid>standard</artifactid>
  <version>1.1.2</version>
  <scope>runtime</scope>
</dependency>


Step 2: To resolve JSTL views, use InternalResourceViewResolver.

2.1: Spring JSTL Java Configuration

// Annotation 
@Bean
// Method 
public ViewResolver configureViewResolver() 

{
  InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
  
  viewResolve.setPrefix("/WEB-INF/jsp/");
  viewResolve.setSuffix(".jsp");
  
  return viewResolve;
}

2.2: Spring JSTL XML Configuration

XML




<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
  <property name="prefix">
    <value>/WEB-INF/jsp/</value>
  </property>
  <property name="suffix">
    <value>.jsp</value>
  </property>
</bean>


Step 3: Use JSTL tags in JSP files

HTML




<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<h1>Welcome message : <c:out value="${message}"></c:out></h1>


RELATED ARTICLES

Most Popular

Recent Comments