I am learning spring mvc rest programming and got stuck at the below issue.
I have an item page in JSP which is used for adding a new item as well as editing an item. What I did here is (not sure if this is the right approach), I have a controller method
@RequestMapping(value = "/item/{id}", method = RequestMethod.GET)
public String addEditItem(Model model, Principal principal, @PathVariable("id") String id) {
model.addAttribute("id", id);
model.addAttribute("item", new Item());
if (Integer.parseInt(id) != 0) {
model.addAttribute("edit", true);
} else {
model.addAttribute("edit", false);
}
System.out.println(model.toString());
return "item";
}
which determines if it is an add/ edit call and accordingly sets the value of booleanedit.
Below given is my item.jsp page.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript">
var itemurl = "";
function onDelete(event) {
var doDelete = confirm("Are you sure you want to delete this offer?");
if (doDelete == false) {
event.preventDefault();
}
}
function onReady() {
$("#delete").click(onDelete);
}
$(document).ready(onReady);
function updateItemDetails(item) {
$("#itemName").val(item.itemName);
$("#taxPercentage").val(item.taxPercentage);
$("#price").val(item.price);
$("#count").val(item.count);
}
function updatePage(itemurl) {
$.getJSON("<c:url value='${itemurl}'/>", updateItemDetails);
}
</script>
<c:if test="${edit}">
<c:set var="itemurl" value="/getitem/${id}"></c:set>
<c:out value="${itemurl}" />
<script type="text/javascript">
updatePage(itemurl);
</script>
</c:if>
<sf:form method="put"
action="${pageContext.request.contextPath}/saveitem" commandName="item">
<sf:input type="hidden" name="itemId" path="itemId" />
<table class="formtable">
<tr>
<td class="label">Item Name :</td>
<td><sf:input class="control" path="itemName" name="itemName"
id="itemName" type="text" />
<div class="error">
<sf:errors path="itemName"></sf:errors>
</div></td>
</tr>
<tr>
<td class="label">Tax Percentage :</td>
<td><sf:input class="control" path="taxPercentage"
name="taxPercentage" id="taxPercentage" type="text" />
<div class="error">
<sf:errors path="taxPercentage"></sf:errors>
</div></td>
</tr>
<tr>
<td class="label">Price :</td>
<td><sf:input class="control" path="price" name="price"
id="price" type="text" />
<div class="error">
<sf:errors path="price"></sf:errors>
</div></td>
</tr>
<tr>
<td class="label">Count :</td>
<td><sf:input class="control" path="count" name="count"
id="count" type="text" />
<div class="error">
<sf:errors path="count"></sf:errors>
</div></td>
</tr>
<tr align="center">
<td colspan="2"><input class="control" value="Save offer"
type="submit" /> <c:if test="${item.itemId != 0}">
<input name="delete" class="control" value="Remove offer"
type="submit" id="delete" />
</c:if></td>
</tr>
</table>
</sf:form>
Above the form I have added a <c:out> to see what url is being formed.
The rest method being used is,
@RequestMapping(value = "/getitem/{id}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Item getItem(@PathVariable("id") String id) {
Item item = itemsService.getItem(Integer.parseInt(id));
return item;
}
The issue is that, if i make a url request with a particular id, in the <c:out value="${itemurl}" /> the correct url is shown, but the form is poulated with item details from the previous url request.
What could be the reason? Could you point out what i'm doing wrong here?