Spring MVC and Ajax - spring

I m trying to write spring mvc application with ajax and jquery and stuck at some point and not able to find solution since last 1 week..please help me out.
i want send request for showing all my users in database using ajax,see how i done
Show User List
now javascript ajax request
function doAjaxPost(id)
{ var submitId=id;
if(submitId=="showUserList")
{
$.ajax({
type: "POST",
url: "showUserList.html",
success: function(response){
// we have the response
if(response!='')
{
//alert(response);
//Not getting this $('#listofUser').html(response);
$("#homeDiv").hide();
$("#userListDiv").show("5");
}
},
error: function(e){
alert('Error: ' + e);
}
});
}
}
now see me controller
#RequestMapping(value="/showUserList",method = RequestMethod.POST)
public #ResponseBody ModelMap showUserList()
{
System.out.println("Ajax Request For user List");
List<UserDetails> userList=service.getAllUser();
ModelMap model=new ModelMap();
model.put("users", userList);
return model;
}
now see how i m displaying the user list on jsp page
<div id="listofUser">
<c:if test="${!empty users}">
<table id="rounded-corner" summary="List Of System Users">
<thead>
<tr>
<th scope="col" class="rounded-select">Select</th>
<th scope="col" class="rounded-id">ID</th>
<th scope="col" class="rounded-fname">Name</th>
<th scope="col" class="rounded-gender">Gender</th>
<th scope="col" class="rounded-username">UserName</th>
<!-- <th scope="col" class="rounded-password">Password</th> -->
<th scope="col" class="rounded-usertype">Type</th>
<th scope="col" class="rounded-userStatus">Status</th>
<th scope="col" class="rounded-Email">Email</th>
<th scope="col" class="rounded-address">Address</th>
<th scope="col" class="rounded-phno">PhoneNo.</th>
</tr>
</thead>
<form action="adminOperations.html" name="userListForm" onsubmit="return ConfirmOperation();" method="post">
<tbody>
<c:forEach items="${users}" var="users" varStatus="status">
<tr>
<td><input type="checkbox" value="${users.id}" id="select" name="select"/></td>
<td><c:out value="${users.id}" /></td>
<td><c:out value="${users.firstName}" /> <c:out value="${users.lastName}" /></td>
<td><c:out value="${users.gender}" /></td>
<td><c:out value="${users.userName}" /></td>
<%-- <td><c:out value="${users.userPassword}" /></td> --%>
<td><c:out value="${users.userType}" /></td>
<td><c:out value="${users.status}" /></td>
<td><c:out value="${users.emailId}" /></td>
<td><c:out value="${users.address}" /></td>
<td><c:out value="${users.contactNo}" /></td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td colspan="12" class="rounded-foot-center" align="center">
<input type="submit" name="activate" id="activate" value="Activate" onClick="ButtonClicked(this);">
<input type="submit" name="deactivate" id="deactivate" value="Deactivate" onClick="ButtonClicked(this);">
<input type="submit" name="update" id="update" value="Update" onclick="ButtonClicked(this);">
<input type="submit" name="delete" id="delete" value="Delete" onclick="ButtonClicked(this);">
</td>
</tr>
</tfoot>
</form>
</table>
<br/>
</c:if>
<c:if test="${empty users}">
There are currently no user found.
</c:if>
</div>
.......................................................................
what i m not getting is , when response come from controller(ModelMap object in my case)
how would i pass model containing list of users to my jsp page so that it can display.
i tried
$('#listofUser').html(response); but it is not working,
i want model containing (model.put("users", userList);) users to be available from javascript to my display part..
please help me out.....
thanks and regards,
Hemant Singh

i am using spring mvc
public ModelAndView showUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("GiveAnyName","users", userList);
}
Now you can get User List in JSP Page
List rows = (List) request.getAttribute("users");

#ResponseBody doesn't work like that, you should return a JSON and in order to display the content you can do it with jQuery but is a bit tricky, that's why I recommend to you AngularJS, forget JSTL.
Here there is a tutorial where SpringMVC, JSTL and asynchronous requests work together

