简体   繁体   English

Spring 引导:保存值列表

[英]Spring Boot: Saving List of Values

I'm developing a Java Spring Boot Web App.我正在开发 Java Spring 启动 Web 应用程序。 The page that concerns my question is a form that the user saves.与我的问题有关的页面是用户保存的表单。 It's shown in the image below:如下图所示: 页

Previously I had it so that the user could only save one hardware item (one description, one serial #, and one model #).以前我有它,所以用户只能保存一个硬件项目(一个描述、一个序列号和一个 model #)。 Now, though, you can see that there can be multiple items (here they're are four).但是,现在您可以看到可以有多个项目(这里是四个)。 At first I was simply able to save the description, serial #, and model # as individual Strings in the database for the one record.起初,我只是能够将描述、序列号和 model # 作为单独的字符串保存在数据库中的一条记录中。 This worked fine.这工作得很好。 Now though, I must save these items as Lists of strings for each of the three categories (List of descriptions, List of Serial #s, List of Model #s) for each record.但是现在,我必须将这些项目保存为每个记录的三个类别(描述列表、序列号列表、Model #s 列表)中每一个的字符串列表。 So, my questions pertain to what I must change in order to make this possible.所以,我的问题与我必须改变什么有关,以使这成为可能。 Shown below first is the declaration of these 3 columns in the NewRecord class declared as the table:下面首先展示的是NewRecord class中这3列的声明,声明为表:

    @Size(min=0, max=100, message="{newRecord.hwDescription.size}")
    @Column(name="hwDescription")
    private String hwDescription;
    @Size(min=0, max=100, message="{newRecord.hwSerialNumber.size}")
    @Column(name="hwSerialNumber")
    private String hwSerialNumber;
    @Size(min=0, max=100, message="{newRecord.hwModelNumber.size}")
    @Column(name="hwModelNumber")
    private String hwModelNumber;

I'm assuming here that I would simply need to make the change from private String... to private List<String>... .我在这里假设我只需要将private String...更改为private List<String>... The real confusion for me lies with what to do on the following newRecord.jsp page where below you can see the relevant markup and JavaScript:对我来说真正的困惑在于在以下 newRecord.jsp 页面上做什么,您可以在下面看到相关标记和 JavaScript:

            <div id="hwAddition0">
                <input class="removeItemButton0" type="button" value="Remove Item"
                    id="removeItemButton" onclick="removeHWItem(this)"
                    style="display: inline-block;">
                <div class="form-row">
                    <div class="form-group col-md-4">
                        <label for="hwDescription" style="text-decoration: underline;">Description</label>
                        <form:textarea id="hwDescription" type="text"
                            class="form-control short" path="hwDescription"
                            name="hwDescription" placeholder="Description" maxlength="100"
                            rows="2" style="resize: none;" />
                    </div>
                    <div class="form-group col-md-4">
                        <label for="hwSerialNumber" style="text-decoration: underline;">Serial
                            #</label>
                        <form:input type="text" class="form-control" path="hwSerialNumber"
                            name="hwSerialNumber" placeholder="Serial #" maxlength="100" />
                    </div>
                    <div class="form-group col-md-4">
                        <label for="hwModelNumber" style="text-decoration: underline;">Model
                            #</label>
                        <form:input type="text" class="form-control" path="hwModelNumber"
                            name="hwModelNumber" placeholder="Model #" maxlength="100" />
                    </div>
                </div>
                <hr />
            </div>

JS: JS:

function removeHWItem(e) {

        var text = e.parentElement.id;

        count--;

        if (count == 0) {
            showHide();
            return;
        }

        e.parentElement.remove();
    }

    var count;

    $(function() {
        count = 0;
    });

    $('#hwAdditionButton').click(

            function() {

                if (count == 0) {

                    var clonedObj = $("#hwAddition" + count).clone().attr('id',
                            'hwAddition' + (count + 1));

                    clonedObj.find("#removeItemButton" + count).attr('id',
                            'removeItemButton' + (count + 1));

                    clonedObj.insertAfter("#hwAddition" + count);

                    count++;
                }

                else {

                    var clonedObj = $("#hwAddition" + (count - 1)).clone()
                            .attr('id', 'hwAddition' + count);

                    clonedObj.find("#removeItemButton" + (count - 1)).attr(
                            'id', 'removeItemButton' + count);

                    clonedObj.insertAfter("#hwAddition" + (count - 1));
                }

                count++;

            });

The JS handles the correct defining and redefining of the ids of the elements in each of the items upon addition and/or removal of a particular item. JS 在添加和/或删除特定项目时处理每个项目中元素 ID 的正确定义和重新定义。 Since this is Spring Boot, though, the real issue lies with what to do with the 'path' of each of these elements of each item.但是,由于这是 Spring 引导,真正的问题在于如何处理每个项目的每个元素的“路径”。 This is an issue because the Lists of hwDescription, hwModelNumber, and hwSerialNumber are all generically defined with no number after them, like I have done with the ids.这是一个问题,因为 hwDescription、hwModelNumber 和 hwSerialNumber 的列表都是一般定义的,它们后面没有数字,就像我对 id 所做的那样。 I could also redefine the path values to be 'hwDescription0', 'hwDescription1', 'hwDescription2', etc. for each addition/removal of an item.对于每次添加/删除项目,我还可以将路径值重新定义为“hwDescription0”、“hwDescription1”、“hwDescription2”等。 However, then the path back to the original List variable name 'hwDescription' would then be incorrect.但是,返回原始列表变量名称“hwDescription”的路径将不正确。 So, I would appreciate any insight as to how to go about being able to redefine the path values (if that's what I even need to do) so that every new item saved adds a hwDescription, hwSerialNumber, and hwModelNumber to these lists for each record.因此,我将不胜感激有关如何 go 能够重新定义路径值(如果这是我什至需要做的话)的任何见解,以便保存的每个新项目都会为每条记录的这些列表添加一个 hwDescription、hwSerialNumber 和 hwModelNumber .

Let me split it up into multiple parts.让我把它分成多个部分。

Basically, as you are probably saving your data into a database you need to think of a good data model to save the information too.基本上,由于您可能将数据保存到数据库中,您需要考虑一个好的数据 model 来保存信息。 When we are talking about relational databases it is of course a relational data model where you split the dataset up into entities comply good practices of data modeling.当我们谈论关系数据库时,它当然是关系数据 model,您可以在其中将数据集拆分为符合数据建模的良好实践的实体。 Quickly brainstorming your use case offers a table which contains HARDWARE_ITEMS .快速集思广益您的用例会提供一个包含HARDWARE_ITEMS的表。 You would probably have the DESCRIPTION as a text field of variable length.您可能会将DESCRIPTION作为可变长度的文本字段。 Your SERIAL_NO could be either the primary key or at least a unique field.您的SERIAL_NO可以是主键或至少是一个唯一字段。 THE MODEL_NO would probably be a foreign key referencing the MODELS table with more specific information about these. THE MODEL_NO可能是引用MODELS表的外键,其中包含有关这些的更具体信息。

As description, serial and model-number belong together as a tuple for one model item it does not really make sense to transfer the information of your frontend as List of descriptions , List of serials and List of model-no .作为描述,序列号和型号作为一个元组属于一个 model 项目,将前端的信息作为List of descriptions List of serials List of model-no As you said that will be for example a pain associating the pairs again together.正如您所说,例如将这对再次关联在一起会很痛苦。 Normally you would transfer a List of model-items which each contains of its three properties.通常,您会传输一个List of model-items都包含其三个属性。

You could have an endpoint accepting the structure of a single item.您可以有一个端点接受单个项目的结构。 Your bulk save/edit endpoint could accept as said the list of items which you could save then accordingly as multiple instances of your entity.您的批量保存/编辑端点可以接受您可以相应地保存为实体的多个实例的项目列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM