I'm new to Spring MVC developer, I'm developing a simple application, but quite different from what I usually create, because usually the SpringWorks MVC FrameWorks uses XML files to configure the project, and I'm using the Java class to do these things, including dispense the XML file that 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 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 is loading perfectly, I can see the page, however I put the data on the page and I click send and it can not save the data in the database, nor does it generate an error message in eclipse.
Where am I going wrong with the project?
Is the action on page form.jsp?
How does the send button link work with the save method?