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

Spring Boot - 开发Web应用

2017-01-23 00:00 204 查看
原文

静态资源访问

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

/static

/public

/resources

/META-INF/resources

渲染 web界面

spring boot 对以下模板引擎提供友好配置

Thymeleaf

FreeMarker

Velocity

Groovy

Mustache

当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resource/templates。这个路径可在后续各模板引擎的配置属性中修改。

配置 pom.xml

<!-- freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

编写 Controller映射

@RequestMapping("/")
public String index(ModelMap map) {
map.addAttribute("host", "http://blog.didispace.com");
return "index";
}

编写模板页面( resources/template/index.ftl)

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
FreeMarker模板引擎
<h1>${host}</h1>
</body>
</html>

application.properties

## h2数据库本地化
spring.datasource.url = jdbc:h2:file:E\:\\test\\db\\testdb
spring.datasource.username = sa
spring.datasource.password = sa
spring.datasource.driverClassName = org.h2.Driver

### jpa 配置 ###
spring.jpa.hibernate.ddl-auto: update
# hibernate的命令策略,方便导入"_"命名的表
spring.jpa.hibernate.naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.database: H2
spring.jpa.show-sql: true

## Freemarker 配置
## 文件配置路径
#spring.velocity.resource-loader-path=classpath:/templates/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
# 视图名后缀
spring.freemarker.suffix=.ftl

server.port=8123
debug=true
logging.file=E:/logs/log.out

配置热部署插件

第一种方式:springloaded (官方文档)

补全 pom文件

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 会在程序运行时下载此依赖包,暂时报错 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
<!-- 加上这个会自动给你打包-->
<!--<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>-->
</plugin>
</plugins>
</build>

第二种方式:spring-boot-devtools (官方文档)

pom文件

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

1 如果使用 maven启动,必须配置 maven插件的 fork选项:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

2 指定让 devtools 监听指定文件夹,那么可以在 application.yml 配置spring.devtools.restart.additional-paths=your path,注意这里需要改成 yml 文件的格式。如果需要排除一些资源,可以使用:

spring.devtools.restart.exclude=static/**,public/**

3 spring-boot 包含一个内嵌的服务器,用于资源改变时触发浏览器刷新。可以设置spring.devtools.livereload.enabled property to false关闭它。

4. 配置好,你可以 Ctrl + S 保存,或者 Ctrl + shift + F9(Recompile),以便触发更新。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: