1

I tried searching but could not find a solution that fits my scenario.

I have a JSP as follows.


    <div class="form-parameters" id="form-parameters">
            <form action="/reports" method="post">
                <h3 class="text-center">Test File</h3>
                <div class="row">
                    <h5 class="col-md-2">Report Expiring Days:</h5>
                    <input type="text" name="expiringDays" class="col-md-4 form-control"
                        value='${parameterValues["expiring_days"]} '/>
                </div>
                <div class="row">
                    <h5 class="col-md-2">Report Expired [days] ago:</h5>
                    <input type="text" name="expiredDays" class="col-md-4 form-control"
                        value="${parameterValues['Exceeded_by_Days']}" />
                </div>
                <div class="organization-information">
                    <div class="row">
                        <div class="col-md-6">
                            <h4>Organization Information</h4>
                        </div>
                        <div class="col-md-6">
                            <h4>Student Information</h4>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-2">
                            <h5>School:</h5>
                        </div>
                        <div class="col-md-4">
                            <select class="form-control school" multiple="multiple"
                                id="schoolNames">
                            </select>
                        </div>
                        <div class="col-md-2">
                            <h5>Area</h5>
                        </div>
                        <div class="col-md-4">
                            <select class="form-control area" multiple="multiple">
                            </select>
                        </div>
                    </div>
                </div>
    </div>
    </body
    </html>

I am using Select2 for the select dropdowns. I want to get the values from controller on page load and display it. I am able to render the values for the jsp as I am using ${parameterValues['expiring_days']. In the controller I have the following code.


    @RequestMapping(value="/reports/{reportId}/{masterName}", method=RequestMethod.GET)
        public String showReportParameters(ModelMap model,@PathVariable int reportId,@PathVariable String masterName) {
            List<MasterSheetEntity> listBasedOnParameters = repo.fetchByParameterName(username, reportId, masterName);
            Map<String, String> allParameters = new HashMap<>();
            for (MasterSheetEntity eachParameter : listBasedOnParameters) {
                allParameters.put(eachParameter.getParamName(), eachParameter.getParamValue());
            }
            model.addAttribute("parameterValues", allParameters);
            return "reports";
        }

I am using Jquery. I also tried an ajax call to the above method.


    @RequestMapping(value="/reports/{reportId}/{masterName}", method=RequestMethod.GET)
            public Map<String, String> showReportParameters(ModelMap model,@PathVariable int reportId,@PathVariable String masterName) {
                List<MasterSheetEntity> listBasedOnParameters = repo.fetchByParameterName(username, reportId, masterName);
                Map<String, String> allParameters = new HashMap<>();
                for (MasterSheetEntity eachParameter : listBasedOnParameters) {
                    allParameters.put(eachParameter.getParamName(), eachParameter.getParamValue());
                }
                model.addAttribute("parameterValues", allParameters);
                return allParameters;
            }

.js

    $.get(
            `${reportId}/${masterName}`,
            function(data){
                var parameterValues=data;
                $('.form-parameters').data(data);
                console.log(data);
                $('#parameterLavel').val(pgName);
            }
        )

I am able to see the data in the console. But I am not sure on how to render the response in the HTML.

Please share your inputs.

I have the data in this format.

{expiring_days: "10", Exceeded_by_Days: "", school: "20", area: "1:2:3:4:5:6:7:8:0", …}

swetha0610
  • 29
  • 1
  • 9
  • here instead of `.data(data)` should be `$('.form-parameters').html(data)` – Swati Jun 19 '20 at 03:54
  • thanks @Swati . How do I render that 'data' in the html input fields based on key? – swetha0610 Jun 19 '20 at 13:07
  • if json data is return you need to parse it using `var datas =JSON.parse(data);` and then to get particular value using keyname i.e : `datas.expiring_days` will give you `10`.Also have a look on more details [here](https://stackoverflow.com/questions/14028259/json-response-parsing-in-javascript-to-get-key-value-pair) – Swati Jun 19 '20 at 13:27

0 Answers0