【问题标题】:Sharing field constraints between server and client side在服务器端和客户端之间共享字段约束
【发布时间】:2020-05-11 17:18:10
【问题描述】:

我正在做一个项目,服务器端基于 Spring Boot 2,前端基于 Angular。

在服务器端,在数据模型类中我有这样的声明:

@Column(length = 256, nullable = false)
@Size(max = 256)
private String subject;

我也想在前端(角度侧)实现字段长度验证。

在服务器端和客户端之间共享字段长度限制的最佳方法是什么?

我不喜欢我需要在双方(服务器端和客户端)重复自己和硬编码字段长度的想法。

如果我像这样声明一组常量,这对我来说是不是最好的方法:

private static final int maxSubjectLength = 256;

并按如下方式使用它们:

@Column(length = maxSubjectLength, nullable = false)
@Size(max = maxSubjectLength)
private String subject;

然后用这些常量创建一个配置类,哪个实例可以通过GEThttp-request 访问?

或者有更好的方法?

【问题讨论】:

    标签: java angular typescript spring-boot jpa


    【解决方案1】:

    我决定使用以下方法。假设我们有一个模型类Question,它有一个属性body

    @Entity
    @Table(name = "Questions")
    public final class Question {
    
        // other properties & code
    
        private String body;
    
        // other properties & code
    }
    

    我们希望将正文长度限制为1024 符号,并仅在服务器上定义一次此限制,并在应用程序的后端和前端使用此限制。

    在服务器端,在我们的模型类Question 中,我们定义了静态映射,其中包含所有类属性的大小限制。

    @Entity
    @Table(name = "Questions")
    public final class Question {
    
        private static class ModelConstraints {
            static final int MAX_BODY_LENGTH = 1024;
            // limits for other fields are going here
        }
    
        private static final Map<String, Integer> modelConstraintsMap;
    
        static
        {
            final Map<String, Integer> localConstraintsMap = new HashMap<>();
            localConstraintsMap.put("MAX_BODY_LENGTH", ModelConstraints.MAX_BODY_LENGTH);
            // .... putting all constants from ModelConstraints to the map here
    
            // composing unmodifable map    
            modelConstraintsMap = Collections.unmodifiableMap(localConstraintsMap);
        }
    
        @Column(length = ModelConstraints.MAX_BODY_LENGTH, nullable = false)
        @Size(max = ModelConstraints.MAX_BODY_LENGTH)
        private String body;
    
        // other properties and code
    
        public static Map<String, Integer> getModelConstraintsMap() {
            return modelConstraintsMap;
        }   
    
        // other properties and code
    }
    

    内部类ModelConstraints 包含所有相关模型属性的最大长度值的定义。

    在静态块中,我创建了一个不可修改的地图,其中包含这些约束,并通过公共方法返回此地图。

    在控制器中,与模型类相关,我添加了一个返回属性长度约束的休息端点。

    @RequestMapping(path = "/questions/model-constraints", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<Map<String, Integer>> getModelConstraints() {
        return new ResponseEntity<>(Question.getModelConstraintsMap(), HttpStatus.OK);
    }
    

    此方法返回带有属性长度约束的地图的 json 表示。

    在(角)前端,我称之为端点并设置表单字段的maxlength 属性,与模型类属性相关。

    在我添加并调用这个方法的组件打字稿文件中:

      loadConstraints() {
        var url: string = "/questions/model-constraints";
        this.http
          .get(url)
          .subscribe((data: Map<string, number>) => (this.modelConstraints = data));
      }
    

    调用此方法后,组件属性modelConstraints 将包含具有字段长度限制的映射。

    我在组件模板 (html) 文件中设置了这些约束。

      <textarea
        matInput
        rows="7"
        placeholder="Question body"
        maxlength="{{ modelConstraints['MAX_BODY_LENGTH'] }}"
        [(ngModel)]="questionBody"
      ></textarea>
    

    就是这样。使用这种方法,您只能在服务器上定义一次字段长度,然后在服务器和客户端上使用此定义。

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      相关资源
      最近更新 更多