Advertisement
Guest User

Untitled

a guest
Oct 15th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. public RegistrationController(UserDao userDao) {
  2. this.userDao = userDao;
  3. }
  4.  
  5. @Autowired
  6. public RegistrationController(UserDao userDao) {
  7. this.userDao = userDao;
  8. }
  9.  
  10. <beans xmlns="http://www.springframework.org/schema/beans"
  11. xmlns:context="http://www.springframework.org/schema/context"
  12. xmlns:mvc="http://www.springframework.org/schema/mvc"
  13. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14. xmlns:p="http://www.springframework.org/schema/p"
  15. xmlns:tx="http://www.springframework.org/schema/tx"
  16. xsi:schemaLocation="
  17. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  18. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  19. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  20. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  21.  
  22. <!--
  23. Adds some default beans (HandlerAdapter, HandlerMapping, Binding Initializer...). It also turn on some annotations.
  24. Explanation in https://stackoverflow.com/questions/28851306/spring-framework-what-is-the-purpose-of-mvcannotation-driven
  25.  
  26. WITHOUT THIS, @RequestMapping ANNOTATIONS ARE LOADED, BUT MAPPING DO NOT WORK!!
  27. -->
  28. <mvc:annotation-driven />
  29.  
  30. <!-- Set loading annotations from classes
  31. <context:component-scan base-package="com.fido.pia"/>-->
  32.  
  33. <!--manual homepage-->
  34. <mvc:view-controller path="/" view-name="home"/>
  35.  
  36. <!--view resolver-->
  37. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  38. <property name="prefix" value="/WEB-INF/view/" />
  39. <property name="suffix" value=".jsp" />
  40. </bean>
  41.  
  42. <!--static resources - request will be handeled by ResourceHttpRequestHandler-->
  43. <mvc:resources mapping="/resources/**" location="/resources/" />
  44.  
  45. <!--database config-->
  46. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  47. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  48. <property name="url" value="jdbc:mysql://localhost:3306/pia" />
  49. <property name="username" value="root" />
  50. <property name="password" value="" />
  51. </bean>
  52.  
  53. <!--entity manager factory-->
  54. <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  55. <property name="packagesToScan" value="com.fido.pia.*/**" />
  56. <property name="dataSource" ref="dataSource" />
  57. <property name="jpaVendorAdapter">
  58. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  59. <!--<property name="generateDdl" value="true" />-->
  60. <property name="showSql" value="true" />
  61. </bean>
  62. </property>
  63. </bean>
  64.  
  65. <!-- Transactions -->
  66. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  67. <property name="entityManagerFactory" ref="entityManagerFactory" />
  68. </bean>
  69.  
  70. <!-- enable the configuration of transactional behavior based on annotations -->
  71. <tx:annotation-driven transaction-manager="transactionManager" />
  72.  
  73. <!--Set loading annotations from classes-->
  74. <context:component-scan base-package="com.fido.pia"/>
  75. </beans>
  76.  
  77. package com.fido.pia;
  78.  
  79. import com.fido.pia.dao.UserDao;
  80. import com.fido.pia.model.User;
  81. import java.io.IOException;
  82. import javax.servlet.http.HttpServletRequest;
  83. import javax.servlet.http.HttpServletResponse;
  84. import org.springframework.beans.factory.annotation.Autowired;
  85. import org.springframework.stereotype.Controller;
  86. import org.springframework.transaction.annotation.Transactional;
  87. import org.springframework.web.bind.annotation.RequestMapping;
  88. import org.springframework.web.bind.annotation.RequestMethod;
  89.  
  90. @Controller
  91. @RequestMapping("/register")
  92. public class RegistrationController {
  93.  
  94. private UserDao userDao;
  95.  
  96. @Autowired
  97. public RegistrationController(UserDao userDao) {
  98. this.userDao = userDao;
  99. }
  100.  
  101. @RequestMapping(method = RequestMethod.POST)
  102. @Transactional
  103. public void registrationSubmit(HttpServletRequest request, HttpServletResponse response)
  104. throws IOException{
  105. String username = request.getParameter("first_name");
  106.  
  107. userDao.save(new User("test"));
  108.  
  109. response.sendRedirect("/");
  110. }
  111. }
  112.  
  113. package com.fido.pia.dao;
  114.  
  115. import com.fido.pia.model.User;
  116. import javax.persistence.EntityManager;
  117. import javax.persistence.PersistenceContext;
  118. import javax.persistence.PersistenceContextType;
  119. import org.springframework.stereotype.Component;
  120. import org.springframework.stereotype.Repository;
  121.  
  122.  
  123. @Repository
  124. public class UserDao {
  125.  
  126. @PersistenceContext
  127. protected EntityManager entityManager;
  128.  
  129. public User save(User row) {
  130. if(row.isNew()) {
  131. entityManager.persist(row);
  132. return row;
  133. } else {
  134. return entityManager.merge(row);
  135. }
  136. }
  137. }
  138.  
  139. <context:annotation-config/>
  140.  
  141. <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
  142.  
  143. - First
  144. @Autowired,
  145. - Second in constructor
  146. @Autowired
  147. constructor(Object object)
  148. - other
  149. @Autowired
  150. setObjectMehod(Object object)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement