return model object from spring controller -
i have spring controller in curd methods 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 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 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 working fine except update method because when try object db being fetched before rendering listemployees method being called again assigning blank object rendering.
my question why listemployee method being called again when returning redirect:/ update method object in map. please help.
when update method finished, send http redirect response client. so, instead of giving him web page, send him request redirect / index page.
now clients browser send request /, , respond list of employees.
makes sense?
this referred post-redirect-get pattern, described here
Comments
Post a Comment