2021年7月15日星期四

社交网站后端项目开发日记(一)

本项目目标是开发一个社区网站,拥有发帖、讨论、搜索、登录等一个正常社区拥有的功能。涉及到的版本参数为:

  • JDK1.8
  • Maven3.8.1(直接集成到IDEA)
  • Springboot 2.5.1
  • tomcat

参考网站(在使用框架过程中可能会看的开发文档):

https://mvnrepository.com/ 查找maven依赖

https://mybatis.org/mybatis-3/zh/index.html mybatis的官方文档,配置等都有说明

项目代码已发布到github https://github.com/GaoYuan-1/web-project

关于数据库文件,该篇博客中已有提到,可去文中github获取数据 MySQL基础篇(一)

本文第一篇只介绍了基础,在(二)中将会介绍如何实现登录界面等后续内容。最终将会把完整项目经历发布出来。

本系列主要介绍的是实战内容,对于理论知识介绍较少,适合有一定基础的人。

首先创建一个项目,可利用Spring Initializr

本人配置如下:

image-20210621235357240

maven项目的 pom. 中初始依赖如下,后面会增加更多依赖。

<dependencies> <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-devtools</artifactId>  <scope>runtime</scope>  <optional>true</optional> </dependency> <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-test</artifactId>  <scope>test</scope> </dependency></dependencies>

1. Spring相关知识

记录一些基本概念,Spring的知识可能说的比较少,本篇关注于如何对网站进行实现。

Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象。

Spring 容器会自动完成@bean对象的实例化。

创建应用对象之间的协作关系的行为称为:装配(wiring),这就是依赖注入的本质。

在使用 Spring Initializr 之后,第一个接触的注解为:

@SpringBootApplication

我们可以把 @SpringBootApplication 看作是 @Configuration@EnableAutoConfiguration@ComponentScan 注解的集合。

根据 SpringBoot 官网,这三个注解的作用分别是:

  • @EnableAutoConfiguration :启用 SpringBoot 的自动配置机制
  • @ComponentScan : 扫描被@Component (@Service,@Controller)注解的 bean,注解默认会扫描该类所在的包下所有的类。
  • @Configuration :允许在 Spring 上下文中注册额外的 bean 或导入其他配置类(表示该类是配置类)

Spring通过识别一些注解可以让容器自动装填bean:

  • @Component :通用的注解,可标注任意类为 Spring 组件。如果一个 Bean 不知道属于哪个层,可以使用@Component 注解标注。
  • @Repository : 对应持久层即 Dao 层,主要用于数据库相关操作。
  • @Service : 对应服务层,主要涉及一些复杂的逻辑,需要用到 Dao (Data Access Object)层。
  • @Controller : 对应 Spring MVC 控制层,主要用户接受用户请求并调用 Service 层返回数据给前端页面。

关于@Scope,负责声明Bean的作用域:

@Scope("singleton") //唯一 bean 实例,Spring 中的 bean 默认都是单例的。@Scope("prototype") //每次请求都会创建一个新的 bean 实例。@Scope("request") //每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP request 内有效。@Scope("session") //每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP session 内有效。

关于@Autowired:

自动导入对象到类中,被注入进的类同样要被 Spring 容器管理。从构造器,到方法,到参数、属性、注解,都可以加上@Autowired注解。

2. SpringMVC相关

可以先了解一些HTTP流的相关知识:(参考自MDN Web Docs)

