Spring MVC框架:第七章:REST架构风格

简介: Spring MVC框架:第七章:REST架构风格

第一节 REST简介

1.概念

Representational State Transfer——表现层(资源)状态转化。是目前最流行的一种互联网软件架构风格。它倡导结构清晰、符合标准、易于理解、扩展方便的Web架构体系,主张严格按照HTTP协议中定义的规范设计结构严谨的Web应用架构体系。由于REST所倡导的理念让Web应用更易于开发和维护,更加优雅简洁,所以正得到越来越多网站的采用。

资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。要获取这个资源,访问它的URI就可以,因此 URI 即为每一个资源的独一无二的识别符。


表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层(Representation)。比如,文本可以用txt格式表现,也可以用HTML格式、XML格式、JSON格式表现,甚至可以采用二进制格式。


状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化”(State Transfer)。而这种转化是建立在表现层之上的,所以就是 “表现层状态转化”。具体说,就是 HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。


请求方式 作用

GET          查询

POST        保存

PUT          更新

DELETE     删除


2.REST风格的URL


REST风格要求我们不要再使用问号键值对的方式携带请求参数,而是从URL地址中获取。下面我们进行一下对比:



3.REST风格URL的好处

①含蓄,安全

使用问号键值对的方式给服务器传递数据太明显,容易被人利用来对系统进行破坏。使用REST风格携带数据不再需要明显的暴露数据的名称。

②风格统一


URL地址整体格式统一,从前到后始终都使用斜杠划分各个内容部分,用简单一致的格式表达语义。

③无状态


在调用一个接口(访问、操作资源)的时候,可以不用考虑上下文,不用考虑当前状态,极大的降低了系统设计的复杂度。

④严谨,规范


严格按照HTTP1.1协议中定义的请求方式本身的语义进行操作。

⑤简洁,优雅


过去做增删改查操作需要设计4个不同的URL,现在一个就够了


⑥丰富的语义

通过URL地址就可以知道资源之间的关系。

http://localhost:8080/shop 
http://localhost:8080/shop/product 
http://localhost:8080/shop/product/cellPhone 
http://localhost:8080/shop/product/cellPhone/iPhone

第二节 SpringMVC对四种请求方式的支持

1.说明


受HTML的限制,只有GET请求和POST请求是可以直接生成的。为了生成PUT和DELETE请求方式我们需要借助一个过滤器:org.springframework.web.filter.HiddenHttpMethodFilter,这个过滤器可以将POST请求转换为PUT或DELETE等其他形式。

2.HiddenHttpMethodFilter的使用方法

①在web.xml中进行配置,拦截所有资源

<filter>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

②在表单隐藏域中通过_method请求参数附带请求方式名称

jsp代码:

<input type="hidden" name="_method" value="put"/>

③通过点击超链接执行删除操作。

这是一个难点,超链接中没有表单隐藏域,所以需要将超链接转换为表单进行提交,这就需要借助于JavaScript。

[1]在页面上创建一个action属性为空的form表单

jsp代码:

<form method="POST">
  <input type="hidden" name="_method" value="DELETE" />
</form>

[2]给所有超链接绑定单击响应函数

jsp代码:

<a href="${pageContext.request.contextPath}/emp/ID" class="empRemove">删除</a>

jsp中jquery代码:

$(".empRemove").click(function(){
      //※※※※※※※※※以下操作将GET请求转换为POST请求※※※※※※※※※
      //1.先获取到当前超链接原本要访问的URL地址
      //this是当前被点击的超链接的引用,是DOM对象
      var targetUrl = this.href;
      //2.获取负责转换请求方式的表单的jQuery对象
      var $form = $("form");
      //3.将表单的action属性设置为超链接的URL地址
      $form.attr("action",targetUrl );
      //4.提交表单
      //将表单元素封装为jQuery对象后调用submit()方法可以提交表单,相当于点击表单的提交按钮
      $form.submit();
      //5.超链接不跳转
      return false;
});

@PathVariable注解

通过URL地址携带的数据需要通过@PathVariable注解来获取。它的用法是:

Handled代码:

//使用@PathVariable注解将URL地址中的变量匹配出来
@RequestMapping(value="/emp/{empId}", method=RequestMethod.DELETE)
public String testPathVariable(@PathVariable("empId") String empId) {
  System.out.println("empId="+empId);
  return "redirect:/result";
}

实战代码:

项目结构



