Reputation: 351
Hello I have build a simple form which should save the data to database however when I submit the form the data is not saved. How would I resolve this ?
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 INTO product (name, serial)"
+ " VALUES (?, ?)";
jdbcTemplate.update(sql, product.getName(), product.getSerial());
}
Upvotes: 2
Views: 2332
Reputation: 45
to save form in DB use @ModelAtribute(your table) + you need to define data what you want to add in DB somting like :
model.addAttribute('productName', productName)
Upvotes: 0
Reputation: 4480
You don't have anything in your view that tells the controller what fields in your JSP file correspond to your model. You can accomplish this by 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>
Upvotes: 1