In the post method, you can add an attribute using the addAttribute method
@RequestMapping(value = "/add", method = RequestMethod.POST) public String added(@RequestParam("name") String name, Model model) { City city = new City(); city.setCity(name); service.addCity(city); model.addAttribute("city", city); return "add"; }
and in JSP you can check if the attribute city is null or not (with the <c:if/> ). If it is not null, it is because it has just been added to the model, so you can show whatever you want. ${city.city} is just a JSTL expression that accesses the city attribute and then calls getter to access the city attribute of this attribute:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> <c:if test="${city != null}"> CITY <c:out value="${city.city}" /> ADDED </c:if>
UPDATE
If you need different messages depending on the update / create operations, you can do this: (In the example, the update is performed when the id parameter is not equal to zero, since the identifier is the identifier of the city to update)
@RequestMapping(value = "/add", method = RequestMethod.POST) public String added(@RequestParam(value="id", required=false) String id, @RequestParam("name") String name, Model model) { City city; String operation; if(id== null){ //create operation city = new City(); operation = "CREATE"; }else{ //update operation city = service.findCity(id); operation = "UPDATE"; } city.setCity(name); service.saveCity(city); //save or update model.addAttribute("city", city); model.addAttribute("operation", operation); //add operation param return "add"; }
and in JSP you can do:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> <c:if test="${operation == 'CREATE'}"> <c:if test="${city != null}"> CITY <c:out value="${city.city}" /> ADDED </c:if> <c:if test="${operation == 'UPDATE'}"> CITY <c:out value="${city.city}" /> UPDATED </c:if> </c:if>
source share