Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从双精度列表创建坐标_Java_Spring Boot_Spring Mvc - Fatal编程技术网

Java 从双精度列表创建坐标

Java 从双精度列表创建坐标,java,spring-boot,spring-mvc,Java,Spring Boot,Spring Mvc,我在SpringBoot中有一个表单,人们可以在其中创建一个或多个坐标。 输入字段是分开的(对于X和Y),但都有相同的名称,因此人们创建的所有坐标都放在一个双精度列表中。现在我遇到了一个问题,我需要将它转换为坐标列表。列表中的值顺序正确(索引0为X,索引1为Y坐标,从索引2开始,新坐标开始等)。但我不知道如何从一个流中每次获得2个列表值来创建坐标。以下是我到目前为止的代码: public class LocationForm { private Integer id; @NotB

我在SpringBoot中有一个表单,人们可以在其中创建一个或多个坐标。 输入字段是分开的(对于X和Y),但都有相同的名称,因此人们创建的所有坐标都放在一个双精度列表中。现在我遇到了一个问题,我需要将它转换为坐标列表。列表中的值顺序正确(索引0为X,索引1为Y坐标,从索引2开始,新坐标开始等)。但我不知道如何从一个流中每次获得2个列表值来创建坐标。以下是我到目前为止的代码:

public class LocationForm {
    private Integer id;
    @NotBlank
    @Size(min=6, max=64)
    private String name;
    @NotNull
    private LocationType type;
    @ListNotBlank
    private List<Double> geometry;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public LocationType getType() {
        return type;
    }
    public void setType(LocationType type) {
        this.type = type;
    }
    public List<Double> getGeometry() {
        return geometry;
    }
    public void setGeometry(List<Double> geometry) {
        this.geometry = geometry;
    }
}
公共类位置表单{
私有整数id;
@不空白
@尺寸(最小值为6,最大值为64)
私有字符串名称;
@NotNull
私有位置类型;
@ListNotBlank
私有列表几何;
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共位置类型getType(){
返回类型;
}
公共无效集合类型(LocationType类型){
this.type=type;
}
公共列表getGeometry(){
返回几何;
}
公共空心集几何图形(列出几何图形){
这个几何=几何;
}
}
上面的代码验证来自表单的输入,并从中生成一个对象。坐标的输入字段如下所示:

<div class="form-row">
                        <div class="form-group col-md-6">
                          <label for="coordinates">Coordinate X</label>
                          <input type="text" th:class="${#fields.hasErrors('geometry')} ? 'form-control is-invalid' : 'form-control'" name="geometry" id="geometry" 
                          placeholder="Enter coordinates" >
                          <small th:if="${#fields.hasErrors('geometry')}" th:errors="*{geometry}" class="text-danger"></small>
                        </div>
                        <div class="form-group col-md-6">
                          <label for="coordinates">Coordinate Y</label>
                          <div class="d-flex">
                              <input type="text" th:class="${#fields.hasErrors('geometry')} ? 'form-control is-invalid' : 'form-control'" name="geometry" id="geometry" 
                              placeholder="Enter coordinates" >
                              <button class="btn btn-success" type="button"  onclick="geometry_fields();">Add</button>
                          </div>
                        </div>
                    </div>

坐标X
坐标Y
添加
上面你可以看到X和Y是分开的,但是因为他们有相同的名字,所以所有的X和Y都会被放在双打列表中。有人知道如何使用上述输入创建坐标列表吗?

因为您的(业务)模型表示两个坐标系,为什么要创建
列表
。相反,尝试通过将
几何体
对象更改为两个不同的对象来解决此问题,例如,
双X;双Y

这将使视图-模型-控制器之间的数据流变得轻松

如果您计划做大量的地理空间工作,那么请看看如何使用这个

,因为您的(业务)模型代表了两个坐标系,为什么要创建
列表
。相反,尝试通过将
几何体
对象更改为两个不同的对象来解决此问题,例如,
双X;双Y

这将使视图-模型-控制器之间的数据流变得轻松


如果你计划做大量的地理空间方面的事情,那么看看如何使用这两个问题。首先,将您的模型更新为类似于
List
的东西,其中
Coordinate
是一个简单的bean,有两个道具
x
y

其次,要在SpringMVC中绑定到列表,需要使用索引属性。因此,您的表单如下所示:

<input type="text" th:class="${#fields.hasErrors('geometry')} ? 
             'form-control is-invalid' : 'form-control'" name="coordinate[0].x">

<input type="text" th:class="${#fields.hasErrors('geometry')} ? 
              'form-control is-invalid' : 'form-control'" name="coordinate[0].y">


然后坐标[1].x等等。

两个问题。首先,将您的模型更新为类似于
List
的东西,其中
Coordinate
是一个简单的bean,有两个道具
x
y

其次,要在SpringMVC中绑定到列表,需要使用索引属性。因此,您的表单如下所示:

<input type="text" th:class="${#fields.hasErrors('geometry')} ? 
             'form-control is-invalid' : 'form-control'" name="coordinate[0].x">

<input type="text" th:class="${#fields.hasErrors('geometry')} ? 
              'form-control is-invalid' : 'form-control'" name="coordinate[0].y">


然后
坐标[1].x
等等。

正如您可能看到的,您可以添加更多这样的输入字段,以便创建多元素x和Y坐标。这样,您仍然需要从2个双列表创建一个坐标,然后从一个位置表单收集多个坐标字段??您可以创建将形成的坐标或一个点(在这种情况下,它只是一个坐标),但您也可以创建多边形,这当然是从多个元素坐标创建的。如果您的用例支持多个坐标,那么Alan的方法应该可以正常工作?正如您可能看到的,您可以添加更多这样的输入字段,从而创建多个元素X和Y坐标。这样,您仍然需要从2个双列表创建一个坐标,然后从一个位置表单收集多个坐标字段??您可以创建将形成的坐标或一个点(在这种情况下,它只是一个坐标),但您也可以创建多边形,这当然是从多个元素坐标创建的。如果您的用例是支持多个坐标,那么Alan的方法应该正确吗?