Setup ViewResolver
Spring MVC is a Web MVC Framework for building web applications. In generic all MVC frameworks provide a way of working with views. Spring does that via the ViewResolvers, which enables you to render models in the browser without tying the implementation to specific view technology. Read more here: ViewResolver in Spring MVC. So for setting up ViewResolver go to the CalculatorAppConfig.java file and write down the code as follows
@Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/view/"); viewResolver.setSuffix(".jsp"); return viewResolver; }
And below is the updated code for the CalculatorAppConfig.java file after writing the code for setting up the ViewResolver.
File: Updated CalculatorAppConfig.java
Java
package com.w3wiki.calculator.config; 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; @EnableWebMvc @Configuration @ComponentScan (basePackages = "com.w3wiki.calculator.controllers" ) public class CalculatorAppConfig { // setup ViewResolver @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix( "/WEB-INF/view/" ); viewResolver.setSuffix( ".jsp" ); return viewResolver; } } |
Spring MVC – Iterating List on JSP using JSTL
JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web pages. They can now simply use a tag related to the task that they need to implement on a JSP page.
To read more in-depth about JSTL refer to this article: JSP Standard Tag Library
To iterate a List on JSP, the <c:forEach> is a commonly used.
Syntax:
For example, ${list} points to a List<Object>, then the following
<c:forEach var="item" items="${list}" > ${item} </c:forEach>
does basically the same as following in “core Java”:
for (Object item : list) { System.out.println(item); }
In this article, we are going to discuss how can we iterate the List on JSP using JSTL in detail with an example project in Spring MVC.
Contact Us