web.xml

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- Map all requests to the DispatcherServlet for handling -->
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
    <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  <!-- 包扫描 -->
  <context:component-scan base-package="com.*" />
  <!-- 加前缀后缀 -->
  <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/page/" />
    <property name="suffix" value=".jsp" />
  </bean>
  <!-- 保证常规资源可以访问 -->
  <mvc:annotation-driven></mvc:annotation-driven>
  <!-- 保证静态资源可以访问 -->
  <mvc:default-servlet-handler />
</beans>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/emp">去list页面</a>
</body>
</html>

dataList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
  $(function(){
    $(".remove").click(function(){
      var data = $(this).parents("tr").children("td:eq(1)").text();
      var confirm = window.confirm("你确定要"+data+"删除吗?");
      if(!confirm){
        return false;
      }
      //※※※※※※※※※以下操作将GET请求转换为POST请求※※※※※※※※※
      //1.先获取到当前超链接原本要访问的URL地址
      //this是当前被点击的超链接的引用,是DOM对象
      var targetUrl = this.href;
      //2.获取负责转换请求方式的表单的jQuery对象
      var $form = $("form");
      //3.将表单的action属性设置为超链接的URL地址
      $form.attr("action",targetUrl );
      //4.提交表单
      //将表单元素封装为jQuery对象后调用submit()方法可以提交表单,相当于点击表单的提交按钮
      $form.submit();
      //5.超链接不跳转
      return false;
    });
  });
</script>
</head>
<body>
  <form method="POST">
    <input type="hidden" name="_method" value="DELETE" />
  </form>
  <center>
    <table>
      <c:if test="${empty requestScope.list }">
        <tr>
          <td>没有查询到数据</td>
        </tr>
      </c:if>
      <c:if test="${!empty requestScope.list }">
        <tr>
          <td>ID</td>
          <td>姓名</td>
          <td>SSN</td>
          <td>部门名称</td>
          <td colspan="2">操作</td>
        </tr>
        <c:forEach items="${requestScope.list }" var="list">
          <tr>
            <td>${list.empId}</td>
            <td>${list.empName}</td>
            <td>${list.ssn }</td>
            <td>${list.department.deptName }</td>
            <td><a href="${pageContext.request.contextPath }/emp/${list.empId}" class="remove">删除</a></td>
            <td><a href="${pageContext.request.contextPath }/emp/${list.empId}">编辑</a></td>
          </tr>
        </c:forEach>
      </c:if>
      <tr>
        <td colspan="6" align="center">
        <a href="${pageContext.request.contextPath }/emp/list">添加</a></td>
      </tr>
    </table>
  </center>
</body>
</html>

dataEdit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <form action="${pageContext.request.contextPath }/emp"
    method="post">
    <input type="hidden" name="_method" value="put" />
    <input type="hidden" name="empId" value="${requestScope.emdit.empId }"/>
    <table>
      <tr>
        <td>姓名</td>
        <td><input type="text" name="empName" value="${emdit.empName }" />
        </td>
      </tr>
      <tr>
        <td>SSN</td>
        <td><input type="text" name="ssn" value="${emdit.ssn }" /></td>
      </tr>
      <tr>
        <td>所在部门</td>
        <td><select name="department.deptId">
            <c:if test="${!empty deptList }">
              <c:forEach items="${requestScope.deptList}" var="dept">
                <c:if test="${dept.deptId==requestScope.emdit.department.deptId }">
                  <option value="${dept.deptId }" selected="selected">${dept.deptName }</option>
                </c:if>
                <option value="${dept.deptId }" selected="selected">${dept.deptName }</option>
              </c:forEach>
            </c:if>
        </select></td>
      </tr>
      <tr>
        <td colspan="2"><input type="submit" value="保存" /></td>
      </tr>
    </table>
  </form>
</body>
</html>

dataAdd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/emp" method="post">
    <table>
      <tr>
        <td>姓名</td>
        <td>
          <input type="text" name="empName"/>
        </td>
      </tr>
      <tr>
        <td>SSN</td>
        <td>
          <input type="text" name="ssn"/>
        </td>
      </tr>
      <tr>
        <td>所在部门</td>
        <td>
          <select name="department.deptId">
            <c:if test="${empty deptList }">
              <option>部门数据查询失败!!!</option>
            </c:if>
            <c:if test="${!empty deptList }">
              <c:forEach items="${requestScope.deptList}" var="dept">
                <option value="${dept.deptId }">${dept.deptName }</option>
              </c:forEach>
            </c:if>
          </select>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <input type="submit" value="保存"/>
        </td>
      </tr>
    </table>
  </form>
