2

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());

}

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm now I am getting the following error Neither BindingResult nor plain target object for bean name
See this for the proper way to set up a Spring MVC form with JSP.. You are missing some key things like a proper model, a bindingresult, and modelAttribute in your form.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.