Java Spring MVC get/post in same JSP -


this controller..

    @requestmapping(value = "/add", method = requestmethod.get) public string add(model model) {     return "add"; }  @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);     return "add"; } 

this jsp view..this adding @ moment...this add.jsp..so posts itself

    <form method="post" action="/spring/krams/edit/add"> linna nimi <input type="text" name="name"> <input type="submit" value="test" name="submit" /> </form> 

i jsp file change , when post file, says..."city added" . possible?

what updating city??

    @requestmapping(value = "/update", method = requestmethod.post) public string updated(@requestparam("city") int city_id,                       @requestparam("text") string name,                       model model) {     service.updatecity(name, city_id);     return "update"; } 

here no object?

in post method can add attribute 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 can check if attribute city null or not (with tag <c:if/>). in case not null, because has been added model, can show whatever want. ${city.city} jstl expression accesses city attribute , call getter access city property of 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 need different messages depending on update/create operations can this: (in example update done when id param not null because id identifier of city 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 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> 

Comments