</body>
</html>

DeptDao.java

package com.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import com.pojo.Department;
@Repository
public class DeptDao {
  private static Map<String, Department> dataMap;
  private static Map<String, Department> namedMap;
  static {
    dataMap = new HashMap<>();
    namedMap = new HashMap<>();
    Department department = new Department(UUID.randomUUID().toString(), "市场部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "销售部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "行政部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "人事部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "技术部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
    department = new Department(UUID.randomUUID().toString(), "公关部");
    dataMap.put(department.getDeptId(), department);
    namedMap.put(department.getDeptName(), department);
  }
  public static Department getDeptByName(String deptName) {
    return namedMap.get(deptName);
  }
  public static Department getDeptById(String uuid) {
    return dataMap.get(uuid);
  }
  public List<Department> getDeptList() {
    return new ArrayList<>(dataMap.values());
  }
}

EmpDao.java

package com.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import com.pojo.Department;
import com.pojo.Employee;
@Repository
public class EmpDao {
  private static Map<String, Employee> dataMap;
  static {
    dataMap = new HashMap<>();
    String empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "乔峰", "SSN001", DeptDao.getDeptByName("市场部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "虚竹", "SSN002", DeptDao.getDeptByName("市场部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "段誉", "SSN003", DeptDao.getDeptByName("市场部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "鸠摩智", "SSN004", DeptDao.getDeptByName("技术部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "萧远山", "SSN005", DeptDao.getDeptByName("技术部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "慕容复", "SSN006", DeptDao.getDeptByName("技术部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "段正淳", "SSN007", DeptDao.getDeptByName("公关部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "段延庆", "SSN008", DeptDao.getDeptByName("公关部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "丁春秋", "SSN009", DeptDao.getDeptByName("销售部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "无崖子", "SSN010", DeptDao.getDeptByName("人事部")));
    empId = UUID.randomUUID().toString();
    dataMap.put(empId, new Employee(empId, "慕容博", "SSN011", DeptDao.getDeptByName("人事部")));
  }
  public void saveEmp(Employee employee) {
    String empId = UUID.randomUUID().toString();
    employee.setEmpId(empId);
    String deptId = employee.getDepartment().getDeptId();
    Department department = DeptDao.getDeptById(deptId);
    employee.setDepartment(department);
    dataMap.put(empId, employee);
  }
  public void removeEmp(String empId) {
    dataMap.remove(empId);
  }
  public void updateEmp(Employee employee) {
    String deptId = employee.getDepartment().getDeptId();
    Department department = DeptDao.getDeptById(deptId);
    employee.setDepartment(department);
    dataMap.put(employee.getEmpId(), employee);
  }
  public Employee getEmpById(String empId) {
    return dataMap.get(empId);
  }
  public List<Employee> getEmpList() {
    return new ArrayList<>(dataMap.values());
  }
}

Department

package com.pojo;
public class Department {
  private String deptId;
  private String deptName;
  public Department() {
  }
  public Department(String deptId, String deptName) {
    super();
    this.deptId = deptId;
    this.deptName = deptName;
  }
  public String getDeptId() {
    return deptId;
  }
  public void setDeptId(String deptId) {
    this.deptId = deptId;
  }
  public String getDeptName() {
    return deptName;
  }
  public void setDeptName(String deptName) {
    this.deptName = deptName;
  }
  @Override
  public String toString() {
    return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";
  }
}

Employee

package com.pojo;
public class Employee {
  private String empId;
  private String empName;
  private String ssn;
  private Department department;
  public Employee(String empId, String empName, String ssn, Department department) {
    super();
    this.empId = empId;
    this.empName = empName;
    this.ssn = ssn;
    this.department = department;
  }
  public Employee() {
  }
  public String getEmpId() {
    return empId;
  }
  public void setEmpId(String empId) {
    this.empId = empId;
  }
  public String getEmpName() {
    return empName;
  }
  public void setEmpName(String empName) {
    this.empName = empName;
  }
  public String getSsn() {
    return ssn;
  }
  public void setSsn(String ssn) {
    this.ssn = ssn;
  }
  public Department getDepartment() {
    return department;
  }
  public void setDepartment(Department department) {
    this.department = department;
  }
  @Override
  public String toString() {
    return "Employee [empId=" + empId + ", empName=" + empName + ", ssn=" + ssn + ", department=" + department
        + "]";
  }
}

Services

package com.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dao.DeptDao;
import com.dao.EmpDao;
import com.pojo.Department;
import com.pojo.Employee;
@Service
public class Services {
  @Autowired
  private DeptDao deptDao;
  @Autowired
  private EmpDao empDao;
  public List<Department> deptDaoList(){
    return deptDao.getDeptList();
  };
  public List<Employee> empDaoList(){
    return empDao.getEmpList();
  }
  public void saveData(Employee employee) {
    empDao.saveEmp(employee);
  }
  public void delectData(String empId) {
    empDao.removeEmp(empId);
  }
  public Employee emdit(String empId) {
    Employee empById = empDao.getEmpById(empId);
    return empById;
  }
  public void updateEmp(Employee employee) {
    empDao.updateEmp(employee);
  }
}

Handled

package com.handled;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.pojo.Department;
import com.pojo.Employee;
import com.services.Services;
import com.sun.tracing.dtrace.ProviderAttributes;
@Controller
public class Handled {
  @Autowired
  private Services services;
  @RequestMapping("/emp")
  public String toList(Model model) {
    List<Employee> empDaoList = services.empDaoList();
    model.addAttribute("list", empDaoList);
    return "dataList";
  }
  @RequestMapping("/emp/list")
  public String toAddData(Model model) {
    List<Department> deptDaoList = services.deptDaoList();
    model.addAttribute("deptList", deptDaoList);
    return "dataAdd";
  }
  @RequestMapping(value="/emp",method=RequestMethod.POST)
  public String addData(Employee employee) {
    services.saveData(employee);
    return "redirect:/emp";
  }
  @RequestMapping(value="/emp/{empId}",method=RequestMethod.DELETE)
  public String deleteData(@PathVariable("empId") String empId) {
    services.delectData(empId);
    return "redirect:/emp";
  }
  @RequestMapping(value="/emp/{empId}",method=RequestMethod.GET)
  public String emditData(@PathVariable("empId") String empId,Model model) {
    List<Department> deptDaoList = services.deptDaoList();
    Employee employee = services.emdit(empId);
    model.addAttribute("emdit",employee);
    model.addAttribute("deptList",deptDaoList);
    return "dataEdit";
  }
  @RequestMapping(value="/emp",method=RequestMethod.PUT)
  public String subemdData(Employee employee) {
    services.updateEmp(employee);
    return "redirect:/emp";
  }
}
相关文章
|
3天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
14 1
|
9天前
|
前端开发 Java PHP
信息系统架构模型(1) MVC
信息系统架构模型(1) MVC
13 0
|
14天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【4月更文挑战第17天】Spring Cloud是Java微服务治理的首选框架,整合了Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心)。通过Eureka实现服务注册与发现,Ribbon提供负载均衡,Hystrix实现熔断保护,Zuul作为API网关,Config Server集中管理配置。理解并运用Spring Cloud进行微服务治理是现代Java开发者的关键技能。
|
15天前
|
敏捷开发 监控 前端开发
深入理解自动化测试框架Selenium的架构与实践
【4月更文挑战第16天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加快迭代速度的关键手段。Selenium作为一种广泛使用的自动化测试工具,其开源、跨平台的特性使得它成为业界的首选之一。本文旨在剖析Selenium的核心架构,并结合实际案例探讨其在复杂Web应用测试中的高效实践方法。通过详细解读Selenium组件间的交互机制以及如何优化测试脚本,我们希望为读者提供深入理解Selenium并有效运用于日常测试工作的参考。
|
18天前
|
XML 前端开发 测试技术
安卓架构模式:MVC、MVP、MVVM及更多
【4月更文挑战第13天】本文探讨了安卓应用开发中的常见架构模式,包括MVC、MVP和MVVM,以及VIPER和Clean Architecture。MVC分离关注点,易于理解,但安卓不直接支持。MVP通过呈现器实现更清晰的分层和便于单元测试。MVVM利用数据绑定简化UI逻辑,适合声明式编程。开发者应根据项目需求、团队技能和维护周期选择合适架构,随着工具和框架的进步,未来将提供更多模块化、可测试性和敏捷性的解决方案。
|
19天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
19天前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
19天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
31 3
|
28天前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南
|
29天前
|
负载均衡 网络协议 Java
构建高效可扩展的微服务架构:利用Spring Cloud实现服务发现与负载均衡
本文将探讨如何利用Spring Cloud技术实现微服务架构中的服务发现与负载均衡,通过注册中心来管理服务的注册与发现,并通过负载均衡策略实现请求的分发,从而构建高效可扩展的微服务系统。