Search code examples
javaspringjspspring-mvcspring-boot

Spring Boot - JSP is not rendering


I am writing a Spring MVC application using Spring boot. I am trying to display a text and a list of Department object which is populated from the Controller class. when the boot application is run the values are getting populated in the Controller's List variable and the page is redirected to the mentioned JSP.

The problem is the output is something like this. Screenshot of the output

I want the values to be populated in the designated EL. Any help is very much appreciated.

Following are the codes used.

Department.jsp

<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en">
<head>
</head>
<body>
    <div>
        <div>
            <h1>${message}</h1>
                <ul>
                <c:forEach var="listValue" items="${DepartmentList}" >
                      <li>${listValue.deptName}</li>
                 </c:forEach>
                 </ul>
        </div>
    </div>
</body>
</html>

SpringMVCApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringMVCApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringMVCApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringMVCApplication.class, args);
    }

}

MVCConfiguration.java

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration
@EnableWebMvc
@ComponentScan(value = "com.practices.spring.mvc")
public class MVCConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setContentType("application/html");

        registry.viewResolver(resolver);
    }

    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }


}

DepartmentController.java

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.practices.spring.mvc.valueobject.Department;

@Controller
public class DepartmentController {


    @RequestMapping(method = RequestMethod.GET, value = "/depts")
    public ModelAndView getAllDepartments(){
        ModelAndView mv = new ModelAndView("Department");
        List<Department> deptList = populateDepartment();

        mv.addObject("DepartmentList", deptList);
        mv.addObject("message", "Hi");


        return mv;
    }




    private List<Department> populateDepartment(){
        List<Department> deptListTemp = new ArrayList<>();
        Department dept1 = new Department("1", "Finance");
        Department dept2 = new Department("2", "CRO");
        deptListTemp.add(dept1);
        deptListTemp.add(dept2);

        return deptListTemp;

    }

}

Department.java

package com.practices.spring.mvc.valueobject;

public class Department {
    private String deptId;
    private String deptName;

    public Department(){}

    public Department (String deptIdentification, String deptartmentName){
        deptId = deptIdentification;
        deptName = deptartmentName;
    }

    public String getDeptId() {
        return deptId;
    }
    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }


    public String toString(){
        return "[deptId : "+deptId +" ! deptName : "+deptName+"]";
    }


}

Solution

  • Can you try adding below dependencies in pom.xml?

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <scope>provided</scope>
    </dependency>