Wednesday, February 7, 2018

Start Your Spring development with IntelliJ IDEA in 3 Minutes


Lots of my students and juniors keep telling me that, setting up a “Spring” development environment is a big hassle. There are many tutorials are available in the web but they are not comfortable with those. May be the tools we are using are slightly different with them or it is just a time consuming for the initial setting up. That is why I choose to write a post them to start a spring project from scratch, hopefully it will help others too. I am using “IntelliJ IDEA” ultimate IDE (as usual) and maven for build automation tool. I will deploy it on JBoss server (similar on Apache Tomcat or Oracle WebLogic).
  
You will find lots of good tutorials on downloading and setting up IDE (Idea) and web-server (JBoss) and the basic knowledge on Spring MVC, so I am omitting those things (if you don’t find it on Google, make a comments on the below comment section).

    Please follow steps by steps –
1)    Go to  New Project -> Maven , don’t select archetype and press Next

2)    Write GroupId, ArtifactId, Version like this.


groupId will identify your project uniquely across all projects, so we need to enforce a naming schema. It has to follow the package name rules, what means that has to be at least as a domain name you control, and you can create as many subgroups as you want. Example-
com.roni

artifactId is the name of the jar without version. If you created it then you can choose whatever name you want with lowercase letters and no strange symbols. If it's a third party jar you have to take the name of the jar as it's distributed. Example-
springtest

version if you distribute it then you can choose any typical version with numbers and dots (1.0, 1.1, 1.0.1, ...). In this example, we will keep the default-
1.0-SNAPSHOT

3)    Press Next then write the Project Name and select the project location, then click Finish  



4)    Open the maven pom.xml file (if not opened yet automatically) and add the following lines inside the project tag.-
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <packaging>war</packaging>

    <properties>
        <spring.version>4.3.0.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>


You can choose enable auto import option or you have to click import changes whenever you have made any change on the pom.xml


5)    Create webapp and WEB-INF directory in the main folder as shown below –


6)    Create web.xml file in WEB-INF and write the following lines-

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Test Application</display-name>
    <description>
        This is a simple spring mvc app
    </description>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>


   

7)    Now create dispatcher-servlet.xml file on WEB-INF directory and add the following lines on it


   
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.roni.springtest.controllers"/>

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




Here base-package is your java package, which we will going to create on the next steps.


8)    Now create the package and a new Controller class SpringTestController in it. Write the following codes –

package com.roni.springtest.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * User: Towhidul Haque Roni
 * Date: 2/7/2018
 * Time: 2:39 PM
 */
@Controller
public class SpringTestController {

    @GetMapping("/")
    public String index(Model m) {
        m.addAttribute("aVariable", "Hello World");
        return "index";
    }
}




9)    Create directory views in WEB-INF and create index.jsp file on it. Write the following lines on it-
<!DOCTYPE html>
<html>
<body>

    <h1>Spring MVC Test</h1>

    <p>${aVariable}</p>

</body>
</html>

10)    Now if you click Run from Run menu for the first time, you will be prompt to Edit Configuration. If you click, following menu will  appear –


11)    Click the top most left + sing, then scroll down and you will see the JBoss Server. Select the local, If you have already configured the JBoss server it will appear in the application server dropdown list, if not then click Configure and it will require to show the JBoss home directory. I have downloaded the jboss-eap-7.1.0 and unzip it and showed the path ( jboss-eap-7.1.0\jboss-eap-7.1 ) as a home directory.

12)    Then go to Deployment , click the right most + sign and add springtest.war artifacts like below  -




13)    Now click Run, hopefully you will be able to see the output like below –


Enjoy!!! I hope that it will save your time. On the upcoming post, I will do REST web service and spring data within 5 minutes setting up time.

2 comments:

@decorators in Python

People have confusion about how to use python decorators in the proper way and how it works. The main reason for it is there are several way...