Spring-MVC model updating only after page refresh - spring

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;

Related

In Spring MVC web application how to retain form SELECT elements between requests?

I'm trying to implement a Spring MVC web application without Ajax.
Currently have a form with a set of cascading dropdowns. On change of subsequent select (dropdowns) elements how to retain the item list of previously selected dropdown and also simultaneously clearing the item lists of subsequent dropdown elements and resetting the values of the dropdown elements to null or default. At present I'm putting the list items fetched from the database into the ModelAndView object. Doing this way the dropdown items are not being retained in the next request. Is it a good practice to put the such cascading dropdown list items in HttpSession object? What is the recommended & efficient way to handle such a page flow in Spring MVC?
Following is the Controller request handler method :-
#RequestMapping(value="/add.do",method=RequestMethod.GET,params="actionMethod=beginAddBldg")
public ModelAndView beginAddBuilding(#ModelAttribute("addBuildingFormBean") Building building) {
ModelAndView mv = new ModelAndView("addBldg");
List<Division> lstOfDivisions=buildingService.getDivisions();
mv.addObject("divisionList", lstOfDivisions);
return mv;
}
And following is the JSP page code :-
<form:form name="addBuildingForm" modelAttribute="addBuildingFormBean" method="POST">
<form:hidden path="actionMethod"/>
<form:hidden path="actionForward"/>
<table border="1" style="border-collapse:collapse;">
<tr>
<th>
<form:label path="divisionID">
Division
</form:label>
</th>
<td>
<form:select path="divisionID" id="divisionID" onchange="getNextListItems(document.addBuildingForm,'getSubdivs','addBldg','/buildings/add.do');">
<form:option value="-1">--Select--</form:option>
<form:options items="${divisionList}" itemLabel="divisionName" itemValue="divisionID"/>
</form:select>
</td>
<th>
<form:label path="subdivisionID">
Subdivision
</form:label>
</th>
<td>
<form:select path="subdivisionID" id="subdivisionID" onchange="getNextListItems(document.addBuildingForm,'getDistrictsList','addBldg','/add.do');">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
</tr>
<tr>
<th>
<form:label path="districtID">
District
</form:label>
</th>
<td>
<form:select path="districtID" id="districtID" onchange="getNextListItems(document.addBuildingForm,'getTaluksList','addBldg','/add.do');">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
<th>
<form:label path="talukID">
Taluk
</form:label>
</th>
<td>
<form:select path="talukID" id="talukID" onchange="getNextListItems(document.addBuildingForm,'getVillagesList','addBldg','/add.do');">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
</tr>
<tr>
<td colspan="4">
<form:checkbox path="isTown" id="isTown"/>
<form:label path="isTown">
List only Towns (villages with population >= 10,000)
</form:label>
</td>
</tr>
<tr>
<th>
<form:label path="villageCode">
Town/Village
</form:label>
</th>
<td>
<form:select path="villageCode" id="villageCode">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
</tr>
<tr>
<th>
<form:label path="yearOfConstID">
Year of Construction
</form:label>
</th>
<td>
<form:select path="yearOfConstID" id="yearOfConstID">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
<th>
<form:label path="buildingTypeID">
Type of Building
</form:label>
</th>
<td>
<form:select path="buildingTypeID" id="buildingTypeID">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
</tr>
<tr>
<th>
<form:label path="buildingName">
Name of Building
</form:label>
</th>
<td>
<form:input path="buildingName" id="buildingName"/>
</td>
</tr>
<tr>
<th>
<form:label path="noOfFloors">
No. of Floors
</form:label>
</th>
<td>
<form:input path="noOfFloors" id="noOfFloors" maxlength="3"/>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</form:form>
You can put them is the session but the previously selected item will not be selected on page refresh unless you actually bind the model i.e. submit the partially completed form on each change.
You can avoid the session and create a method annotated with #ModelAttribute. See http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods and for any request through that controller the list will be populated. This will not avoid the problem of the wrong item being selected but will avoid the use of the session.
A better method that would fix everything is to make an Ajax call which would simply return an HTML fragment containing the updated <select/> i.e. avoiding whole page refresh but you will need to research that.

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"/>

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>

Issue with form:select, all labels are selected

