Software Architecture

CS/BS with Spring MVC

回顾

从主机到CS模式









          早期集中式计算环境                          PC与主机任务分割(CS)

三层的分割


Fat Clients

C/S模式的传统形式,一般用户个人软件系统

  • 应用系统在Client端运行
  • Client知道Server上的数据、文件等如何组织和存储
  • 为用户端程序的设计和开发提供较大的灵活性和便利性

Fat Servers

  • Server通过一组确定的过程提供资源访问,而非提供对资源直接操作
  • Client提供GUI界面供用户进行操作,并通过远程方法调用与Server通信,获得服务
  • 应用代码集中于Server端,便于部署和管理,减少网络通信开销

From 2-tiers to 3 tiers

资源共享,集中化管理,性能分布,安全

Java EE

Java Servlet

import javax.servlet.*;
import java.io.*; 

public class BasicServlet extends GenericServlet { 
	public void service(ServletRequest req, 
                            ServletResponse resp)
	    throws Servlet etException, IOException { 
		resp.setContentType("text/plain"); 
		PrintWriter out = resp.getWriter( ); 
		out.println("Hello.");
	} 
}

B/S架构:更便于部署和管理

A browser-based architecture runs an application directly from an internet browser on any device that has a browser (laptop, tablet, smartphone).

Web Application

https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

发展史

最初

<!DOCTYPE html>
<html>
  <head>
    <title>This is a title</title>
  </head>
  <body>
    <p>Hello world!</p>
  </body>
</html>

CGI

Common Gateway Interface

  • Forward requests to external programs
  • Spawn one process for each new request
  • Communicate via Standard input/output
  • Process terminates after request is handled

CGI Example

#!perl -w  
use CGI qw/:standard/;  
{  
  my $cgi= new CGI;  
  print $cgi->header,  
  $cgi->start_html('A Simple Example'),  
  $cgi->h1('A Simple Example');  
  if ( $cgi->param())  
  {  
    print "Your name is ",  
    $cgi->param('name');  
  }  
  $cgi->end_html();  
}

Active Pages

<html>
<head><title>First JSP</title></head>
<body>
  <%
    double num = Math.random();
    if (num > 0.95) {
  %>
      <h2>You will have a luck day!</h2><p>(<%= num %>)</p>
  <% } else { %>
      <h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
  <% } %>
  <a href="<%= request.getRequestURI() %>"><h3>Try Again</h3></a>
</body>
</html>

Servlet

  • Containing application-specific code
  • Web server does generic part of request handling
  • Servlets run “in” the web server and do some of the handling

Servlet

  • Advantages:
    • Running a servlet doesn’t require creating a separate process each time and one instance can handle multiple requests, not a separate instance for every request
    • It stays in memory and doesn’t have to be reloaded each time
    • Untrusted servlets can be run in a “sandbox”
  • Disadvantage:
    • Less choice of languages (CGI scripts can be in any language)

Servlet Example

import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 
public class HelloWorldServlet extends HttpServlet { 
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException { 
		resp.setContentType("text/html"); 
		PrintWriter out = resp.getWriter( ); 
		out.println("<html>"); 
		out.println( "<head><title>Have you seen this before?</title></head>"); 
		out.println( "<body><h1>Hello, World!</h1><h6>Again.</h6></body></html>"); 
	} 
} 

https://www.bilibili.com/video/BV1zE411M7HM?p=1 (16:50)

sa-spring/servlet

doGet()

问题来了

  • Context
    • 人机交互界面设计
  • Problem
    • 功能的变化要求用户界面的改变
    • 平台的改变要求用户界面随之改变
    • 界面的改变需足够方便,且不应影响系统功能本身

做一些改变

UI Template

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    request.setAttribute("time", new Date()); // 'time' would be  shown on JSP page             
    RequestDispatcher view = request.getRequestDispatcher("WEB-INF/templates/sample.jsp");      
    view.forward(request, response);
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Sample Page</title>
  </head>
  <body>
    <b>Time Now:</b> ${requestScope["time"]}
  </body>
</html>

UI Template Demo



https://www.bilibili.com/video/BV1zE411M7HM?p=2 (5:12)


sa-spring/thymeleaf3-servlet-helloworld

做更多改变

MVC

系统分割为processing, output和input三个部分

  • “Model”: processing,核心数据功能
  • “View”: output, 从Model获得数据显示给用户
  • “Controller”: input, 处理事件操作模型

MVC实例

  • Graphical Editing Framework (GEF)
  • Spring MVC
  • ASP.Net MVC
  • Struts
  • Flask

GEF

The Graphical Editing Framework (GEF) allows developers to take an existing application model and quickly create a rich graphical editor.

GEF MVC

Spring MVC

Spring Web

spring-guides/
gs-serving-web-content

https://www.bilibili.com/video/BV1zE411M7HM?p=3 (1:10)

Testing

https://spring.io/guides/gs/testing-web/

spring-guides/gs-testing-web

https://www.bilibili.com/video/BV1zE411M7HM?p=3 (7:05)

Spring Petclinic

sa-spring/spring-petclinic

代码结构

https://www.bilibili.com/video/BV1zE411M7HM?p=4

Community Versions

改进版本

https://www.bilibili.com/video/BV1zE411M7HM?p=4 (4:00)

spring-petclinic/spring-framework-petclinic

Spring Stereotype

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/package-summary.html

多视图

https://www.bilibili.com/video/BV1zE411M7HM?p=4 (12:00)

sa-spring/spring-mobile

小结与概念梳理