下面记录了使用了 gradle 作为构建工具的实现
1、实现了基本的 springboot 的 HelloWorld,可以参考:
http://projects.spring.io/spring-boot/
这时候,视图返回的是一个 json。我们需要让 springboot 也支持返回 jsp。
2、加入运行时依赖
runtime ("org.apache.tomcat.embed:tomcat-embed-jasper")
3、加入插件 springboot 的配置
apply plugin: 'spring-boot'
要能应用该插件,还须要配置
buildscript {
repositories {
maven { url "https://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RELEASE")
}
}
对于该配置的解释,可以参考:http://www.tuicool.com/articles/rUfEBrr
配置以后,你就可以在自己的 gradle 面板上看到:
3、在 application.properties 文件中写上
# 配置启动端口号
server.port=9090
# 配置返回视图的前缀和后缀
spring.mvc.view.prefix=/WEB-INF/app/jsp/
spring.mvc.view.suffix=.jsp
4、容器启动类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
启动项目可以使用以下两种方式
1、使用命令行
gradle bootRun
2、或者运行主方法
然后我们在浏览器中输入
http://localhost:9090/hello/book
就可以看到视图和对应的模型返回了。
以下是使用 maven 作为构建工具的做法:
1 先看到这篇文章
spring-boot 添加JSP页面功能 - 记录分享每一点进步和成长 - 博客频道 - youkuaiyun.com
http://blog.youkuaiyun.com/lifuxiangcaohui/article/details/47060231
2 github 官网上 Spring 的例子
spring-boot/spring-boot-samples/spring-boot-sample-web-jsp at v1.2.7.RELEASE · spring-projects/spring-boot · GitHub
https://github.com/spring-projects/spring-boot/tree/v1.2.7.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp
已经初步完成了,就差最后一步,没有实现。
思路:把上面 github 的代码下载下来研究一下就可以了。