Related

java.lang.IllegalArgumentException: Attribute 'items' must be an array, a Collection or a Map

i am trying to make simple spring mvc application, using jdbctemplate,but when i try to open registration page i got this error- java.lang.IllegalArgumentException: Attribute 'items' must be an array, a Collection or a Map.
last time i used this thing in another applicaton, it worked fine,but this time it is not working :(
here is my controller code and register.jsp
#RequestMapping("/register")
public ModelAndView registerEmployee(#ModelAttribute Employee employee) {
List<String> cityList = new ArrayList<String>();
cityList.add("kashipur");
cityList.add("moradabad");
cityList.add("delhi");
cityList.add("noida");
List<String> genderList = new ArrayList<String>();
genderList.add("male");
genderList.add("female");
Map<String,List<String>> map = new HashMap<String,List<String>>();
map.put("cityList",cityList);
map.put("genderList",genderList);
return new ModelAndView("register","map",map);
}
register.jsp is
<div>
<form:form method="post" action="/insert" modelAttribute="employee">
<table>
<tr>
<td>Name :</td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Gender :</td>
<td><form:radiobuttons path="gender" items="${map.genderList}" /></td>
</tr>
<tr>
<td>City :</td>
<td><form:select path="city" items="${map.cityList}" /></td>
<tr>
<tr>
<td>Email :</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>Phone :</td>
<td><form:input path="phone" /></td>
</tr>
<tr>
<td><input type="submit" value="Save" /></td>
</tr>
<tr>
<td colspan="2"><a href="getList">Click Here to See User
List</a></td>
</tr>
</table>
</form:form>
</div>
Try to change this List<String> cityList
to this ArrayList<String> cityList
same here
Map<String,List<String>> map = new HashMap<String,List<String>>();
Also try to access map element like this map['cityList']
You don't need to qualify with ${map.
Simply write
return new ModelAndView("register", map);
and in the JSP:
<td><form:radiobuttons path="gender" items="${genderList}" /></td>
and
<td><form:select path="city" items="${cityList}" /></td>

Image upload using Hibernate inside PostgreSQL

I am working on a Spring-MVC application and I must use image upload for storing images in Database. I tried to go through examples on internet, but my overall observation is there is some argument regarding type of upload, and who manages it. Nevertheless, I am uploading the image from an HTML, saving it in Database as OID, using byte array with #Lob to save. I am unable to see the image preview when i use :
I will post code of how I have achieved it, kindly let me know whats wrong.
HTML code for image preview :
<img src="${product.productimage}" width="100" height="100"/>
When I use product.getImage().toString in service, I get weird characters by 'B[jlkisf12', but only 12-13 of them, I imagine that is the path.
Entity :
#Column(name = "productimage")
byte[] productimage; // With getters and setters
JSP file :
<c:url var="add" value="/product/add"></c:url>
<form:form action="${add}" commandName="product">
<table>
<c:if test="${!empty product.productname}">
<tr>
<td>
<form:label path="productid">
<spring:message text="productid"/>
</form:label>
</td>
<td>
<form:input path="productid" readonly="true" size="8" disabled="true" />
<form:hidden path="productid" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="productname">
<spring:message text="productname:"/>
</form:label>
</td>
<td>
<form:input path="productname"/>
</td>
</tr>
<tr>
<td>
<form:label path="productdescription">
<spring:message text="productdescription"/>
</form:label>
</td>
<td>
<form:input path="productdescription"/>
</td>
</tr>
<tr>
<td>
<form:label path="productcondition">
<spring:message text="productcondition"/>
</form:label>
</td>
<td>
<form:input path="productcondition"/>
</td>
</tr>
<tr>
<td>
<form:label path="productage">
<spring:message text="productage"/>
</form:label>
</td>
<td>
<form:input path="productage" />
</td>
</tr>
<tr>
<td>
<form:label path="productean">
<spring:message text="productean"/>
</form:label>
</td>
<td>
<form:input path="productean" />
</td>
</tr>
<tr>
<td>
<form:label path="productisbn">
<spring:message text="productisbn"/>
</form:label>
</td>
<td>
<form:input path="productisbn" />
</td>
</tr>
<tr>
<td>
<form:label path="ordernumber">
<spring:message text="ordernumber"/>
</form:label>
</td>
<td>
<form:input path="ordernumber" />
</td>
</tr>
<tr>
<td>
<form:label path="productimage">
<spring:message text="productimage"/>
</form:label>
</td>
<td>
<form:input type="file" path="productimage" />
</td>
</tr>
</table>
<tr>
<td colspan="2">
<c:if test="${!empty product.productname}">
<input type="submit"
value="<spring:message text="Edit Product"/>" />
</c:if>
<c:if test="${empty product.productname}">
<input type="submit"
value="<spring:message text="Add Product"/>" />
</c:if>
</td>
</tr>
</form:form>
<br>
<h3>Product List</h3>
<c:if test="${!empty listProducts}">
<table class="tg">
<tr>
<th width="80">Product ID</th>
<th width="80">Product image</th>
<th width="120">Product name</th>
<th width="120">Product description</th>
<th width="120">Product condition</th>
<th width="120">Product age</th>
<th width="120">Product EAN</th>
<th width="120">Product ISBN</th>
<th width="120">Product ordernumber</th>
<th width="120">Product owners id</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
<c:forEach items="${listProducts}" var="product">
<tr>
<td>${product.productid}</td>
<td>${product.productimage} </td>
<td>${product.productname}</td>
<td>${product.productdescription}</td>
<td>${product.productcondition}</td>
<td>${product.productage}</td>
<td>${product.productean}</td>
<td>${product.productisbn}</td>
<td>${product.ordernumber}</td>
<td>${product.user1id}</td>
<img src="${image}" width="100" height="100"/> //image not found
<td><a href="<c:url value='/editproduct/${product.productid}' />" >Edit Product</a></td>
<td><a href="<c:url value='/removeproduct/${product.productid}' />" >Delete Product</a></td>
</tr>
<img src="${product.saveimage}" height="100" width="100">
</c:forEach>
</table>
</c:if>
Controller :
#RequestMapping(value="/product/show",method= RequestMethod.GET)
public String listProducts(#ModelAttribute("product") ProductBasic productBasic,Model model) {
User user = userService.getCurrentlyAuthenticatedUser();
model.addAttribute("product", new ProductBasic());
model.addAttribute("listProducts",this.productBasicService.listProduct(user));
BASE64Encoder base64Encoder = new BASE64Encoder();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("data:image/png;base64,");
stringBuilder.append(base64Encoder.encode(productBasic.getProductimage()));
String image = stringBuilder.toString();
model.addAttribute("saveimage",image);
return "product";
}
#RequestMapping(value="/product/add")
public String addProduct(#ModelAttribute("product") ProductBasic productBasic,Model model){
User user = userService.getCurrentlyAuthenticatedUser();
model.addAttribute("product", new ProductBasic());
productBasicService.addProduct(user,productBasic);
return "redirect:/product/show";
}
#RequestMapping("/removeproduct/{id}")
public String removeProduct(#PathVariable("id") int productid,Model model) {
User user = userService.getCurrentlyAuthenticatedUser();
model.addAttribute("listProducts",this.productBasicService.listProduct(user));
this.productBasicService.removeProduct(productid,user);
return "redirect:/product/show";
}
#RequestMapping("/editproduct/{id}")
public String updateProduct(#ModelAttribute("product") ProductBasic productBasic,#PathVariable("id") Integer id,Model model){
User user = userService.getCurrentlyAuthenticatedUser();
model.addAttribute("product", this.productBasicService.getProductById(id));
model.addAttribute("listProducts",this.productBasicService.listProduct(user));
return "product";
}
NullPointerException:
java.lang.NullPointerException
java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:106)
sun.misc.CharacterEncoder.encode(CharacterEncoder.java:188)
com.WirTauschen.UserController.listProducts(UserController.java:67)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
What am I doing wrong? Kindly let me know.
In your Controller or method add below code for image
BASE64Encoder base64Encoder = new BASE64Encoder();
StringBuilder imageString = new StringBuilder();
imageString.append("data:image/png;base64,");
imageString.append(base64Encoder.encode(bytes)); //bytes will be image byte[] come from DB
String image = imageString.toString();
modelView.put("imagetoDisplay",image);// put in your model object for using in your JSP
And in your JSP
<img src="${imagetoDisplay}" width="100" height="100"/>

