0

I have a spring controller in which CURD methods are there.

     @Controller
  public class EditEmployeeController { 

@Autowired
private EmployeeManager employeeManager; 

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String listEmployees(ModelMap map) 
{ 
    map.addAttribute("employee", new TestEmployee());
    map.addAttribute("employeeList", employeeManager.getAllEmployees()); 
    System.out.println("calling listEmployees");
    return "editEmployeeList"; 
} 

@RequestMapping(value = "/add", method = RequestMethod.POST) 
public String addEmployee(@ModelAttribute(value="employee") TestEmployee     employee,          BindingResult result) 
{ 
    employeeManager.addEmployee(employee); 
    return "redirect:/"; 
} 

@RequestMapping("/delete/{employeeId}") 
public String deleteEmplyee(@PathVariable("employeeId") Integer employeeId) 
{ 
    employeeManager.deleteEmployee(employeeId); 
    return "redirect:/"; 
} 

@RequestMapping("/update/{employeeId}")
public String updateEmployee(@PathVariable("employeeId") Integer employeeId, ModelMap map)
{
    TestEmployee emp  = employeeManager.updateEmployee(employeeId);
    map.addAttribute("employee", emp);
    return "redirect:/";

}

jsp page is like this

  <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 
  <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
  <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
  <html> 
  <head> 
  <title>Spring Hibernate Integration</title> 
  </head> 
 <body> 

  <h2>Employee Management Screen</h2> 

 <form:form method="post" action="add" commandName="employee"> 

<table> 
<tr> 
    <td><form:label path="firstname"><spring:message       code="label.firstname"/></form:label></td> 
    <td><form:input path="firstname" /></td> 
</tr> 
<tr> 
    <td><form:label path="lastname"><spring:message code="label.lastname"/></form:label></td> 
    <td><form:input path="lastname" /></td> 
</tr> 
<tr> 
    <td><form:label path="email"><spring:message code="label.email"/></form:label></td> 
    <td><form:input path="email" /></td> 
</tr> 
<tr> 
    <td><form:label path="telephone"><spring:message   code="label.telephone"/></form:label></td> 
    <td><form:input path="telephone" /></td> 
</tr> 
<tr> 
    <td colspan="2"> 
        <input type="submit" value="<spring:message code="label.add"/>"/> 
    </td> 
</tr> 
  </table>  
  </form:form>
  <h3>Employees</h3> 
     <c:if  test="${!empty employeeList}"> 
     <table class="data"> 
     <tr> 
    <th>FirstName</th>
    <th>LastName</th> 
    <th>Email</th> 
    <th>Telephone</th> 
    <th>Action</th> 
      </tr> 
      <c:forEach items="${employeeList}" var="emp"> 
    <tr> 
        <td>${emp.firstname}</td> 
        <td>${emp.lastname}</td>
        <td>${emp.email}</td> 
        <td>${emp.telephone}</td> 
        <td><a href="delete/${emp.id}">Delete</a>|
            <a href="update/${emp.id}">Update</a>
        </td> 
    </tr> 
     </c:forEach> 
    </table> 
    </c:if> 

   </body> 
   </html>

Here is my web.xml file

 <servlet> 
    <servlet-name>employee</servlet-name> 
    <servlet-class> 
        org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>employee</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping>

All functionality is working fine except Update method because when i try to get object from DB it is being fetched but before rendering it listEmployees method is being called which is again assigning blank object for rendering.

My question is why this listEmployee method is being called again when i am returning redirect:/ from update method with object in Map. please help.

rishi
  • 1,792
  • 5
  • 31
  • 63

2 Answers2

0

When your update method is finished, you send a HTTP redirect response to the client. So, instead of giving him a web page, you send him a request to redirect to / which is your index page.

Now the clients browser send a request for /, and you respond with a list of employees.

Makes sense?

This is referred to the POST-redirect-GET pattern, described here

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
  • thanks for reply but I need to redirect to the index page with the object that i have fetched from DB. kindly tell me how should I modify my code. – rishi Mar 06 '14 at 07:17
  • You can do one of two things. Either redirect to a /view/id for a particular view of the employee, or as is more commonly used.. When the update is finished, you store the employee in the session scope. In the index scope, you search session for an employee, and use that one in your view. This second option is a technique called flash scope, where you store for retrieval at a later request. From spring MVC 3.1 there is a particular scope for this http://stackoverflow.com/questions/11763779/how-to-read-flash-attributes-after-redirection-in-spring-mvc-3-1 – Niels Bech Nielsen Mar 06 '14 at 08:28
0

The reason why your listEmployee method gets called because redirect path maps to listEmployee method.

So instead give redirection to another view.

Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70