您的位置:首页 > 编程语言 > Java开发

SpringBoot快速入门

2019-03-26 23:07 393 查看

 

1.创建一个Maven工程,注意类型为Jar工程项目

2.Pom.xml文件引入依赖

[code]    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!—SpringBoot web 组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

3.编写熟悉的Helloworld

->创建包,com.jxau.test

->创建类,helloworld类

[code]@RestController
@EnableAutoConfiguration
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);
}
}

4.打开浏览器,输入http://localhost:8080/index,可以看到页面输出Hello World

@RestController:

在上加上RestController 表示修饰该Controller所有的方法返回JSON格式,直接可以编写Restful接口

@EnableAutoConfiguration

告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。

@ComponentScan(basePackages = "com.jxau.test")---控制器扫包范围

web开发动静分离:默认格式配置

Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

/static

/public

/resources        

/META-INF/resources

web页面渲染-一般选择模板渲染,jsp的形式也可以但官方说明使用模块引擎

->使用Freemarker模板引擎渲染web视图

->引入pom.xml依赖

[code]<!-- 引入freeMarker的依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

--在src/main/resources/创建一个templates文件夹,后缀为*.ftl

[code]<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
${name}
</body>
</html>

--

[code]@RestController
@EnableAutoConfiguration
public String index(Map<String, Object> map) {
map.put("name","helloworld");
return "index";
}

public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);
}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: