Mahesh
Mahesh

Reputation: 35

how to return multiple values from controller to view

Below is my controller code:

@RequestMapping(value = "/productProgramsDataTable", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public ResponseEntity<List<ProductsDataTableItemDto>> getInitiaorTasks(String productStatusType, String productStatusForEachProduct) {
    final long l1 = System.currentTimeMillis();
    ResponseEntity<List<ProductsDataTableItemDto>> retDetail = null;
    List<ProductsDataTableItemDto> ppdList = null;
    List<String> statusVar = null;
    try {
      ppdList = programPlannerListService.productPrograms();
      statusVar.add(productStatusType);
      statusVar.add(productStatusForEachProduct);
    } catch (final Exception e) {
      e.printStackTrace();
    }
    retDetail = new ResponseEntity<>(ppdList,HttpStatus.OK);
    return retDetail;
  }

Along with retDetail I want to return productStatusType and productStatusForEachProduct

How is it possible to return these values to jQuery?

Upvotes: 1

Views: 1541

Answers (2)

Phat H. VU
Phat H. VU

Reputation: 2370

In you Controller, you can add data that be accessed from JSP/JS to Model model

model.addAttribute("productStatusForEachProduct", productStatusForEachProduct);
model.addAttribute("productStatusType", productStatusType);
model.addAttribute("retDetail", ppdList);

Then, in your JSP

<script>
    var productStatusForEachProduct = "${productStatusForEachProduct}"; 
    var productStatusType = "${productStatusType}"; 
    var retDetail= "${retDetail}"; 
</script>

Upvotes: 2

SamCle88
SamCle88

Reputation: 275

Try to create a bean containing all the objects you need to return, with a getter and a setter for each of them, so that in the method you can set all of them and get them whenever you need them. Something of the kind

public class Container{
    ResponseEntity<List<ProductsDataTableItemDto>> retDetail;
    String productStatusType;
    String productStatusForEachProduct;

    // Getters and setters
}

So that you can do:

Container result = new Container();
result.setRetDetail(retDetail);
result.setProductStatusType(productStatusType);
result.setProductStatusForEachProduct(productStatusForEachProduct);
return result;

Then change return type to Container.

Another possibility is putting a constructor with the three fields in the class Container and use it to create the result.

Upvotes: 0

Related Questions