首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在服务器和客户端之间共享字段约束

在服务器和客户端之间共享字段约束
EN

Stack Overflow用户
提问于 2020-01-26 02:44:32
回答 1查看 36关注 0票数 1

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

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

代码语言:javascript
复制
@Column(length = 256, nullable = false)
@Size(max = 256)
private String subject;

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

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

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

在我的例子中,如果我像这样声明一组常量,这是不是最好的方法:

代码语言:javascript
复制
private static final int maxSubjectLength = 256;

并按如下方式使用它们:

代码语言:javascript
复制
@Column(length = maxSubjectLength, nullable = false)
@Size(max = maxSubjectLength)
private String subject;

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

或者有一种更好的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-12 00:11:19

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

代码语言:javascript
复制
@Entity
@Table(name = "Questions")
public final class Question {

    // other properties & code

    private String body;

    // other properties & code
}

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

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

代码语言:javascript
复制
@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包含所有相关模型属性的最大长度值的定义。

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

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

代码语言:javascript
复制
@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属性。

在组件类型脚本文件中,我添加并调用此方法:

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

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

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

代码语言:javascript
复制
  <textarea
    matInput
    rows="7"
    placeholder="Question body"
    maxlength="{{ modelConstraints['MAX_BODY_LENGTH'] }}"
    [(ngModel)]="questionBody"
  ></textarea>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59912369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档