Record a spring boot experience
- springboot project creation
Here's a way to share it with other bloggers https://blog.csdn.net/mousede/article/details/81285693
- directory structure
- Next, write a startup class
Startup class: SpringBootDemoApplication
package com.start; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication(scanBasePackages = "com") @MapperScan({"com.start.mangage.repository","com.start.mangage.service"}) public class SpringBootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class,args); } }
Create a new configuration file named application.properties
#Server port number
server.port=8011
Here, I use maven to build projects, so the following dependencies need to be added to pom.xml. You don't need to put them in
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>1.2</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <build> <plugins> <!-- Resource file copy plug-in --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
A basic project is set up here. You can visit the default homepage by visiting http://localhost:8011 with a browser
The following is the most difficult part in my first construction process. Baidu took a lot of time to solve it
- How to access static resources
First, you need to create folders static (store js, css, etc.) and templates (store jsp, html, etc.) in the directory
Add dependency in pomxml
<!-- Accessing static resources --> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency --> <!-- jsp rely on --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jsp-api</artifactId> </dependency> <resources> <resource> <directory>src/main/java</directory>java Path to file <includes> <include>**/*.properties</include> <include>**/*.*</include> </includes> <!-- <filtering>false</filtering> --> </resource> <resource> <directory>src/main/resources</directory>Path to resource file <includes> <include>**/*.*</include> </includes> </resource> </resources>
At first, I used thmeleaf to rely on it, which made me unable to access my jsp format page. Later, I used jsp dependency instead, which made me a beginner. Alas!
Add in profile
#Configure the location and suffix of the jsp view spring.mvc.static-path-pattern=/** spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
Then I built a controller class, tested the access page, and successfully accessed it.
Hard work can make up for weakness!