I am using spring 3 JSP tab lib to generate HTML. I have a select box with following code -
<form:select path="categoryList" multiple="false">
<form:option value="" label="--" selected="selected"/>
<form:options items="${categoryList}" itemValue="catId" itemLabel="catName"/>
</form:select>
HTML getting generated is -
<select id="categoryList" name="categoryList">
<option selected="selected" value="">--</option>
<option value="1" selected="selected">S</option>
<option value="2" selected="selected">Ster</option>
<option value="3" selected="selected">ice</option>
<option value="4" selected="selected">Cees</option>
</select>
Issue is all options getting generated has selected="selected" which makes "Cees" selected on page rather then "--". Can someone please let me know ohw to fix this?
complete page -
<form:form modelAttribute="productManagerVO" name="productManager" id="productManager">
<jsp:directive.include file="../common/header.jsp" />
<div class="MainDiv">
<div class="ManagerHeadline">Product Manager</div>
<table cellspacing="2px" cellpadding="0" width="100%">
<tr>
<td width="100%">
<fieldset>
<legend class="checkoutlegend">Category Information</legend>
<table align="left" id="addCategoryTable">
<tr><td><input type="button" id="addCat" value="Add New Category" /></td></tr>
</table>
<c:if test="${not empty productManagerVO.categoryList}">
<table cellspacing="2px" cellpadding="0" class="timeTable" id="categoryTable">
<th>Name</th>
<th>Order</th>
<th>Active</th>
<th> </th>
<c:forEach var="categoryListVO" items="${productManagerVO.categoryList}" varStatus="item">
<tr>
<td><c:out value="${categoryListVO.catName}" /></td>
<td><c:out value="${categoryListVO.catOrder}" /></td>
<td><c:if test="${categoryListVO.categoryActive}">Yes</c:if>
<c:if test="${!categoryListVO.categoryActive}">No</c:if>
</td>
<td>edit</td>
</tr>
</c:forEach>
</table>
</c:if>
</fieldset>
</td>
</tr>
<tr>
<td width="100%">
<fieldset>
<legend class="checkoutlegend">Product Information</legend>
<table align="left" id="addProductTable">
<tr>
<td><input type="button" id="addPro" value="Add New Product"/></td>
</tr>
</table>
<table cellspacing="2px" cellpadding="0" class="timeTable" id="productTable">
<col width="10%" />
<col width="7%" />
<col width="40%" />
<col width="10%" />
<col width="5%" />
<col width="6%" />
<col width="7%" />
<col width="10%" />
<col width="5%" />
<tr>
<th>Name</th>
<th>Subname</th>
<th>Description</th>
<th>Veg</th>
<th>Spicy</th>
<th>Is Active</th>
<th>Price</th>
<th>Category</th>
<th> </th>
</tr>
<c:if test="${not empty productManagerVO.productList}">
<c:forEach var="productListVO" items="${productManagerVO.productList}" varStatus="item">
<tr>
<td><c:out value="${productListVO.fName}" /></td>
<td><c:out value="${productListVO.fSubname}" /></td>
<td><c:out value="${productListVO.fDesc}" /></td>
<td><c:if test="${productListVO.fVeg}">Yes</c:if>
<c:if test="${!productListVO.fVeg}">No</c:if></td>
<td><c:if test="${productListVO.fSpicy}">Yes</c:if>
<c:if test="${!productListVO.fSpicy}">No</c:if></td>
<td><c:if test="${productListVO.fActive}">Yes</c:if>
<c:if test="${!productListVO.fActive}">No</c:if></td>
<td><c:out value="${productListVO.fPrice}" /></td>
<td><c:out value="${productListVO.categoryName}" /></td>
<td><a id="product" href="#editProduct" onclick="editProductData('${productListVO.fId}')">edit</a></td>
</tr>
</c:forEach>
</c:if>
</table>
</fieldset>
</td>
</tr>
</table>
</div>
<div style="display:none">
<div id="addCategory">
<table cellspacing="2px" cellpadding="0" class="adminTable" style="border:1px solid #dadada;">
<tr>
<td style="font-weight:bold;">Category Name</td>
<td><input type="text" id="caddName" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Category Description</td>
<td><input type="text" id="caddDesc" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Category Order</td>
<td><input type="text" id="caddOrder" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Is Active</td>
<td>Yes <input type="radio" value="y" name="catActive"/> No <input type="radio" value="n" name="catActive"/></td>
</tr>
<tr>
<td colspan="2"><input type="button" id="cSave" name="catSave" value="Submit" onclick="addNewCategory();"/></td>
</tr>
</table>
</div>
<div id="editCategory">
<table cellspacing="2px" cellpadding="0" class="adminTable" style="border:1px solid #dadada;">
<tr>
<td style="font-weight:bold;">Category Name</td>
<td><input type="text" id="ceName" name="ceName"/></td>
</tr>
<tr>
<td style="font-weight:bold;">Category Description</td>
<td><input type="text" id="ceDesc" name="ceDesc"/></td>
</tr>
<tr>
<td style="font-weight:bold;">Category Order</td>
<td><input type="text" id="ceOrder" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Is Active</td>
<td><input type="radio" id="ceActiveY" name="cateActive" value="y"/> Yes
<input type="radio" id="ceActiveN" name="cateActive" value="n"/> No</td>
</tr>
<tr><td colspan="2"><input type="button" id="cSave" name="catSave" value="Submit" onClick="editSaveCategory();"/></td>
<td><input type="hidden" id="ceId"/></td>
</tr>
</table>
</div>
<div id="addProduct">
<table cellspacing="2px" cellpadding="0" class="adminTable" style="border:1px solid #dadada;">
<tr>
<td style="font-weight:bold;">Product Name</td>
<td><input type="text" id="paName" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Product SubName</td>
<td><input type="text" id="paSubName" /></td>
</tr>
<tr>
<td style="font-weight:bold;"> Product Description</td>
<td><input type="text" id="paDesc" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Price</td>
<td><input type="text" id="paPrice" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Is Vegetarian</td>
<td>Yes <input type="radio" id="paVeg" name="proVeg" value="y"/> No <input type="radio" id="paVeg" name="proVeg" value="n"/></td>
</tr>
<tr>
<td style="font-weight:bold;">Is Spicy</td>
<td>Yes <input type="radio" id="paSpicy" name="proSpicy" value="y"/> No <input type="radio" id="paSpicy" name="proCpicy" value="n"/></td>
</tr>
<tr>
<td style="font-weight:bold;">Is Active</td>
<td>Yes <input type="radio" id="paActive" name="proActive" value="y"/> No <input type="radio" id="paActive" name="proActive" value="n"/></td>
</tr>
<tr>
<td style="font-weight:bold;">Category</td>
<td>
<form:select path="categoryList" multiple="single">
<form:option value="" label="--"/>
<form:options items="${productManagerVO.categoryList}" itemValue="catId" itemLabel="catName"/>
</form:select>
</td>
</tr>
<tr>
<td colspan="2"><input type="button" id="caSave" name="cataSave" value="Submit"/></td>
</tr>
</table>
</div>
<div id="editProduct">
<table cellspacing="2px" cellpadding="0" class="adminTable" style="border:1px solid #dadada;">
<tr>
<td style="font-weight:bold;">Product Name</td>
<td><input type="text" id="peName" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Product SubName</td>
<td><input type="text" id="peSubName" /></td>
</tr>
<tr>
<td style="font-weight:bold;"> Product Description</td>
<td><input type="text" id="peDesc" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Price</td>
<td><input type="text" id="pePrice" /></td>
</tr>
<tr>
<td style="font-weight:bold;">Is Vegetarian</td>
<td>Yes <input type="radio" id="peVegY" name="proeVeg" value="y"/> No <input type="radio" id="peVegN" name="proeVeg" value="n"/></td>
</tr>
<tr>
<td style="font-weight:bold;">Is Spicy</td>
<td>Yes <input type="radio" id="peSpicyY" name="proeSpicy" value="y"/> No <input type="radio" id="peSpicyN" name="proeSpicy" value="n"/></td>
</tr>
<tr>
<td style="font-weight:bold;">Is Active</td>
<td>Yes <input type="radio" id="peActiveY" name="proeActive" value="y"/> No <input type="radio" id="peActiveN" name="proeActive" value="n"/></td>
</tr>
<tr>
<td style="font-weight:bold;">Category</td>
<td>
<form:select path="categoryList" multiple="single" id="peCat">
<form:option value="" label="--"/>
<form:options items="${productManagerVO.categoryList}" itemValue="catId" itemLabel="catName"/>
</form:select>
</td>
</tr>
<tr><td>
<td colspan="2"><input type="button" id="peSave" name="proeSave" value="Submit" onClick="editSaveProduct();"/></td>
<input type="text" id="peId"/></td>
</tr>
</table>
</div>
</div>
</form:form>
Try removing the selected=selected on your first item. I don't think its necessary as the list will display in the order you specified - unless you want -- to be the default selection if the form is submitted without this select being touched?
Try this:
<form:select path="categoryList" multiple="single">
<form:option value="" label="--"/>
<form:options items="${categoryList}" itemValue="catId" itemLabel="catName"/>
</form:select>
I've only changed the multiple attribute and removed the selected attribute from the option. This works for me...and i think, must also work for you.
edit:
It seems to be okay, only your last <tr> is wrong...you have two <td> in a row, without </td>
<tr>
<td>
<td colspan="2">
<input type="button" id="peSave" name="proeSave" value="Submit" onClick="editSaveProduct();"/>
</td>
<input type="text" id="peId"/>
</td>
</tr>
must be something like
<tr>
<td>
<input type="button" id="peSave" name="proeSave" value="Submit" onClick="editSaveProduct();"/>
</td>
<td colspan="2">
<input type="text" id="peId"/>
</td>
</tr>
Second, why are you using c:out? You can replace <td><c:out value="${categoryListVO.catName}" /></td> with <td>${categoryListVO.catName}</td>...
Third - this construction
<td><c:if test="${productListVO.fActive}">Yes</c:if>
<c:if test="${!productListVO.fActive}">No</c:if></td>
can you replace with
<c:choose>
<c:when test="${productListVO.fActive}">
Yes
</c:when>
<c:otherwise>
No
</c:otherwise>
</c:choose>
I find this way better...the rest seems to be okay, i can't find anything, what can cause the problem. Search for other not properly closed tags. Is there any warnings/messages from the jsp editor?
I had this problem, and after a lot of digging around in Spring I found what was causing it for me. It was something very specific in my case but there is a general point, which is this.
Spring goes to a great deal of trouble to try to work out what the selected option should be. I started digging around in the OptionWriter and then moved down into the SelectedValueComparator. This tries all manner of comparisons to work out if the current option value should be selected. In the end it falls back to the Propertyeditors and Converters (which is where my mistake was).
So I wonder if your CategoryList class had a badly implemented equals method or a converter that doesn't convert properly (mine always converted into the same object)?

Resources