版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
项目结构
1、首先创建一个maven项目
2、配置pom.xml文件
<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.jmxk</groupId>
<artifactId>SpringBoot-Start</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- SpringBoot自动整合版本号信息 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependencies>
<!-- SpringMVC内部依赖整合 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合Fremarker -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- SpringBoot热部署 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 自动重启 -->
<configuration>
<fork>true</fork>
<!-- 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
</configuration>
</plugin>
</plugins>
</build>
</project>
3、创建SpringBoot启动程序
注意:springBoot必须放在Controller包的顶层
package com.jmxk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBoot_Start {
public static void main(String[] args) {
SpringApplication.run(SpringBoot_Start.class, args);
}
}
4、创建HTTP请求连接类
package com.jmxk.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//@RestController = @Conctroller + @ResponseBody
@RestController
public class HelloController {
@RequestMapping("/helloIndex")
public String HelloIndex() {
return "欢迎使用springBoot2.0!";
}
}
5、启动SpringBoot程序
控制信息出现Spring图标样式并且程序一直在运行没报错则表示启动成功!
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.1.RELEASE)
2019-08-15 11:25:58.306 INFO 15224 --- [ restartedMain] com.jmxk.SpringBoot_Start : Starting SpringBoot_Start on DESKTOP-A9VMB8N with PID 15224 (F:\Eclipse-WorkSpace\JavaEE\SpringBoot_Start\target\classes started by com in F:\Eclipse-WorkSpace\JavaEE\SpringBoot_Start)
2019-08-15 11:25:58.309 INFO 15224 --- [ restartedMain] com.jmxk.SpringBoot_Start : No active profile set, falling back to default profiles: default
2019-08-15 11:25:58.357 INFO 15224 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-08-15 11:25:58.357 INFO 15224 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-08-15 11:25:59.419 INFO 15224 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-08-15 11:25:59.441 INFO 15224 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-08-15 11:25:59.441 INFO 15224 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13
2019-08-15 11:25:59.449 INFO 15224 --- [ restartedMain] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk1.8/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8/bin/../jre/bin;C:/Program Files/Java/jdk1.8/bin/../jre/lib/amd64;C:\Python27\;C:\Python27\Scripts;C:\Python\Python37\Scripts\;C:\Python\Python37\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8\bin;MYSQL_HOME%\bin;D:\Maven\apache-maven-3.6.0\bin;D:\Node\nodejs\;C:\ProgramData\chocolatey\bin;C:\Program Files\TortoiseGit\bin;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\TXsoft\Git\cmd;C:\Users\com\AppData\Local\Microsoft\WindowsApps;D:\Node\nodejs\node_global;;D:\JetBrains\IntelliJ IDEA 2019.1\bin;;D:\JetBrains\WebStorm 2019.1\bin;;C:\WINDOWS\system32;;.]
2019-08-15 11:25:59.522 INFO 15224 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-08-15 11:25:59.523 INFO 15224 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1165 ms
2019-08-15 11:25:59.725 INFO 15224 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-15 11:25:59.806 INFO 15224 --- [ restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2019-08-15 11:25:59.905 INFO 15224 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-08-15 11:25:59.954 INFO 15224 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-08-15 11:25:59.958 INFO 15224 --- [ restartedMain] com.jmxk.SpringBoot_Start : Started SpringBoot_Start in 1.936 seconds (JVM running for 3.838)
6、浏览器访问 http://localhost:8080/helloIndex
springboot不需要输入项目名称,输入格式是本地IP地址+端口号+请求路径
本地IP地址:localhost或者127.0.0.1
端口号默认:8080
请求路径为Controller类中注解@RequestMapping("/helloIndex")自定义的值
7、SpringBoot整合fremarker,在pom.xml添加配置信息(之前的pom.xml中已经有了)
<!-- SpringBoot整合Fremarker -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
8、在resources下创建一个文件夹templates,在文件夹下创建一个ftl的文件
Spring整合FreeMarker!
${map.id}
${map.name}
${map.sex}
${map.age}
9、新建一个Controller类
package com.jmxk.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FTLController {
@RequestMapping("/index")
public String index(Map<String, Object> map,HttpServletRequest request) {
map.put("id", 1);
map.put("name","李白");
map.put("age",20);
map.put("sex","男");
request.setAttribute("map", map);
return "index";
}
}