通过Spring Boot构建项目工程

来源:互联网 发布:linux中vi常用命令 编辑:程序博客网 时间:2024/06/04 18:55

通过Spring Boot构建项目工程
1.1Spring项目初始化
https://start.spring.io/

官方文档
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/
http://docs.spring.io/spring-boot/docs/current/reference/html/index.html
例子 https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

本课用到工具是:idea,maven,关于maven的配置可以百度。pom.xml如果出问题一般都是仓库的jar没下载完整,我们直接把原来的删除了,让它重新去更新依赖。
1.2导入Spring Booot工程
application类是整个项目的入口。

2.1最简单的服务器(代码演示)

   @Controller    public class IndexController {        @RequestMapping("/")        @ResponseBody        public String index() {            return "Hello NowCoder";        }    }
    @RequestMapping(value = "/profile/{groupId}/{userId}")    @ResponseBody    public String profile(@PathVariable("groupId") String groupId,                          @PathVariable("userId") int userId,                          @RequestParam(value = "type", defaultValue = "1") int type,                          @RequestParam(value = "key", defaultValue = "nowcoder") String key) {        return String.format("{%s},{%d},{%d},{%s}", groupId, userId, type, key);    }

3.HTTP Method(代码演示)
GET
获取接口信息
HEAD
紧急查看接口HTTP的头
POST
提交数据到服务器
PUT
支持幂等性的POST
DELETE
删除服务器上的资源
OPITIONS
查看支持的方法
一般网站只用Get和Post,代表获取和更新,html的form仅支持Get和Post

4.静态和模板文件(代码演示)
静态
默认目录:static/templates
文件:css/js/images

模板文件
默认目录:templates
文件:xxx.vm

5.Velocity模板语法(类似Java语法)

$!{ 变量/表达式 }## 注释 ## #* 多行注释 *#for#foreach ($color in $colors)Color$!{foreach.count}/${foreach.index}:$!{color}#endVelocity官方APIhttps://velocity.apache.org/engine/devel/user-guide.html5.Velocity语法(代码演示)macro#macro (render_color, $color, $index)This is Color $index:$color#end#foreach ($color in $colors)#render_color($color, $foreach.index)#end属性访问$!{user.name}$!{user.getName()}模板继承include 纯文本扩展parse 变量解析

6.request/response(代码演示)
request
参数解析
cookie读取
http请求字段
文件上传

response
页面内容返回
cookie下发
http字段设置,headers

HttpServletResponse
response.addCookie(new Cookie(key, value));
response.addHeader(key, value);

HttpServletRequest
request.getHeaderNames();
request.getMethod()
request.getPathInfo()
request.getQueryString()

7.重定向/Error(代码演示)
重定向
301:永久转移
302:临时转移

Error
// Spring MVC外的Exception或Spring MVC没有处理的Exception
@ExceptionHandler()
@ResponseBody

public String error(Exception e) {    return "ERROR:" + e.getMessage();}

8.IoC(代码演示)

9.AOP/Log(代码演示)
AOP
面向切面,所有业务都要处理的业务

“`
private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

@Before("execution(* com.nowcoder.controller.IndexController.*(..))")public void beforeMethod(JoinPoint joinPoint) {    logger.info("before" + joinPoint.toLongString());}

10课后作业
a) SpringBoot项目创建
b) Velocity模板语言练习
c) HTTP协议复习
d) Controller练习
e) AOP练习
f) IoC练习

0 0
原创粉丝点击