Advertisement
Guest User

javamvc

a guest
Feb 14th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 KB | None | 0 0
  1. package it.scai.tutoring.ecommerce.config;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.naming.NamingException;
  6. import javax.persistence.EntityManagerFactory;
  7.  
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.ComponentScan;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  13. import org.springframework.orm.jpa.JpaTransactionManager;
  14. import org.springframework.orm.jpa.JpaVendorAdapter;
  15. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  16. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  17. import org.springframework.transaction.PlatformTransactionManager;
  18. import org.springframework.transaction.annotation.EnableTransactionManagement;
  19. import org.springframework.web.servlet.ViewResolver;
  20. import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
  21. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  22. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  23. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  24. import org.springframework.web.servlet.view.JstlView;
  25.  
  26.  
  27. //@EnableWebMvc
  28. @Configuration
  29. @EnableWebMvc
  30. //@ComponentScan(basePackages =  {"it.scai.tutoring.ecommerce"})
  31. @ComponentScan(basePackages = {"it.scai.tutoring.ecommerce"})
  32. @EnableTransactionManagement
  33. public class SpringConfig extends WebMvcConfigurerAdapter{
  34.  
  35. //  @Bean
  36. //    public ViewResolver viewResolver() {
  37. //        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  38. //        viewResolver.setViewClass(JstlView.class);
  39. //        viewResolver.setPrefix("/jsp/");
  40. //        viewResolver.setSuffix(".jsp");
  41. //
  42. //        return viewResolver;
  43. //    }
  44.    
  45.     @Override
  46.     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  47.         configurer.enable();
  48.     }
  49.    
  50.     @Bean
  51.     public InternalResourceViewResolver getInternalResourceViewResolver() {
  52.         InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  53.         resolver.setPrefix("/jsp/");
  54.         resolver.setSuffix(".jsp");
  55.         return resolver;
  56.     }
  57.  
  58.     @Bean
  59.     public DriverManagerDataSource getDataSource() {
  60.         DriverManagerDataSource dataSource = new DriverManagerDataSource();
  61.         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  62.         dataSource.setUrl("jdbc:mysql://localhost:3306/ecommerce2");
  63.         dataSource.setUsername("root");
  64.         dataSource.setPassword("root");
  65.         return dataSource;
  66.     }
  67.     @Bean
  68.     @Autowired
  69.     public PlatformTransactionManager getTransactionManager(EntityManagerFactory emf) throws NamingException{
  70.         //Adopero il JpaTransactionManager per gestire le transazioni
  71.         JpaTransactionManager jpaTransaction = new JpaTransactionManager();
  72.         jpaTransaction.setEntityManagerFactory(emf);
  73.         return jpaTransaction;
  74.     }
  75.  
  76.     //Classe di Spring che permette di istanziare l'EntityManager
  77.     //Potrei usare anche LocalEntityManagerFactoryBean che va bene per applicazioni semplici ma è meno flessibile
  78.     //Usando LocalContainerEntityManagerFactoryBean posso impostare poù opzioni.
  79.     @Bean
  80.     public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
  81.  
  82.         LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
  83.         emf.setDataSource(getDataSource()); //imposto il DataSource creato sopra
  84.         emf.setPersistenceUnitName("EcommerceDan");// Vedi persistence.xml
  85.         emf.setJpaVendorAdapter(getHibernateAdapter());//imposto l'Adapter: Dico a Spring che mi dovrà implementare la specifica JPA con hibernate
  86.         Properties jpaProperties = new Properties();
  87.         jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
  88.         jpaProperties.put("hibernate.hbm2ddl.auto", "update");
  89.         jpaProperties.put("hibernate.show_sql", "true");
  90.         jpaProperties.put("hibernate.format_sql", "true");
  91.         //jpaProperties.put("hibernate.enable_lazy_load_no_trans", "true");
  92.         emf.setJpaProperties(jpaProperties);
  93.         return emf;
  94.     }
  95.     //Creo il mio Adapter JPA che Spring inietterà nell'EntityManagerFactory. Adopererò hibernate
  96.     @Bean
  97.     public JpaVendorAdapter getHibernateAdapter() {
  98.         return new HibernateJpaVendorAdapter();
  99.     }
  100.  
  101. }
  102.  
  103. //----------------------------------------------------------------------
  104.  
  105.  
  106. package it.scai.tutoring.ecommerce.controller;
  107.  
  108. import javax.servlet.http.HttpServletRequest;
  109. import javax.servlet.http.HttpServletResponse;
  110.  
  111. import org.springframework.beans.factory.annotation.Autowired;
  112. import org.springframework.stereotype.Controller;
  113. import org.springframework.web.bind.annotation.GetMapping;
  114. import org.springframework.web.bind.annotation.ModelAttribute;
  115. import org.springframework.web.bind.annotation.RequestMapping;
  116. import org.springframework.web.bind.annotation.RequestMethod;
  117. import org.springframework.web.bind.annotation.RestController;
  118. import org.springframework.web.servlet.ModelAndView;
  119.  
  120. import it.scai.tutoring.ecommerce.business.service.LoginService;
  121. import it.scai.tutoring.ecommerce.dto.LoginCredentials;
  122. import it.scai.tutoring.ecommerce.dto.LoginResult;
  123.  
  124. @RestController
  125. public class LoginController {
  126.  
  127.     @Autowired
  128.     private LoginService ecommerce;
  129.  
  130.     // @RequestMapping("/hello")
  131.     @GetMapping("/pippo")
  132.     public String add() {
  133.         System.out.println("mapped /pippo");
  134.         return "hello";
  135.     }
  136.  
  137.     @RequestMapping("/login")
  138.     public ModelAndView showLogin(HttpServletRequest request, HttpServletResponse response) {
  139.         ModelAndView mav = new ModelAndView("login");
  140.         mav.addObject("login", new LoginCredentials());
  141.         return mav;
  142.     }
  143.  
  144.     @RequestMapping(value = "/login", method = RequestMethod.POST)
  145.     public ModelAndView loginProcess(HttpServletRequest request, HttpServletResponse response,
  146.             @ModelAttribute("login") LoginCredentials login) {
  147.         ModelAndView mav = null;
  148.  
  149.         LoginResult result = ecommerce.logIn(login.getUsername(), login.getPassword());
  150.  
  151.         if (result.isLoginOk()) {
  152.             mav = new ModelAndView("welcome");
  153.             mav.addObject("firstname", result.getNome());
  154.         } else {
  155.             mav = new ModelAndView("login");
  156.             mav.addObject("message", "Username or Password is wrong!!");
  157.         }
  158.  
  159.         return mav;
  160.     }
  161.  
  162.     public LoginService getEcommerce() {
  163.         return ecommerce;
  164.     }
  165.  
  166.     public void setEcommerce(LoginService ecommerce) {
  167.         this.ecommerce = ecommerce;
  168.     }
  169.  
  170.    
  171.    
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement