Home  >  Article  >  Java  >  Share some frequently asked questions about web-mvc projects building SpringBoot on IDEA

Share some frequently asked questions about web-mvc projects building SpringBoot on IDEA

Y2J
Y2JOriginal
2017-04-27 09:42:372806browse

This article mainly introduces a summary of the problems encountered in building a SpringBoot web-mvc project on IDEA. Friends in need can refer to it

I have been studying how to build a web-mvc project on IDEA these days. mvc's SpringBoot project, I followed online tutorials to build it step by step, but there were still a lot of problems.

In order to save everyone from taking some detours in the future, I am here to share the results of my research over the past few days, and I hope it can be helpful to everyone.

Here we first introduce the configuration information of various environments: idea2016.2.1 jdk1.8.0_31

Because SpringBoot has built-in tomcat, there is no need for additional tomcat configuration. Now Let’s start talking about how to build the SpringBoot web-mvc project on idea

Step 1:Create a new regular maven project in IDEA. Please see the following diagram for the specific steps:


##Go through the steps above , a basic maven project has been built, and the next step is to start building various configuration file information in SpringBoot.

Step 2:

1. First copy the following code to pox.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <groupId>com.example</groupId> 
  <artifactId>demo</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <packagingexample>jar</packagingexample> 
  <name>demo</name> 
  <description>Demo project for Spring Boot</description> 
  <parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.0.RELEASE</version> 
    <relativePath/> <!-- lookup parent from repository --> 
  </parent> 
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    <java.version>1.8</java.version> 
  </properties> 
  <dependencies> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
  </dependencies> 
  <build> 
    <plugins> 
      <plugin> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
    </plugins> 
  </build> 
</project>

2. Click the jar package dependency update button in maven , please see the following illustration for specific operations:

3. Configure the Web resource files under resources. Here I will configure two files, one is used to store static folders static files, and another resource folder templates used to store HTML.

What is particularly important here is that static files generally store static resource files such as css, js, and images, while template files generally store various HTML files. Moreover, these two files exist by default, and the paths can be referenced directly without special configuration.

application.properties is a configuration file, in which SpringBoot related information can be configured. What everyone needs to pay attention to is that the file name must not be written incorrectly or placed in the wrong location, otherwise it will not take effect.

Look at the illustrated case and code case below:

Code information of csstest.css: Code information of

body { 
  padding: 0px; 
  margin: auto; 
  font-family: "黑体", "仿宋", Arial, "Arial Unicode MS", System; 
  background-color: #00F; 
  font-size: 20px; 
  text-align: left; 
}

welcome.html :

<html> 
<head> 
  <title>Title</title> 
</head> 
<link href="css/csstest.css" rel="external nofollow" rel="stylesheet"/> 
<body> 
  <p>welcome page is login.........</p> 
</body> 
</html>

Code information of the application.properties configuration file:

#修改tomcat的默认的端口号,将8080改为8888 
server.port=8888

4. Write the controller and project startup entry for Web-Mvc in SpringBoot:

DemoApplication.Java specific code:

package example; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
@SpringBootApplication 
public class DemoApplication { 
  public static void main(String[] args) { 
    SpringApplication.run(DemoApplication.class, args); 
  } 
}

HelloController.java specific code:

package example; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
import java.util.HashMap; 
import java.util.Map; 
@Controller 
public class HelloController { 
  @RequestMapping("/index") 
  public String index(){ 
    return "welcome"; 
  } 
}

In this way, the SpringBoot Web-mvc project has been successfully built. The specific steps are as follows .

One more important thing to note is: because I have modified the port number, the address must be written as 127.0.0.1:8888/index when accessing.

The above is the detailed content of Share some frequently asked questions about web-mvc projects building SpringBoot on IDEA. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn