Problems in saving in the bank in Spring MVC project

Asked

Viewed 282 times

0

I’m new as a Spring MVC developer, I’m developing a simple application, but quite different from what I usually create, because usually Frameworks Spring MVC uses XML files to set up the project, and I’m using the Java class to do these things, even dispensed with the XML file which is persistences.xml and connecting through java class as you can see below;

@EnableTransactionManagement
public class JPAConfiguration {
      @Bean
       public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource);
          em.setPackagesToScan(new String[] { "br.com.casadocodigo.loja.models" });

          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          em.setJpaProperties(additionalProperties());

          return em;
       }

       @Bean       
       public DataSource dataSource(Environment environment){
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName("com.mysql.jdbc.Driver");
          dataSource.setUrl("jdbc:mysql://localhost:3306/casadocodigo");
          dataSource.setUsername( "root" );
          dataSource.setPassword( "123" );
          return dataSource;
       }

       @Bean
       public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);     
          return transactionManager;
       }

       @Bean
       public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
          return new PersistenceExceptionTranslationPostProcessor();
       }

      Properties additionalProperties() {
          Properties properties = new Properties();
          properties.setProperty("hibernate.hbm2ddl.auto", "update");
          properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
          properties.setProperty("hibernate.show_sql", "true");
          return properties;
       }
}

To configure the project I used this class ;

@EnableWebMvc
@ComponentScan(basePackageClasses={HomeController.class, ProductDAO.class})
public class AppWebConfiguration extends WebMvcConfigurerAdapter{
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

}

This is my controller class that of the product table;

import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import br.com.casadocodigo.loja.daos.ProductDAO;
import br.com.casadocodigo.loja.models.Product;

@Controller
@Transactional
public class ProductsController {

@Autowired
private ProductDAO productDAO;

@RequestMapping("/produtos/")
public String save(Product product){
    productDAO.save(product);
    return "products/ok";
}

@RequestMapping("/produtos/form")
public String form(){
    return "products/form";
}


}

And this is my page;

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cadastro de Produtos</title>
</head>
<body>

    <form method="post" >

        <div>
            <label for="title">Titulo</label> <input type="text" name="title"
                id="title" />
        </div>

        <div>
            <label for="description">Descrição</label>
            <textarea rows="10" cols="20" name="description" id="description"></textarea>

        </div>
        <div>
            <label for="pages">Número de Paginas</label>
            <input type="text" name="pages" id="pages"/>
        </div>
        <div>
            <input type="submit" value="Enviar"/>
        </div>
    </form>


</body>
</html>

This loading perfect, I can see the page, but I put the data on the page and click send and can not save the data in the database, and also does not generate error message in the eclipse.

I wonder where I’m going wrong on the project?

Is it the action that is on the form.jsp page?

how the link of the send button works with the method that save?

  • Any exceptions? Any errors in the server console?

  • nothing you believe!

  • https://github.com/wladimirbandeira/Loja my full project.

  • has some place of code that I could give system.print.out to see what is inside input and output of the variable?

  • Dude, take a look at how to add log4j to your project and set it as debug, so you’ll see everything that’s going on in the server log.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.