项目描述
上传时间
浏览人数
1.下载spring,解压,把项目拖进去
2.右键->File->settings->输入Maven 修改然后启动
3.点击File -> New Project
4.修改pom.xml文件;
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--热加载--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency>
5.新建测试类
@Controller public class UserController { @RequestMapping("welcome")//添加函数的映射路径 @ResponseBody/*把函数的返回值作为相应的内容,访问地址*/ public String welcome(){ return "Hello,World!"; }
6.运行成功的控制台
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.2.RELEASE)
2021-07-06 15:41:18.593 INFO 10552 --- [ restartedMain] com.czsay.onepiece.OnepieceApplication : Starting OnepieceApplication on class04 with PID 10552 (D:\LIN202107\onepiece\target\classes started by Administrator in D:\LIN202107\onepiece)
2021-07-06 15:41:18.593 INFO 10552 --- [ restartedMain] com.czsay.onepiece.OnepieceApplication : No active profile set, falling back to default profiles: default
2021-07-06 15:41:18.793 INFO 10552 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-07-06 15:41:18.794 INFO 10552 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-07-06 15:41:18.794 INFO 10552 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.14]
2021-07-06 15:41:18.815 INFO 10552 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-07-06 15:41:18.815 INFO 10552 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 221 ms
2021-07-06 15:41:18.857 INFO 10552 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-07-06 15:41:18.876 WARN 10552 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2021-07-06 15:41:18.892 INFO 10552 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-07-06 15:41:18.905 INFO 10552 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-06 15:41:18.906 INFO 10552 --- [ restartedMain] com.czsay.onepiece.OnepieceApplication : Started OnepieceApplication in 0.333 seconds (JVM running for 121.502)
2021-07-06 15:41:18.907 INFO 10552 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
2021-07-06 15:41:30.302 INFO 10552 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-06 15:41:30.302 INFO 10552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-07-06 15:41:30.306 INFO 10552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
7.在浏览器上输入 http://localhost:8080/welcome
页面会显示 Hello,World!
=================================================
简单解释一下相关
1. spring-boot-starter-parent:
springboot官方推荐的maven管理工具,最简单的做法就是继承它。 spring-boot-starter-parent包含了以下信息:
1)、缺省使用java6编译, 如果需要改为java 1.7,则在pom中加上java.version属性即可
2)、使用utf-8编码
3)、实现了通用的测试框架 (JUnit, Hamcrest, Mockito).
4)、智能资源过滤
5)、智能的插件配置(exec plugin, surefire, Git commit ID, shade).
2. spring-boot-starter-web
springboot内嵌的WEB容器, 缺省会使用tomcat
3. @Controller
Java中的注解@Controller会启动一个Spring MVC的控制器,
4. @EnableAutoConfiguration
@EnableAutoConfiguration也是必须的,它用于开启自动配置
5. @RequestMapping
示例中该注解将”/"映射到处理Hello world 输出的home()方法
6. @ResponseBody
示例中该注解将home()的输出字符串“Hello world”直接作为http request的响应内容,而不是Spring MVC中常用的视图名字