摘要:大家自己了解一下的使用方法,我这里就不进行详细的讲述了。启动方式两种方式都可以主函数启动或者验证访问页面,验证是否输出了当前时间。为了提高大家学习效果,录制了同步的视频课程,还望大家支持视频课程
Spring Boot - 初识 Hello World 索引
Spring Boot - 初识 Hello World
Spring Boot - Servlet、过滤器、监听器、拦截器
Spring Boot - 静态资源处理、启动加载、日志处理
Spring Boot - 数据库配置
Spring Boot - 部署Deploy
准备Jdk8
Ide intelliJ IDEA 或者 eclipse
Maven 3
返回Json格式数据 修改pom依赖</>复制代码
4.0.0
com.wanye
com.wanye.springboot
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
org.springframework.boot
spring-boot-starter-web
创建启动类
</>复制代码
package com.wanye;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by wanye on 2017/5/20.
*/
@SpringBootApplication
public class Start {
public static void main(String[] args) {
SpringApplication.run(Start.class, args);
}
}
创建Controller
</>复制代码
package com.wanye.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* Created by wanye on 2017/5/20.
*/
@RestController // @Controller + @ResponseBody
public class HelloController {
@RequestMapping("/hello")
public Map hello(){
Map hello = new HashMap();
hello.put("data", "hello 小红");
hello.put("status", "SUCCESS");
return hello;
}
}
启动方式
验证</>复制代码
通过main()方法来启动
访问http://localhost:8080/hello 我们可以看到页面返回了数据,并且自动转换成JSON格式,接下来我们讲解刚刚用到的注解
整合JSP/FreeMarker在整合JSP/FreeMarker之前,我们先了解一下spring boot对于controller的支持
模版引擎:spring boot支持FreeMarker 、Groovy 、Thymeleaf (Spring 官⽹网使⽤用这个)、Velocity 、JSP
接收参数:@RequestBody、@RequestParam、@ModelAttribute、JSONObject、HttpEntity 等
通过JSP模板引擎渲染</>复制代码
4.0.0
com.wanye
com.wanye.springboot
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
org.springframework.boot
spring-boot-starter-web
org.apache.tomcat.embed
tomcat-embed-jasper
provided
javax.servlet
jstl
增加⽬目录“src/main/webapp/WEB-INF/jsp/”,将jsp⽂文件放⼊入这个⽬目录中,示例home.jsp代码(只用来验证是否访问到该文件)
</>复制代码
jsp
hello jsp
在⽬目录“resources”中,增加application.properties配置⽂文件
</>复制代码
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
创建JSPController
</>复制代码
package com.wanye.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by wanye on 2017/5/20.
*/
@Controller
public class JSPController {
@RequestMapping("/jsp/home")
public String home() {
return "home";
}
}
</>复制代码
#必须⽤用sping-boot:run启动
mvn clean spring-boot:run
访问http://localhost:8080/jsp/home 页面返回”hello jsp”,说明整合JSP成功,该请求能够访问到home.jsp这个文件
通过FreeMarker模板引擎渲染删除刚刚jsp的pom配置,并修改spring boot 启动依赖的jar
</>复制代码
4.0.0
com.wanye
com.wanye.springboot
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
org.springframework.boot
spring-boot-starter-freemarker
在resources下创建templates文件夹,将.ftl文件放⼊
application.properties文件中无需配置(删除jsp配置)
home.ftl文件
</>复制代码
hello freemarker. ${time?string("yyyy-MM-dd hh:mm:ss")}
创建FreemarkerController
</>复制代码
package com.wanye.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
/**
* Created by wanye on 2017/5/20.
*/
@Controller
public class FreemarkerController {
@RequestMapping("/ftl/home1")
public String home1(Model model) {
model.addAttribute("time", new Date(System.currentTimeMillis()));
return "home";
}
@RequestMapping("/ftl/home2")
public ModelAndView home2() {
ModelAndView res = new ModelAndView("home");
res.addObject("time", new Date(System.currentTimeMillis()));
return res;
}
}
这里通过两种方式,向页面传递参数“time”。大家自己了解一下Model, ModelAndView的使用方法,我这里就不进行详细的讲述了。
</>复制代码
两种方式都可以:主函数main()启动或者spring-boot:run
访问http://localhost:8080/ftl/home1 页面,验证是否输出了当前时间。关于FreeMarker语法,大家自己了解一下,不是本文关注的重点
总结本文讲述了(json,jsp,freemarker)配置及整合方法,并针对web开发常用的注解的概念及功能进行了介绍,留下了一个疑问:为什么整合jsp后必须通过spring-boot:run方式启动?欢迎大家留言讨论。
注解含义
@SpringBootApplication 等价于 @Configuration + @ComponentScan + @EnableAutoConfiguration
@Configuration (可以理解为spring的xml配置) +
@ComponentScan (进行组件扫描,如果扫描到有@Component、@Controller、@Service等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类) +
@EnableAutoConfiguration (开启自动配置,这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring,建议标记在启动类上)
@RestController 等价于 @Controller + @ResponseBody 返回json数据格式(springboot默认使用jackson组件进行转换)
@RequestMapping 提供路由信息,注册访问路径
最后若本文对你有帮助,望点赞。为了提高大家学习效果,录制了同步的视频课程,还望大家支持视频课程
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71140.html
摘要:为什么整合后必须通过方式启动背景在整合这篇文章中,我们用了两种启动方式方法启动测试发现,通过启动能够正常渲染页面,而通过方法启动无法渲染,本文分析下原因。通过来启动对应的服务器。 为什么整合jsp后必须通过spring-boot:run方式启动? 背景 在Spring Boot - 整合Jsp/FreeMarker这篇文章中,我们用了两种启动方式 mvn clean spring-b...
摘要:过滤器监听器拦截器上一篇,我们讲解了配置及整合方法,不清楚的可以点击了解的两种实现方式通过手动注入实现一个返回的方法,并将该对象注册到中。 Spring Boot - Servlet、过滤器、监听器、拦截器 上一篇,我们讲解了spring boot(json,jsp,freemarker)配置及整合方法,不清楚的可以点击了解 Servlet的两种实现方式 通过@Bean手动注入实现一个...
摘要:是一个基于的框架。控制器将视图响应给用户通过视图展示给用户要的数据或处理结果。有了减少了其它组件之间的耦合度。 相关阅读: 本文档和项目代码地址:https://github.com/zhisheng17/springmvc 转载请注明出处和保留以上文字! 了解 Spring: Spring 官网:http://spring.io/ 一个好的东西一般都会有一个好的文档解释说明,如果你...
摘要:框架搭建首先下载相应的包,对于包有两种方式使用创建依赖从而导入所需的包。总结主要进行页面的请求接受与响应。组件包括前端控制器,处理器映射器,处理器适配器,视图解析器,处理器,视图。 我之前的文章介绍了如何搭建SSH框架以及如何利用这一框架来进行web应用开发,最近我又接触了SSM框架即Spring+SpringMVC+Mybatis三大框架的整合,而且目前该框架就SSH框架而言使用的较...
阅读 2338·2021-10-09 09:41
阅读 3518·2021-09-13 10:34
阅读 2010·2019-08-30 12:59
阅读 633·2019-08-29 17:27
阅读 1143·2019-08-29 16:07
阅读 3042·2019-08-29 13:15
阅读 1394·2019-08-29 13:14
阅读 1656·2019-08-26 12:18