java-spring-mvc Form values not getting printed on POST -


i new spring mvc created project using springtemplateproject , used hibernate in that.

i created form , on post values getting inserted in database problem not able display values on post.

here code.

//controller

package com.projects.data;  import java.util.locale;  import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.ui.modelmap; import org.springframework.validation.bindingresult; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod;  import com.projects.data.*;  @controller public class homecontroller {  private static final logger logger = loggerfactory.getlogger(homecontroller.class);  @autowired serviceimpl service;   @requestmapping(value = "/", method = requestmethod.get)         public string customer(locale locale, model model) {     logger.info("welcome home! client locale {}.", locale);     return "home"; }  @requestmapping(value = "/customer", method = requestmethod.post)    public string addcustomer(@modelattribute("customer") customer customer,model model) {     service.addcustomer(customer);             return "customer/customer"; } } 

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ page session="false" %> <html> <head> <title>home</title> </head> <body> <spring:url var="customer" value="/customer"/> <form:form action="${customer}" method="post" modelattribute="customer" commandname="custform"> <form:label path="custid">id:</form:label> <form:input path="custid"/> <br>  <form:label path="name">name:</form:label> <form:input path="name"/> <br>  <form:label path="age">age:</form:label> <form:input path="age"/> <br>  <input type="submit" value="save"/>     </form:form> </html> 

customer.jsp

<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>submitted information</title> </head>  <body>  <h2>submitted information</h2> <table>  <tr>     <td>customer id</td>     <td>${custform.custid}</td>  </tr>  <tr>     <td>name</td>     <td>${custform.name}</td>  </tr>  <tr>     <td>age</td>     <td>${custform.age}</td>  </tr>  </table>   </body>  </html> 

data getting inserted in database not getting displayed.please me resolve this.

i tried using @modelattribute

model.addattribute("custid",customer.getcustid()); model.addattribute("name", customer.getname()); model.addattribute("age", customer.getage()); 

but not seem work.

model class

package com.projects.data;  import javax.persistence.column; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.table;  @entity @table(name="customer") public class customer {     @id     @column(name="cust_id")     int custid;      @column(name="name")     string name;      @column(name="age")     int age;      public customer(int custid,string name,int age)     {         this.custid=custid;         this.name=name;         this.age=age;     }      //getter , setter methods      public customer() {         // todo auto-generated constructor stub     }      public int getcustid() {         return custid;     }     public void setcustid(int custid) {         this.custid = custid;     }     public string getname() {         return name;     }     public void setname(string name) {         this.name = name;     }     public int getage() {         return age;     }     public void setage(int age) {         this.age = age;     }  } 

remove custform. ${custform.age},${custform.name} , ${custform.custid} in jsp , keep yout model.addattribute in controller. should able access age customer using ${age} in jsp.

controller:

@requestmapping(value = "/customer", method = requestmethod.post)    public string addcustomer(@modelattribute("customer") customer customer,model model) {     service.addcustomer(customer);     model.addattribute("custid",customer.getcustid());     model.addattribute("name", customer.getname());     model.addattribute("age", customer.getage());     return "customer/customer"; } 

jsp:

<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>submitted information</title> </head>  <body>  <h2>submitted information</h2> <table>  <tr>     <td>customer id</td>     <td>${custid}</td>  </tr>  <tr>     <td>name</td>     <td>${name}</td>  </tr>  <tr>     <td>age</td>     <td>${age}</td>  </tr>  </table>   </body>  </html> 

or

simply model.addattribute("custform",customer); in controller , leave else is.

controller

@requestmapping(value = "/customer", method = requestmethod.post)    public string addcustomer(@modelattribute("customer") customer customer,model model) {     service.addcustomer(customer);     model.addattribute("custform",customer);     return "customer/customer"; } 

jsp keep same in question.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -