Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring MVC get/post in same JSP

this is from my 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 is my JSP view..this is only for adding at the moment...this is add.jsp..so it posts back to 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 would like the JSP file change , so that when i post it to this file, then it says..."CITY ADDED" . is that possible?

WHAT ABOUT UPDATING A 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 is no object?

like image 664
Jaanus Avatar asked Apr 29 '11 16:04

Jaanus


1 Answers

In the post method you can add an attribute with 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 the JSP you can check if the attribute city is null or not (with the tag <c:if/>). In case it is not null, it is because it has just been added to the model, so you can show whatever you want. ${city.city} its just a JSTL expression which accesses to the city attribute and then call the getter to access to the city property of that 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 update/create operations you can do this: (In the example an update is done when the id param is not null because the id 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 the 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>
like image 95
Javi Avatar answered Oct 09 '22 16:10

Javi