Simple Web application in Java Spring

Spring is the most popular Java framework. You can build everything using this platform. If you learn Java you need to know this framework. In this post, we will build the simple Web application in Java Spring. There is no better way to learn like practice and that’s why after reading this article you should code this app on your own.

In this post:
  • you will learn about Spring Framework
  • read about Spring Boot
  • create a simple web application

What is the Spring Framework?

This is the framework that helps you build an application in Java. You don’t need to write a lot of code by yourself because of this platform provide the complete classes that you can use e.g. to get data from a database, generate HTML view and more.
Spring Framework is made with core libraries that provide the main structure of the application. When you want to use e.g. MongoDB you need to add libraries that would help you using this database. In the project structure, these libraries are named dependencies.

Spring Boot

If you don’t know how to start with Spring or you want to start building your app as quickly as possible. You need to check out Spring Boot. Spring Boot helps you by generating common app structure and add dependencies that you would want use.  At https://start.spring.io/ you can generate your project. Add project metadata, search and add dependency you need. In this example, we will use “Web”, “DevTools”, “Thymleaf”. Also, you can pick what build do you want to use: Gradle or Maven (personally I like Gradle but you can choose what you want). At the end click on Generate Project button. The browser would start to download the zip with a basic project structure. Now, you only need to open the project in your favorite IDE and start coding.
Unzip your project and open it in your IDE. I prefer IntelliJ IDEA. IDE syncing the project and downloading external libraries (dependencies).  Go to src/main/java/[Your Package] and you will see one class that is named [Your Project Name]Application. This is the main class that you will use to run the application.
package com.mateuszzbylut.webdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebdemoApplication {

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

As you can see, there is annotation named SpringBootApplication. This annotation tells the framework that this is the main class. Spring will look here for components and configuration and so on.  In the main() method we use SpringApplication.run() to launch the app.

HelloController

package com.mateuszzbylut.webdemo;

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

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String sayHello(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "hello";
    }
}

Controller triggers sayHello() method when you open the app with the path “/hello”. This method requests a param named “name”, the path will look like this “/hello?name=Joe”. If you don’t put parm, the method will use the default value “World”.  In the method we adding the name as the attribute to our model. In the end, we return the name of our template file (without extension).

Template

In src/main/resources/templates make a HTML file named hello.html. Create a basic HTML template. Attention! It is important to add xmlns:th=”http://www.thymeleaf.org” to HTML tag. As you can see, in the body we add the h1 tag with text and our variable.
Now it’s time to run our application and see the effects in the browser.  To run app right click on the main class and pick Run. In the console, you will see information from Spring what is doing now. So open the browser and type http://localhost:8080/hello to see your app running on your local environment. Add the name parameter to see if the app works properly.
Finally, you build your first web application in Java Spring. Great work! This is a very simple app but that is a good example to start. Rome wasn’t built in one day! Hope you have fun and learn something about the Spring Framework.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.