当客户端想要和服务端进行信息交互时(服务端是指最终服务器,或者是一个中间代理),过程表现为下面几步:

  1. 打开一个TCP连接:TCP连接被用来发送一条或多条请求,以及接受响应消息。客户端可能打开一条新的连接,或重用一个已经存在的连接,或者也可能开几个新的TCP连接连向服务端。

  2. 发送一个HTTP报文:HTTP报文(在HTTP/2之前)是语义可读的。在HTTP/2中,这些简单的消息被封装在了帧中,这使得报文不能被直接读取,但是原理仍是相同的。

    GET / HTTP/1.1Host: developer.mozilla.orgAccept-Language: fr
  3. 读取服务端返回的报文信息:

    HTTP/1.1 200 OKDate: Sat, 09 Oct 2010 14:28:02 GMTServer: ApacheLast-Modified: Tue, 01 Dec 2009 20:18:22 GMTETag: "51142bc1-7449-479b075b2891b"Accept-Ranges: bytesContent-Length: 29769Content-Type: text/html<!DOCTYPE html... (here comes the 29769 bytes of the requested web page)
  4. 关闭连接或者为后续请求重用连接。

当HTTP流水线启动时,后续请求都可以不用等待第一个请求的成功响应就被发送。然而HTTP流水线已被证明很难在现有的网络中实现,因为现有网络中有很多老旧的软件与现代版本的软件共存。因此,HTTP流水线已被在有多请求下表现得更稳健的HTTP/2的帧所取代。

HTTP请求的例子:

image-20210628144125974

响应的例子:

image-20210628144336876

2.1 SpringMVC概要

三层架构:表现层,业务层和数据访问层。

MVC:Model(模型层),View(视图层),Controller(控制层)

image-20210628145510740

核心组件: DispatcherServlet

2.2 Thymeleaf思想

如果想给浏览器返回一个动态网页,则需要一个工具支持,例如:Thymeleaf(模板引擎,生成动态的HTML)。

image-20210628170701838

该引擎需要学习的有:标准表达式,判断与循环,模板的布局。

学习建议参考官方文档 https://www.thymeleaf.org/index.html

2.3 SpringMVC代码示例

GET请求

@RequestMapping支持Servlet的request和response作为参数,以下为一个简单示例:

