Monday, June 8, 2020

Creating webapp using Spring Boot and Maven

I created a base Spring Boot project to see all the dependencies I needed for a simple web application.

I created a simple Spring Boot project using the Initializr. It had no extra packages added.

I added the spring-boot-starter-web package, which included tomcat. Tomcat had type 'provided'. If I omitted the version, the war did not work when deployed, but worked locally.

I created a simple home controller using the Controller annotation and the GetMapping annotation.

The default view resolver is to the /src/main/resources/static folder without an extension. I created home.html in the static package and the page was found. Static is copied to WEB-INF/classes.

I wanted to deploy the app to an external tomcat installation. I extended the application class from SpringBootServletInitializer to create a war file and identify the starting application.

I added a view resolver and set the view class to JSTL. I had to add a dependency for jstl. I had to add a webapp folder for the JSPs, /src/main/webapp. The path is to the root of the web app. I did not want my JSPs in the classes folder.

Be sure that the path to the JSTL library is http and not https.

These are all the updated files to get it working:

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }             
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
           return application.sources(DemoApplication.class);
        }
        
        @Bean
        public ViewResolver internalResourceViewResolver() {
            InternalResourceViewResolver bean = new InternalResourceViewResolver();
            bean.setViewClass(JstlView.class);
            bean.setPrefix("/WEB-INF/jsp/");
            bean.setSuffix(".jsp");
            return bean;
        }

}

pom.xml


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.0.RELEASE</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.35</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
index.jsp

<%-- 
    Document   : index
    Created on : Jun 8, 2020, 1:01:38 PM
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

    <c:choose>
        <c:when test="${not empty param.name}">
            <h1>Hello ${param.name}</h1>
        </c:when>
        <c:otherwise>
            <h1>Hello Stranger</h1>
        </c:otherwise>
    </c:choose>
    
</body>
</html>



No comments:

Post a Comment

Followers