Guest User

Untitled

a guest
Nov 17th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. @RestController
  2. @RequestMapping(value = "/users")
  3. public class UserController {
  4.  
  5. @Autowired
  6. UserService userService;
  7.  
  8. @RequestMapping(method = RequestMethod.GET)
  9. public ModelAndView getAll(){
  10. ModelAndView mov = new ModelAndView("main");
  11. mov.addObject("users", userService.getAllUsers());
  12. return mov;
  13. }
  14.  
  15. @RequestMapping(value = "/addUser", method = RequestMethod.GET)
  16. public ModelAndView addView(){
  17. ModelAndView mov = new ModelAndView("add");
  18. mov.addObject("user", new User());
  19. return mov;
  20. }
  21.  
  22. @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
  23. public ModelAndView getUser(@PathVariable(value = "userId") Integer userId){
  24. ModelAndView mov = new ModelAndView("update");
  25. mov.addObject("user", userService.getUserById(userId));
  26. return mov;
  27. }
  28.  
  29. @RequestMapping(value = "/add", method = RequestMethod.POST)
  30. public ModelAndView addNewUser(@ModelAttribute("user") User user, BindingResult result){
  31. userService.addUser(user);
  32. return new ModelAndView(new RedirectView("/users"));
  33. }
  34.  
  35. @RequestMapping(value = "/delete/{userId}")
  36. public ModelAndView delUser(@PathVariable("userId") Integer userId){
  37. userService.deleteUser(userId);
  38. return new ModelAndView(new RedirectView("/users"));
  39. }
  40.  
  41. @RequestMapping(value = "/update", method = RequestMethod.PUT)
  42. public ModelAndView updUser(@ModelAttribute("user") User user){
  43. userService.updateUser(user);
  44. return new ModelAndView(new RedirectView("/users"));
  45. }
  46.  
  47.  
  48.  
  49. }
  50.  
  51. <?xml version="1.0" encoding="UTF-8"?>
  52. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  53. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55. version="3.1">
  56.  
  57. <display-name>UserControl</display-name>
  58. <servlet>
  59. <servlet-name>dispatcher</servlet-name>
  60. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  61. <load-on-startup>1</load-on-startup>
  62. </servlet>
  63. <servlet-mapping>
  64. <servlet-name>dispatcher</servlet-name>
  65. <url-pattern>/</url-pattern>
  66. </servlet-mapping>
  67. <context-param>
  68. <param-name>contextConfigLocation</param-name>
  69. <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
  70. </context-param>
  71. <listener>
  72. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  73. </listener>
  74.  
  75. </web-app>
  76.  
  77. <?xml version="1.0" encoding="UTF-8"?>
  78. <beans xmlns="http://www.springframework.org/schema/beans"
  79. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  80. xmlns:context="http://www.springframework.org/schema/context"
  81. xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
  82. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  83.  
  84. <tx:annotation-driven/>
  85. <context:component-scan base-package="com.malashenko.usercontrol.*"/>
  86. <jpa:repositories base-package="com.malashenko.usercontrol.repository"/>
  87.  
  88. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  89. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  90. <property name="url" value="jdbc:mysql://localhost:3306/users?createDatabaseIfNotExist=true"/>
  91. <property name="username" value="root"/>
  92. <property name="password" value="1"/>
  93. </bean>
  94.  
  95. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  96. <property name="dataSource" ref="dataSource"/>
  97. </bean>
  98.  
  99. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  100. <property name="entityManagerFactory" ref="entityManagerFactory"/>
  101. </bean>
  102.  
  103. <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
  104.  
  105. <bean id="hibernateExceptionTranslator" class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
  106.  
  107. <bean id="entityManagerFactory"
  108. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  109. <property name="dataSource" ref="dataSource"/>
  110. <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
  111. <!-- Set JPA properties -->
  112. <property name="jpaProperties">
  113. <props>
  114. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
  115. <!--<prop key="javax.persistence.schema-generation.database.action">none</prop>-->
  116. <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
  117. <prop key="hibernate.show_sql">true</prop>
  118. </props>
  119. </property>
  120. <!-- Set base package of your entities -->
  121. <property name="packagesToScan" value="com.malashenko.usercontrol.*"/>
  122. </bean>
  123.  
  124. <bean id="springLiquibaseBean" class="liquibase.integration.spring.SpringLiquibase">
  125. <property name="dataSource" ref="dataSource"/>
  126. <property name="changeLog" value="classpath:liquibase/usercontrol-liquibase.xml"/>
  127. </bean>
  128.  
  129. </beans>
  130.  
  131. <%@ page language="java" contentType="text/html; charset=utf8"
  132. pageEncoding="utf8"%>
  133. <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
  134. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
  135. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  136. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  137. <html>
  138. <head>
  139. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  140. <title>Панель пользователей</title>
  141. <script type="text/javascript" c:url value="resources/js/jquery.min.js"></script>
  142. <script type="text/javascript" c:url value="resources/js/bootstrap.min.js"></script>
  143. <script type="text/javascript" c:url value="resources/js/main.js"></script>
  144.  
  145. <style>
  146. <%@include file='resources/css/bootstrap.min.css' %>
  147. <%@include file='resources/css/main.css' %>
  148. <%@include file='resources/css/jquery-ui.css' %>
  149. <%@include file='resources/css/jquery-ui.structure.css' %>
  150. <%@include file='resources/css/jquery-ui.theme.css' %>
  151. </style>
  152. </head>
  153. <body>
  154. <div class="tableUsers">
  155. <table class="table table-condensed">
  156. <tr>
  157. <td><input id="add" name="add" type="button" value="+"
  158. onclick="javascript:window.location='/users/addUser'"></th></td>
  159. <td>Фамилия</td>
  160. <td>Логин</td>
  161. <td>Пароль</td>
  162. <td>О себе</td>
  163. <td>Адрес</td>
  164. </tr>
  165. <c:forEach items="${users}" var="user">
  166. <tr>
  167. <td>
  168. <input id="del" name="delete" type="button" value=" "
  169. onclick="javascript:window.location='/users/delete/'+ '${user.id}'">
  170. <input id="upd" name="update" type="button" value=" "
  171. onclick="javascript:window.location='/users/'+ '${user.id}'">
  172. </td>
  173. <td>${user.surname}</td>
  174. <td>${user.login}</td>
  175. <td>${user.password}</td>
  176. <td>${user.info}</td>
  177. <td>${user.address}</td>
  178. </tr>
  179. </c:forEach>
  180. </table>
  181. </div>
  182.  
  183. </body>
  184. </html>
  185.  
  186. <%@ page language="java" contentType="text/html; charset=utf8"
  187. pageEncoding="utf8" %>
  188. <%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
  189. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
  190. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  191. <%@ page isErrorPage="true" %>
  192. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  193. <html>
  194. <head>
  195. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  196. <title>Панель пользователей</title>
  197. <script type="text/javascript" c:url value="resources/js/jquery.min.js"></script>
  198. <script type="text/javascript" c:url value="resources/js/bootstrap.min.js"></script>
  199. <script type="text/javascript" c:url value="resources/js/main.js"></script>
  200. <script type="text/javascript" c:url value="resources/js/localization.js"></script>
  201.  
  202. <style>
  203. <%@include file='resources/css/bootstrap.min.css' %>
  204. <%@include file='resources/css/main.css' %>
  205. <%@include file='resources/css/jquery-ui.css' %>
  206. <%@include file='resources/css/jquery-ui.structure.css' %>
  207. <%@include file='resources/css/jquery-ui.theme.css' %>
  208. </style>
  209. </head>
  210. <body>
  211. <form:form method="post" action="/users/update" commandName="user">
  212. <table>
  213. <tr>
  214. <td><form:label path="surname">
  215. <spring:message text="Фамилия"/>
  216. </form:label></td>
  217. <td><form:input path="surname" value='${user.surname}'/></td>
  218. </tr>
  219. <tr>
  220. <td><form:label path="login">
  221. <spring:message text="Логин"/>
  222. </form:label></td>
  223. <td><form:input path="login" value='${user.login}'/></td>
  224. </tr>
  225. <tr>
  226. <td><form:label path="password">
  227. <spring:message text="Пароль"/>
  228. </form:label></td>
  229. <td><form:input path="password" value='${user.password}'/></td>
  230. </tr>
  231. <tr>
  232. <td><form:label path="info">
  233. <spring:message text="О себе"/>
  234. </form:label></td>
  235. <td><form:input path="info" value='${user.info}'/></td>
  236. </tr>
  237. <tr>
  238. <td><form:label path="address">
  239. <spring:message text="Адрес"/>
  240. </form:label></td>
  241. <td><form:input path="address" value='${user.address}'/></td>
  242. </tr>
  243. <tr>
  244. <td colspan="2"><input type="submit"
  245. value="<spring:message text="Изменить"/>"/></td>
  246. </tr>
  247. </table>
  248. </form:form>
  249. </body>
  250. </html>
  251.  
  252. <form:form method="post" action="/users/update" commandName="user">
  253.  
  254. @RequestMapping(value = "/update", method = RequestMethod.PUT)
  255. public ModelAndView updUser(@ModelAttribute("user") User user){
  256. userService.updateUser(user);
  257. return new ModelAndView(new RedirectView("/users"));
  258. }
Add Comment
Please, Sign In to add comment