@Controller@RequestMapping("/alpha")public class AlphaController { @RequestMapping("/hello") @ResponseBody public String sayHello(){  return "Hello Spring Boot."; } @RequestMapping("/http") public void http(HttpServletRequest request, HttpServletResponse response) throws IOException {  //获取请求数据  System.out.println(request.getMethod());  System.out.println(request.getServletPath());//请求路径  Enumeration<String> enumeration = request.getHeaderNames();//得到请求行的key  while(enumeration.hasMoreElements()) {   String name = enumeration.nextElement(); //当前值(key)   String value = request.getHeader(name);//得到value   System.out.println(name + ":" + value);  }  System.out.println(request.getParameter("code"));  // 返回响应数据  response.setContentType("text/html;charset=utf-8");//返回网页类型的文本  PrintWriter writer = response.getWriter();  writer.write("<h1>牛客网</h1>");//这里只进行简单输出  writer.close(); }}

在项目Controller层加入代码,以response体返回一个html的文本。

image-20210713235642108

这是通过底层对象处理请求的方式,便于理解。

更简单的方式为:

// GET请求,用于获取某些数据 // /students?current=1&limit=20 假设查询学生数据,第一页,每页20条 @RequestMapping(path = "/students", method = RequestMethod.GET) @ResponseBody// public String getStudents(int current,int limit) { //直接使用Int类型,前端控制器会自动识别匹配//  System.out.println(current);//  System.out.println(limit);//  return "some students";// }// 也可加上注解 public String getStudents(   @RequestParam(name = "current", required = false, defaultValue = "1") int current,   @RequestParam(name = "limit", required = false, defaultValue = "1") int limit) {  System.out.println(current);  System.out.println(limit);  return "some students"; }

利用@ResponseBody注解,实现效果为:

image-20210714001035908

控制台返回结果:

image-20210714000959995

利用这种方式,不论是返回数据还是获取数据都很方便。

另外,通过路径方式传参查询的话:(注意,路径长度并不是无限的,并不可以传很多参数)

// /student/123 查询某个学生,直接放在路径中@RequestMapping(path = "/student/{id}", method = RequestMethod.GET)@ResponseBodypublic String getStudent(@PathVariable("id") int id) { System.out.println(id); return "a student";}

注意以上传参时使用的两个注解。

POST请求

如何向浏览器提交数据?

在项目静态文件中添加html文件,action指示路径

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>增加学生</title></head><body> <form method="post" action="/community/alpha/student">  <p>   姓名:<input type="text" name="name">  </p>  <p>   年龄:<input type="text" name="age">  </p>  <p>   <input type="submit" value="保存">  </p> </form></body></html>
//POST请求@RequestMapping(path = "/student", method = RequestMethod.POST)@ResponseBodypublic String saveStudent(String name, int age) { System.out.println(name); System.out.println(age); return "success";}

在Controller中代码如下,展示效果:

image-20210714003253800

控制台:

image-20210714003310693

响应HTML数据

从程序内部响应到HTML

//响应HTML数据@RequestMapping(path = "/teacher", method = RequestMethod.GET)public ModelAndView getTeacher() { //这个对象就是向前端控制器返回的moder和view ModelAndView mav = new ModelAndView(); mav.addObject("name", "张三"); mav.addObject("age", 30); mav.setViewName("/demo/view");//这个view实际上指的是view.html return mav;}

Controller代码

此外,还需要一个动态网页,利用thymeleaf模板引擎(resource/templates)。

<!DOCTYPE html><html lang="en" 

实现效果,程序已经设定初始值。

image-20210714004731050

模板引擎会自动进行处理。

image-20210714004749881

另一种方式:

@RequestMapping(path = "/school", method = RequestMethod.GET)public String getSchool(Model model) { //前端控制器会创建一个model model.addAttribute("name","大学"); model.addAttribute("ag......

原文转载:http://www.shaoqun.com/a/878490.html

跨境电商:https://www.ikjzd.com/

rfq:https://www.ikjzd.com/w/251

hemingway:https://www.ikjzd.com/w/2344

心怡:https://www.ikjzd.com/w/1327


本项目目标是开发一个社区网站,拥有发帖、讨论、搜索、登录等一个正常社区拥有的功能。涉及到的版本参数为:JDK1.8Maven3.8.1(直接集成到IDEA)Springboot2.5.1tomcat参考网站(在使用框架过程中可能会看的开发文档):https://mvnrepository.com/查找maven依赖https://mybatis.org/mybatis-3/zh/index.htm
瑞典【冰酒店】—0℃以下体验家的温暖 - :http://www.30bags.com/a/408818.html
瑞典阿比斯库:http://www.30bags.com/a/245433.html
瑞典电视上的中国是什么样子的?来中国旅游的瑞典游客道出了实情:http://www.30bags.com/a/230796.html
瑞典鲱鱼罐头:太臭禁止游客携带上飞机,臭豆腐在它面前都是香的_过程:http://www.30bags.com/a/219924.html
一女多男两根同时进去小说 娇嫩的被三根粗大的:http://lady.shaoqun.com/a/248205.html
同房交换4P好爽 口述做爰全过程和细节:http://lady.shaoqun.com/m/a/247881.html
口述被房东老头不停的要 被老头玩好爽快受不了:http://lady.shaoqun.com/m/a/247575.html
老师抬起臀部让我进去 掀开老师湿润的短裙:http://www.30bags.com/m/a/249910.html
你能不能瞥一眼这些常见的立场,并把它做对?:http://lady.shaoqun.com/a/422142.html
亚马逊等平台1254家店铺被起诉侵权,卖家被冻结3000万元:保护好商标专利迫在眉睫:https://www.ikjzd.com/articles/146616
一些女性产品实际上是男性为了自己使用而发明的:http://lady.shaoqun.com/a/422143.html
提高性生活质量的11个细节:http://lady.shaoqun.com/a/422144.html

没有评论:

发表评论