Pass Entire Object From User's Drop-Down Selec

2019-08-17 23:32发布

问题:

I have an object that contains two strings(ID and Name). I populate a List with two entries that contain both IDs and Names. I am then passing this to the html page using Thymeleaf. I want the user to select a value from the drop-down. The drop-down displays only the name and when the user clicks submit, I want it to return the selected instance of the object with both the name and the ID.

For instance - User selects object.Name - the next mapping receives the object.Name and object.ID so that I can populate the following page based on this data.

I've tried just about every example I can find from Googling and none of the examples really seem to fit what I am trying to do or perhaps I am/have been almost there and missed something tiny. At some point I tried creating another container to hold the object, and used th:field=*{object} with th:object=$container.

Please keep in mind that I have changed the mappings to Post, used ModelAttribute, Request, etc.

This is the current iteration of the code:

Controllers:

@GetMapping(value="/reports")
    public String selectRelease(Model model){
        DBFunctions dbFunctions = new DBFunctions();
        List<TFSQueryStructure> releaseNames = new ArrayList<>();
        TFSQueryStructure tfsQueryStructure= new TFSQueryStructure();

        ResultSet rs = dbFunctions.executeQuery(ReportingQueries.returnQueryName());

        TFSQueryStructure tfsQueryStructures;
        try {
            while(rs.next()){
                tfsQueryStructures = new TFSQueryStructure();
                tfsQueryStructures.setID(rs.getString("ReleaseID").trim());
                tfsQueryStructures.setName(rs.getString("AutomationRelease").trim());
                releaseNames.add(tfsQueryStructures);
            }
        }catch(Exception e){}

        model.addAttribute("releaseNames", releaseNames);
        model.addAttribute("tfsQueryStructure", tfsQueryStructure);

        return "reports";
    }

@GetMapping("/createreport/view")   
public String retrieveReportingDataDataReview(@RequestParam("TFSQueryStructure") TFSQueryStructure tfsQueryStructure, Model model) {

        String releaseID = tfsQueryStructure.getID();
        String releaseName = tfsQueryStructure.getName();
<body>
<div class="container">
    <!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->
</div>

        <form action="#" class="form-horizontal" id="form" th:action="@{/web/createreport/view}" th:object="${tfsQueryStructure}" method="get">
        <div class="form-group">
            <label class="col-md-4 control-label" for="releaseName">Release Name:</label>
            <div class="col-md-4">
                <select id="releaseName" name="releaseName" class="form-control">
                    <option value="">Select Release</option>
                    <option th:each="releaseName: ${releaseNames}" th:value="${releaseName}" th:text="${releaseName.getName()}"></option>
                </select>
            </div>
        </div>

        <!-- Button (Double) -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="submit"></label>
            <div class="col-md-8">
                <button id="submit" type="submit" class="btn btn-success">Submit</button>
            </div>
        </div>

I have managed to get many results - most often that the value for tfsQueryStructure is null. At some point I was also getting string conversion errors, validation errors, etc. Nothing I have tried worked, but the expected outcome is to receive the entire object with the name and matching ID that I get from the List of the objects.

Thank you in advance for any help you might be able to give!