- Building Web Apps with Spring 5 and Angular
- Ajitesh Shukla
- 612字
- 2021-07-02 19:38:19
Building Hello World web app using Spring Boot
In this section, we will go through the steps required to build a Hello World Web App using Spring Boot. The following given steps assume that you have set up Spring STS within your Eclipse IDE by following the steps given earlier. Now, with the steps given next, one can set up the Hello World web app in an easy manner:
- Press Ctrl + N to open up the Project creation Wizard dialog box.
- Write Spring in the Wizards text field. This will show various project options related to Spring projects. Select the option Spring Starter Project, and click on Next.
- Name the project HelloWorld, or leave the default name demo, and click on Next:

- Select Web in the list of dependencies as shown in the following screenshot, and click on Finish:

- Clicking on Finish will create a HelloWorld project whose file structure will look as seen in the following screenshot. Pay attention to the annotation @SpringBootApplication in the HelloWorldApplication class shown in the screenshot. Spring applications are commonly found to be annotated with annotations such as @Configuration, @EnableAutoConfiguration, and @ComponentScan. Spring Boot came up with @SpringBootApplication as an alternative to using three annotations together:

- Right-click on the project, and start Spring Boot App, as shown in the following screenshot. It will start the app on the embedded Tomcat server. Access the URL http://localhost:8080 on your browser. It will show up a page with the heading as Whitelabel Error Page followed by some content.

- At times, the project may not run successfully due to one or more errors encountered during setting up the project. One can open the Problems View, and find out if there are any issues. As creating and running a Maven project requires the dependencies to be downloaded from the internet, it is to be ensured that the internet connection is active while setting up and running the project.
- It is time to write some custom code in the HelloWorldApplication Java file, and run the app. Place the following code (in bold) as shown and run the app. Access the URL http://localhost:8080, and you will find the following getting printed: Hello World. How are you?. Pay attention to the usage of the @Controller annotation, which is required to annotate a class as a controller. In case one wants the class to serve REST APIs, one can use either @Controller and @ResponseBody together appropriately at the class and method level, or use @RestController at the class level:
@Controller
@SpringBootApplication
public class HelloWorldApplication {
@RequestMapping("/")
String home() {
return "Hello world. How are you?";
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
- When wanting to use the JSP files for displaying views, the following steps needed to be taken:
- Include the following code in the pom.xml file. This is done to enable JSP support:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
- The following needs to be put in src/main/resources/application.properties in order to define the template prefix and suffix for the JSP files:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
- Create the JSP files in the folder src/main/resources/META-INF/resources/WEB-INF/jsp/. For the sake of this current example, let's call the file as index.jsp.
- The code for @Controller would look like the following to render the index.jsp file. Note the absence of the @ResponseBody annotation on the home() method. This is why the JSP file, index.jsp, is rendered. Otherwise, a string object would have been returned as a response leading to errors while rendering.
@Controller
@SpringBootApplication
public class HelloWorldApplication {
@RequestMapping("/")
String home() {
return "index";
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
推薦閱讀
- 極簡算法史:從數(shù)學(xué)到機(jī)器的故事
- Facebook Application Development with Graph API Cookbook
- SQL Server 2016從入門到精通(視頻教學(xué)超值版)
- Python 深度學(xué)習(xí)
- 青少年軟件編程基礎(chǔ)與實(shí)戰(zhàn)(圖形化編程三級)
- Cassandra Data Modeling and Analysis
- 教孩子學(xué)編程:C++入門圖解
- 精通Python設(shè)計(jì)模式(第2版)
- Python機(jī)器學(xué)習(xí)算法: 原理、實(shí)現(xiàn)與案例
- Linux Shell核心編程指南
- Vue.js應(yīng)用測試
- 創(chuàng)意UI:Photoshop玩轉(zhuǎn)APP設(shè)計(jì)
- Python 3 Object:oriented Programming(Second Edition)
- Java 9 with JShell
- 大規(guī)模語言模型開發(fā)基礎(chǔ)與實(shí)踐