Spring-MVC model updating only after page refresh

I am working on a spring-mvc project which adds notes using a JSP file. Right now, all CRUD operations work in the project. The only problem is to see the updated information in the database, I have to hit refresh browser button everytime. I checked flashattributes, but as I am not redirecting, just returning the object, I don't find that useful. I am posting my controller code along with JSP code. Any ideas why I need to hit refresh.
P.S : Refresh is not necessary for delete function, and yes, I tried using redirect already for other functions.
COntroller code :
#RequestMapping(value= "/note/add")
public String addNote(#ModelAttribute("notices") Notes p,Model model) {
Person person = personService.getCurrentlyAuthenticatedUser();
model.addAttribute("notices", new Notes());
model.addAttribute("listNotes",this.notesService.listNotes());
model.addAttribute("listNotes", this.notesService.listNotePerson(person));
model.addAttribute("section1",this.notesService.listNotesBySectionId(1,person));
model.addAttribute("section2",this.notesService.listNotesBySectionId(2,person));
model.addAttribute("section3",this.notesService.listNotesBySectionId(3,person));
}
#RequestMapping("/editnote/{id}")
public String editNote(#PathVariable("id") Integer id, Model model){
Person person = personService.getCurrentlyAuthenticatedUser();
model.addAttribute("notices", this.notesService.getNoteById(id));
model.addAttribute("section1",this.notesService.listNotesBySectionId(1,person));
model.addAttribute("section2",this.notesService.listNotesBySectionId(2,person));
model.addAttribute("section3",this.notesService.listNotesBySectionId(3,person));
}
#RequestMapping(value = "/note/listing", method=RequestMethod.GET)
public String listNotices(Model model) {
Person person = personService.getCurrentlyAuthenticatedUser();
model.addAttribute("notices",new Notes());
model.addAttribute("listNotes", this.notesService.listNotePerson(person));
model.addAttribute("section1",this.notesService.listNotesBySectionId(1,person));
model.addAttribute("section2",this.notesService.listNotesBySectionId(2,person));
model.addAttribute("section3",this.notesService.listNotesBySectionId(3,person));
}
JSP code :
<c:url var="addAction" value="/note/add" ></c:url>
<form:form action="${addAction}" commandName="notices">
<table>
<c:if test="${!empty notices.notetext}">
<tr>
<td>
<form:label path="noticesid">
<spring:message text="noticesid"/>
</form:label>
</td>
<td>
<form:input path="noticesid" readonly="true" size="8" disabled="true" />
<form:hidden path="noticesid" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="notetext">
<spring:message text="notetext"/>
</form:label>
</td>
<td>
<form:input path="notetext" />
</td>
</tr>
<tr>
<td>
<form:label path="notetag" >
<spring:message text="notetag"/>
</form:label>
</td>
<td>
<form:input path="notetag"/>
</td>
</tr>
<tr>
<td>
<form:label path="notecolor">
<spring:message text="notecolor"/>
</form:label>
</td>
<td>
<form:input path="notecolor" />
</td>
</tr>
<tr>
<td>
<form:label path="canvasid">
<spring:message text="canvasid"/>
</form:label>
</td>
<td>
<form:input path="canvasid" />
</td>
</tr>
<tr>
<td>
<form:label path="sectionid">
<spring:message text="sectionid"/>
</form:label>
</td>
<td>
<form:input path="sectionid" />
</td>
</tr>
<tr>
<td>
<form:label path="canvasnName">
<spring:message text="canvasnName"/>
</form:label>
</td>
<td>
<form:input path="canvasnName" />
</td>
</tr>
<tr>
<td colspan="2">
<c:if test="${!empty notices.noticesid}">
<input type="submit"
value="<spring:message text="Edit note"/>" />
</c:if>
<c:if test="${empty notices.notetext}">
<input type="submit"
value="<spring:message text="Add note"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
<br>
<h3>Notes List</h3>
<c:url var="listAction" value="/note/listing" ></c:url>
<form:form action="${section1}" commandName="notices" method="post"> // Same code with section2 and section3
<c:if test="${!empty notices.noticesid}">
<table class="tg">
<tr>
<th width="80">Notes ID</th>
<th width="120">Notes text</th>
<th width="120">Note Tag</th>
<th width="120">Note color</th>
<th width="120">Note section</th>
<th width="120">Canvas id</th>
<th width="120">Canvas name</th>
<th width="120">Other id</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
<c:forEach items="${section1}" var="notices">
<tr>
<td>${notices.noticesid}</td>
<td>${notices.notetext}</td>
<td>${notices.notetag}</td>
<td>${notices.notecolor}</td>
<td>${notices.sectionid}</td>
<td>${notices.canvasid}</td>
<td>${notices.canvasnName}</td>
<td>${notices.personid}</td>
<td><a href="<c:url value='/editnote/${notices.noticesid}' />" >Edit</a></td>
<td><a href="<c:url value='/removenote/${notices.noticesid}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</form:form>
So I was able to solve it finally by calling return "redirect:/note/listing" at end of/note/add function.
To be completely clear on this answer, simply create:
ModelAndView controller, and at the end of your controller, please, add:
ModelAndView mv = new ModelAndView("redirect:/note/listing");
return mv;

