好了,今天接着说spring-boot,主要讲解的是如何使用spring-boot进行网页端开发。
我们说到网页端开发,首先一定会想到html跳转,那么我们来进行html跳转呢?
我本来以为不需要配置任何文件可以直接跳转html,so,我写了一个html跳转,但是IT is wrong!我查了一些资料,了解到我们需要配合html模板引擎来使用,比如freemarker、thymeleaf。
注:
好多的文章说,这两个模板引擎是可以兼容的,但是我尝试了一下,却没有成功。如果朋友们发现了怎么让这两个模板引擎兼容,请告诉我,谢谢!邮箱:18703642982@163.com
我们直接上pom代码:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
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"
4.0.0
com.example
demo-boot
0.0.1-SNAPSHOT
jar
demo-boot
Demo project for Spring Boot
<!-- 引入父项目 -->
org.springframework.boot
spring-boot-starter-parent
1.4.1.RELEASE
<!-- 参数设置 -->
.
build.
sourceEncoding
UTF-8
.
build.
sourceEncoding
.
reporting.
outputEncoding
UTF-8
.
reporting.
outputEncoding
.
version
1.8
.
version
.
version
5.1.21
.
version
<!-- jar包依赖 -->
<!-- spring-boot web引入,支持 Web 应用开发,包含 Tomcat 和 spring-mvc。 -->
org.springframework.boot
spring-boot-starter-web
<!-- thymeleaf 模板引擎引入 -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> -->
<!-- freemarker -->
org.springframework.boot
spring-boot-starter-freemarker
<!-- redis 引入 -->
org.springframework.boot
spring-boot-starter-redis
<!-- 添加适用于生产环境的功能,如性能指标和监测等功能 -->
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
<!-- spring-boot 配置处理器引入 -->
org.springframework.boot
spring-boot-configuration-processor
<!-- apache tomcat jdbc 引入 -->
<!-- <dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency> -->
<!-- dbcp 连接池引入 -->
commons-dbcp
commons-dbcp
1.4
commons-pool
commons-pool
1.4
<!-- mysql 驱动包引入 -->
mysql
mysql-connector-java
runtime
5.1.21
<!-- <version>${mysql-connector.version}</version> -->
<!-- mybatis 引入 -->
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
maven-compiler-plugin
1.8
1.8
org.springframework.boot
spring-boot-maven-plugin
我们在pom中添加了两个模板引擎,哦,忘记了,应该先看项目结构的,sorry:
there is no explain!不解释,今天有点懒,直接上application配置和java代码
##
server.port: 8082
#server.tomcat.uri-encoding=UTF-8
##
默
认
数
据
源
配
置
spring.datasource.url=jdbc:mysql://119.23.78.228:3306/shares_manager?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##
数
据
库
链
接
池
参
数
配
置
#
指
定
使
用
哪
个
连
接
池
,
默
认
使
用tomcate-jdbc
连
接
池
spring.datasource.type=org.apache.commons.dbcp.BasicDataSource
#
连
接
池
使
用
的
数
据
库
账
号
spring.datasource.dbcp.username=root
#
连
接
池
使
用
的
数
据
库
密
码
spring.datasource.dbcp.password=123456
#
连
接
池
使
用
的
数
据
库
驱
动
类
spring.datasource.dbcp.driver-class-name=com.mysql.jdbc.Driver
#
初
始
化
链
接
数
spring.datasource.dbcp.initial-size=1
#
最
大
链
接
数
spring.datasource.dbcp.max-active=20
#
最
小
空
闲
数
量
spring.datasource.dbcp.min-idle=1
#
最
大
空
闲
数
量
spring.datasource.dbcp.max-idle=20
#
最
大
等
待
时
间 - 60
秒
spring.datasource.dbcp.max-wait=6000
##
为
实
体
对
象
所
在
的
包
,
跟
数
据
库
表
一
一
对
应
#mybatis.typeAliasesPackage=com.example.model
##mapper
文
件
的
位
置
#mybatis.mapperLocations=classpath:com/example/mapper/*.xml
##log
设
置(
两
种
方
式
,1.log
参
数
配
置
,2.logback.xml
配
置)
##freemarker
配
置
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=classpath:/templates/
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates
#spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/tm/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
#spring.thymeleaf.cache=true
##
注
:thymeleaf
和freemarker
不
可
以
共
存
##
自
定
义
参
数
userName=Alex
bookTitle=Spring Boot
入
门
package
com.
example.
controller;
import
java.
util.
Map;
import
org.
apache.
log4j.
Logger;
import
org.
springframework.
stereotype.
Controller;
import
org.
springframework.
web.
bind.
annotation.
RequestMapping;
@
Controller
//这里有个坑就是@RestController,绝对不能用@RestController
@
RequestMapping(
"/tm")
public
class
ThymeleafController {
private
static
Logger
logger
=
Logger.
getLogger(
ThymeleafController.
class);
/**
* 返回html模板.
*/
@
RequestMapping(
"/thymeleaf")
public
String
helloHtml(
Map
<
String,
Object
>
map){
logger.
info(
"hello from TemplateController.helloHtml");
map.
put(
"hello",
"from TemplateController.helloHtml");
return
"/helloHtml";
}
@
RequestMapping
public
String
helloHtml1(
Map
<
String,
Object
>
map){
logger.
info(
"hello from TemplateController.helloHtml");
map.
put(
"hello",
"from TemplateController.helloHtml");
return
"/helloHtml";
}
}
package
com.
example.
controller;
import
java.
util.
Map;
import
org.
springframework.
stereotype.
Controller;
import
org.
springframework.
web.
bind.
annotation.
RequestMapping;
@
Controller
//这里有个坑就是@RestController,绝对不能用@RestController
@
RequestMapping(
"fm")
public
class
FreeMarkerController {
@
RequestMapping(
"freemarker")
public
String
testModel(
Map
<
String,
Object
>
map){
map.
put(
"name",
"[Angel -- 守护天使]");
return
"/fm/welcome";
}
@
RequestMapping
public
String
testModel1(
Map
<
String,
Object
>
map){
map.
put(
"name",
"[Angel -- 守护天使]");
return
"/fm/welcome";
}
}