Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.scai.tutoring.ecommerce.config;
- import java.util.Properties;
- import javax.naming.NamingException;
- import javax.persistence.EntityManagerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.JpaVendorAdapter;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import org.springframework.web.servlet.ViewResolver;
- import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
- import org.springframework.web.servlet.view.InternalResourceViewResolver;
- import org.springframework.web.servlet.view.JstlView;
- //@EnableWebMvc
- @Configuration
- @EnableWebMvc
- //@ComponentScan(basePackages = {"it.scai.tutoring.ecommerce"})
- @ComponentScan(basePackages = {"it.scai.tutoring.ecommerce"})
- @EnableTransactionManagement
- public class SpringConfig extends WebMvcConfigurerAdapter{
- // @Bean
- // public ViewResolver viewResolver() {
- // InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- // viewResolver.setViewClass(JstlView.class);
- // viewResolver.setPrefix("/jsp/");
- // viewResolver.setSuffix(".jsp");
- //
- // return viewResolver;
- // }
- @Override
- public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
- configurer.enable();
- }
- @Bean
- public InternalResourceViewResolver getInternalResourceViewResolver() {
- InternalResourceViewResolver resolver = new InternalResourceViewResolver();
- resolver.setPrefix("/jsp/");
- resolver.setSuffix(".jsp");
- return resolver;
- }
- @Bean
- public DriverManagerDataSource getDataSource() {
- DriverManagerDataSource dataSource = new DriverManagerDataSource();
- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
- dataSource.setUrl("jdbc:mysql://localhost:3306/ecommerce2");
- dataSource.setUsername("root");
- dataSource.setPassword("root");
- return dataSource;
- }
- @Bean
- @Autowired
- public PlatformTransactionManager getTransactionManager(EntityManagerFactory emf) throws NamingException{
- //Adopero il JpaTransactionManager per gestire le transazioni
- JpaTransactionManager jpaTransaction = new JpaTransactionManager();
- jpaTransaction.setEntityManagerFactory(emf);
- return jpaTransaction;
- }
- //Classe di Spring che permette di istanziare l'EntityManager
- //Potrei usare anche LocalEntityManagerFactoryBean che va bene per applicazioni semplici ma è meno flessibile
- //Usando LocalContainerEntityManagerFactoryBean posso impostare poù opzioni.
- @Bean
- public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
- LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
- emf.setDataSource(getDataSource()); //imposto il DataSource creato sopra
- emf.setPersistenceUnitName("EcommerceDan");// Vedi persistence.xml
- emf.setJpaVendorAdapter(getHibernateAdapter());//imposto l'Adapter: Dico a Spring che mi dovrà implementare la specifica JPA con hibernate
- Properties jpaProperties = new Properties();
- jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
- jpaProperties.put("hibernate.hbm2ddl.auto", "update");
- jpaProperties.put("hibernate.show_sql", "true");
- jpaProperties.put("hibernate.format_sql", "true");
- //jpaProperties.put("hibernate.enable_lazy_load_no_trans", "true");
- emf.setJpaProperties(jpaProperties);
- return emf;
- }
- //Creo il mio Adapter JPA che Spring inietterà nell'EntityManagerFactory. Adopererò hibernate
- @Bean
- public JpaVendorAdapter getHibernateAdapter() {
- return new HibernateJpaVendorAdapter();
- }
- }
- //----------------------------------------------------------------------
- package it.scai.tutoring.ecommerce.controller;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.servlet.ModelAndView;
- import it.scai.tutoring.ecommerce.business.service.LoginService;
- import it.scai.tutoring.ecommerce.dto.LoginCredentials;
- import it.scai.tutoring.ecommerce.dto.LoginResult;
- @RestController
- public class LoginController {
- @Autowired
- private LoginService ecommerce;
- // @RequestMapping("/hello")
- @GetMapping("/pippo")
- public String add() {
- System.out.println("mapped /pippo");
- return "hello";
- }
- @RequestMapping("/login")
- public ModelAndView showLogin(HttpServletRequest request, HttpServletResponse response) {
- ModelAndView mav = new ModelAndView("login");
- mav.addObject("login", new LoginCredentials());
- return mav;
- }
- @RequestMapping(value = "/login", method = RequestMethod.POST)
- public ModelAndView loginProcess(HttpServletRequest request, HttpServletResponse response,
- @ModelAttribute("login") LoginCredentials login) {
- ModelAndView mav = null;
- LoginResult result = ecommerce.logIn(login.getUsername(), login.getPassword());
- if (result.isLoginOk()) {
- mav = new ModelAndView("welcome");
- mav.addObject("firstname", result.getNome());
- } else {
- mav = new ModelAndView("login");
- mav.addObject("message", "Username or Password is wrong!!");
- }
- return mav;
- }
- public LoginService getEcommerce() {
- return ecommerce;
- }
- public void setEcommerce(LoginService ecommerce) {
- this.ecommerce = ecommerce;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement