Announcement Announcement Module
Collapse
No announcement yet.
Can't make a create product form works Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't make a create product form works

    I can't make a product form work. The funny part is that it works under the Unit Testing.

    Take a look at my code.

    The form:

    HTML Code:
    <%@ include file="/WEB-INF/jsp/include.jsp" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    
    <html>
    <head>
      <title><fmt:message key="title"/></title>
      <style type="text/css">
        .error { color: red; }
      </style>
    </head>
    <body>
    <h1><fmt:message key="priceincrease.heading"/></h1>
    <form:form method="post" commandName="priceIncrease">
      <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
        <tr>
          <td align="right" width="20%">Increase (%):</td>
            <td width="20%">
              <form:input path="percentage"/>
            </td>
            <td width="60%">
              <form:errors path="percentage" cssClass="error"/>
            </td>
        </tr>
      </table>
      <br>
      <input type="submit" align="center" value="Execute">
    </form:form>
    <a href="<c:url value="hello.htm"/>">Home</a>
    </body>
    </html>
    Controller:
    Code:
    package org.admios.nuevoproyecto.controller;
    
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.support.SessionStatus;
    import org.admios.nuevoproyecto.dao.ProductDAO;
    import org.admios.nuevoproyecto.model.Product;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import static java.lang.System.out;
    
    @Controller
    @RequestMapping("/product")
    public class ProductController {
    
    	@Autowired
    	ProductDAO pdi;
    
    	@RequestMapping(value="/add", method=RequestMethod.POST)
    	public String addProduct(@ModelAttribute Product product, BindingResult result, SessionStatus status) {
    		out.println("entering addProduct()");
    
    		Product savedProduct = this.pdi.saveProduct(product);
    		status.setComplete();
    		//out.println(savedProduct.getId());
    
    		return "hello";
    	}
    
    	@RequestMapping(value="/form")
    	public String viewForm() {
    		out.println("entering viewForm()");
    		return "addproduct";
    	}
    
    	@ModelAttribute("product")
    	public Product getProductObject() {
    		out.println("entering getProductObject()");
    		return new Product();
    	}
    }
    This is the Unit Testing (it works):
    Code:
    package org.admios.nuevoproyecto.model;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.junit.Test;
    import org.admios.nuevoproyecto.dao.ProductDAO;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import static org.junit.Assert.*;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({
    	"classpath:applicationContext.xml",
    	"classpath:org/admios/nuevoproyecto/applicationContext-services.xml"
    })
    public class ProductTest {
    
    	@Autowired
    	ProductDAO pdi;
    
    	@Test
    	public void testAddProduct() throws Exception {
    		Product product = new Product();
    		product.setTitle("New Product");
    		product.setDescription("Product description");
    		product.setPrice(220f);
    
    		Product savedProduct = pdi.saveProduct(product);
    
    		assertNotNull(savedProduct);
    
    		assertNotNull(savedProduct.getId());
    
    		System.out.println("ID: " + savedProduct.getId());
    	}
    }
    Last edited by demogar; Sep 22nd, 2010, 10:30 AM.

  • #2
    Try adding the following to your request mapping:
    @RequestMapping(value="/add", method=RequestMethod.POST)

    http://static.springsource.org/sprin...requestmapping

    Comment


    • #3
      did it and it doesn't work

      Comment


      • #4
        by the way:

        I see the forum, I can submit it and retrieve the values from the form, but I can't add the new value on the DB

        Comment


        • #5
          Are there any errors being thrown? What does your DAO and Service layer configuration look like?

          Comment


          • #6
            No errors are being thrown.

            The DAO is just a Bean with getters and setters, like this:

            Code:
            package org.admios.nuevoproyecto.dao;
            
            import java.util.List;
            import org.admios.nuevoproyecto.model.Product;
            import org.springframework.dao.DataAccessException;
            
            public interface ProductDAO {
            	public List<Product> getProducts() throws DataAccessException;
            
            	public Product saveProduct(Product product) throws DataAccessException;
            
            	public Product getProductById(Integer id) throws DataAccessException;
            
            	public void removeProduct(Product product) throws DataAccessException;
            }

            Comment


            • #7
              At a minimum you'll have to add some logging to each of your layers and see where things are being called and not called.

              Comment

              Working...
              X