java - Spring MVC does not save to database from html form -
hello have build simple form should save data database when submit form data not saved. how resolve ?
form in jsp
<form method="post"> product name:<br> <input type="text" id="productname" name="product name"> <br> last name:<br> <input type="text" id="productserial" name="serial number"> <input type="submit" value="submit"> </form>
controller
@requestmapping(value = "/savenewcontact", method = requestmethod.post) public modelandview savecontact(@requestparam("productname") string productname,@requestparam("productserial") int productserial) { product product = new product(); product.setname(productname); product.setserial(productserial); productdao.savenewproduct(product); return new modelandview("redirect:/"); }
dao implementation
public void savenewproduct(product product) { jdbctemplate = new jdbctemplate(datasource); string sql = "insert product (name, serial)" + " values (?, ?)"; jdbctemplate.update(sql, product.getname(), product.getserial()); }
you don't have in view tells controller fields in jsp file correspond model. can accomplish using spring form tags:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <form:form method="post"> product name:<br> <form:input type="text" id="productname" name="product name" path="productname" /> <br> last name:<br> <form:input type="text" id="productserial" name="serial number" path="productserial" /> <input type="submit" value="submit"> </form:form>
Comments
Post a Comment