JSP page not calling function with correct paramters

I am working on a Spring application which has a JSP page. In one single JSP page, I have 3 actions. Out of them 2 actions don't require any parameter and the third one has a static parameter(/note/list/1234), 1234 being the static parameter.
If I call any of the 2 functions, the third function should use the static parameter and execute. Instead of that it calls /note/list/0. I have no idea why. I am posting the JSP file below. Kindly have a look.
Note.JSP
<h1>
Add notes
</h1>
<c:url var="addAction" value="/note/add" ></c:url>
<form:form action="${addAction}" commandName="notices">
<table>
<c:if test="${!empty notices.notetext}">
<tr>
<td>
<form:label path="noticesid">
<spring:message text="noticesid"/>
</form:label>
</td>
<td>
<form:input path="noticesid" readonly="true" size="8" disabled="true" />
<form:hidden path="noticesid" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="notetext">
<spring:message text="notetext"/>
</form:label>
</td>
<td>
<form:input path="notetext" />
</td>
</tr>
<tr>
<td>
<form:label path="notetag" >
<spring:message text="notetag"/>
</form:label>
</td>
<td>
<form:input path="notetag"/>
</td>
</tr>
<tr>
<td>
<form:label path="notecolor">
<spring:message text="notecolor"/>
</form:label>
</td>
<td>
<form:input path="notecolor" />
</td>
</tr>
<tr>
<td>
<form:label path="canvasid">
<spring:message text="canvasid"/>
</form:label>
</td>
<td>
<form:input path="canvasid" />
</td>
</tr>
<tr>
<td>
<form:label path="sectionid">
<spring:message text="sectionid"/>
</form:label>
</td>
<td>
<form:input path="sectionid" />
</td>
</tr>
<tr>
<td>
<form:label path="canvasnName">
<spring:message text="canvasnName"/>
</form:label>
</td>
<td>
<form:input path="canvasnName" />
</td>
</tr>
<tr>
<td colspan="2">
<c:if test="${!empty notices.noticesid}">
<input type="submit"
value="<spring:message text="Edit note"/>" />
</c:if>
<c:if test="${empty notices.notetext}">
<input type="submit"
value="<spring:message text="Add note"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
<br>
<h3>Notes List</h3>
<c:url var="listAction" value="/note/listing" ></c:url>
<form:form action="${listNotes}" commandName="notices" method="post">
<c:if test="${!empty notices.noticesid}">
<table class="tg">
<tr>
<th width="80">Notes ID</th>
<th width="120">Notes text</th>
<th width="120">Note Tag</th>
<th width="120">Note color</th>
<th width="120">Note section</th>
<th width="120">Canvas id</th>
<th width="120">Canvas name</th>
<th width="120">Other id</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
<c:forEach items="${listNotes}" var="notices">
<tr>
<td>${notices.noticesid}</td>
<td>${notices.notetext}</td>
<td>${notices.notetag}</td>
<td>${notices.notecolor}</td>
<td>${notices.sectionid}</td>
<td>${notices.canvasid}</td>
<td>${notices.canvasnName}</td>
<td>${notices.personid}</td>
<td><a href="<c:url value='/editnote/${notices.noticesid}' />" >Edit</a></td>
<td><a href="<c:url value='/removenote/${notices.noticesid}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</form:form>
<c:url var="listAction" value="/note/list/2323" ></c:url> // This method here calls /note/list/0
<form:form action="${listNotes}" commandName="notices" method="post">
<c:if test="${!empty notices.noticesid}">
<table class="tg">
<tr>
<th width="80">Notes ID</th>
<th width="120">Notes text</th>
<th width="120">Note Tag</th>
<th width="120">Note color</th>
<th width="120">Note section</th>
<th width="120">Canvas id</th>
<th width="120">Canvas name</th>
<th width="120">Other id</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
<c:forEach items="${listNotes}" var="notices">
<tr>
<td>${notices.noticesid}</td>
<td>${notices.notetext}</td>
<td>${notices.notetag}</td>
<td>${notices.notecolor}</td>
<td>${notices.sectionid}</td>
<td>${notices.canvasid}</td>
<td>${notices.canvasnName}</td>
<td>${notices.personid}</td>
<td><a href="<c:url value='/editnote/${notices.noticesid}' />" >Edit</a></td>
<td><a href="<c:url value='/removenote/${notices.noticesid}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</form:form>
</body>
</html>
Controller :
#RequestMapping(value = "/note/list/{id}", method=RequestMethod.GET)
public String listNotes(#PathVariable int id,Model model) {
System.out.println("Did we reach in noteslist");
System.out.println("Section id is"+id); // returns zero on first execution unless called manually with /note/list/{number}
Person person = personService.getCurrentlyAuthenticatedUser();
model.addAttribute("person", new Person());
model.addAttribute("listPersons", this.personService.listPersons());
model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person));
return "note";
}
What can I do that it will get called with the proper URL i.e /note/list/1234 even if I call /note/add. Any help would be nice. Thank you.

What should be the value of #ModelAttribute when handling POST in Spring MVC?

I'm learning Spring MVC with the tutorial of the site here
My question is basically in the code fragment bellow.
#RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(#ModelAttribute("contact")
Contact contact, BindingResult result) {
System.out.println("First Name:" + contact.getFirstname() +
"Last Name:" + contact.getLastname());
return "redirect:contacts.html";
}
What should the value of #ModelAttribute be when just handling a POST request from a form submit?
Or in other words, what exactly does the "contact" value refers to in this situation?
The posted form is defined as follows:
<form:form method="post" action="addContact.html">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Last Name</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Email</form:label></td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><form:label path="lastname">Telephone</form:label></td>
<td><form:input path="telephone" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Contact"/>
</td>
</tr>
</table>
</form:form>

Resources