I am trying to call a GetParam method
from another controller within the same Controller class
to refresh the page after some set of operations. This is what I have tried.
@GetMapping("/inventories")
public String showInventories(Model model) {
model.addAttribute("inventories", inventoriesService.getUserInventory());
return "user_inventories";
}
@PostMapping(value = "create_inventory_record", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> createInventoryRecord(@RequestBody MultiValueMap<String, String> inventoryRecord)
throws Exception {
InventoryRecord newInventory = new InventoryRecord(
0,
inventoryRecord.getFirst("addProductType"),
inventoryRecord.getFirst("addProductDescription"),
inventoryRecord.getFirst("addProductCategory")
);
// Response HashMap
HashMap<String, String> inventoryCreationResponse = inventoryService
.createInventoryRecord(newInventory);
boolean inventoryCreated = Boolean.parseBoolean(limitCreationResponse.get("ItemCreated"));
boolean inventoryItemExists = Boolean.parseBoolean(limitCreationResponse.get("ItemAlreadyExists"));
// Check if item created/stored
if (!inventoryCreated) {
// Check if item already exists
if (!inventoryItemExists) {
// Throw exception if Inventory didn't exist but still record wasn't created
return ResponseEntity.unprocessableEntity().body("Inventory item not created!!");
} else {
// Inventory already exists, advice user on front-end
return ResponseEntity.ok(inventoryCreationResponse);
}
} else {
// Refresh inventories page
return ResponseEntity.status(HttpStatus.FOUND).location(URI.create("inventories")).build();
}
}
The showInventories(Model model)
method shows/loads the inventory when the page is loaded. I have a button within the page that shows a form to get inputs for the inventory details. I then send a POST request to the springboot controller. The data is received and the whole process succeeds ie, the data is stored in the database, I get a response on whether the record was created, if not it will notify for existing similar record, if not throw an exception.
The controller that receives the data before processing is of type ResponseEntity<?>
, the one named createInventoryRecord
. After checking for record creation, and if record exists, in case the record was stored successfully, I want to call the showInventories
method with the @GetMapping("inventories")
.
I have tried using ResponseEntity as below to try calling that path but it does not work. My problem is.
return ResponseEntity.status(HttpStatus.FOUND).location(URI.create("inventories")).build();
There is something I am missing, where am I going wrong, what should I do, or do better?
尝试使用redirect:/
返回,如下所示:
return "redirect:/inventories";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.