Skip to content

Commit a83fda7

Browse files
committed
6 12 spring web
1 parent 38dcc81 commit a83fda7

4 files changed

Lines changed: 48 additions & 17 deletions

File tree

pom.xml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@
108108
<version>${spring-data-jpa.version}</version>
109109
</dependency>
110110

111+
<dependency>
112+
<groupId>org.springframework</groupId>
113+
<artifactId>spring-web</artifactId>
114+
<exclusions>
115+
<exclusion>
116+
<groupId>commons-logging</groupId>
117+
<artifactId>commons-logging</artifactId>
118+
</exclusion>
119+
</exclusions>
120+
</dependency>
121+
111122
<dependency>
112123
<groupId>org.hibernate</groupId>
113124
<artifactId>hibernate-core</artifactId>
@@ -146,9 +157,9 @@
146157

147158
<!--Web-->
148159
<dependency>
149-
<groupId>javax.servlet</groupId>
150-
<artifactId>javax.servlet-api</artifactId>
151-
<version>3.1.0</version>
160+
<groupId>org.apache.tomcat</groupId>
161+
<artifactId>tomcat-servlet-api</artifactId>
162+
<version>${tomcat.version}</version>
152163
<scope>provided</scope>
153164
</dependency>
154165

src/main/java/ru/javawebinar/topjava/web/MealServlet.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ru.javawebinar.topjava.web;
22

3-
import org.springframework.context.support.ClassPathXmlApplicationContext;
4-
import ru.javawebinar.topjava.Profiles;
3+
import org.springframework.web.context.WebApplicationContext;
4+
import org.springframework.web.context.support.WebApplicationContextUtils;
55
import ru.javawebinar.topjava.model.Meal;
66
import ru.javawebinar.topjava.util.DateTimeUtil;
77
import ru.javawebinar.topjava.web.meal.MealRestController;
@@ -20,25 +20,15 @@
2020

2121
public class MealServlet extends HttpServlet {
2222

23-
private ClassPathXmlApplicationContext springContext;
2423
private MealRestController mealController;
2524

2625
@Override
2726
public void init(ServletConfig config) throws ServletException {
2827
super.init(config);
29-
springContext = new ClassPathXmlApplicationContext(new String[]{"spring/spring-app.xml", "spring/spring-db.xml"}, false);
30-
// springContext.setConfigLocations("spring/spring-app.xml", "spring/spring-db.xml");
31-
springContext.getEnvironment().setActiveProfiles(Profiles.getActiveDbProfile(), Profiles.REPOSITORY_IMPLEMENTATION);
32-
springContext.refresh();
28+
WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
3329
mealController = springContext.getBean(MealRestController.class);
3430
}
3531

36-
@Override
37-
public void destroy() {
38-
springContext.close();
39-
super.destroy();
40-
}
41-
4232
@Override
4333
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
4434
request.setCharacterEncoding("UTF-8");

src/main/java/ru/javawebinar/topjava/web/UserServlet.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package ru.javawebinar.topjava.web;
22

33
import org.slf4j.Logger;
4+
import org.springframework.web.context.WebApplicationContext;
5+
import org.springframework.web.context.support.WebApplicationContextUtils;
46
import ru.javawebinar.topjava.AuthorizedUser;
7+
import ru.javawebinar.topjava.web.user.AdminRestController;
58

9+
import javax.servlet.ServletConfig;
610
import javax.servlet.ServletException;
711
import javax.servlet.http.HttpServlet;
812
import javax.servlet.http.HttpServletRequest;
@@ -14,6 +18,15 @@
1418
public class UserServlet extends HttpServlet {
1519
private static final Logger LOG = getLogger(UserServlet.class);
1620

21+
private AdminRestController adminController;
22+
23+
@Override
24+
public void init(ServletConfig config) throws ServletException {
25+
super.init(config);
26+
WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
27+
adminController = springContext.getBean(AdminRestController.class);
28+
}
29+
1730
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
1831
int userId = Integer.valueOf(request.getParameter("userId"));
1932
AuthorizedUser.setId(userId);
@@ -22,7 +35,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
2235

2336
@Override
2437
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
25-
LOG.debug("forward to users");
38+
LOG.debug("getAll");
39+
request.setAttribute("users", adminController.getAll());
2640
request.getRequestDispatcher("/users.jsp").forward(request, response);
2741
}
2842
}

src/main/webapp/WEB-INF/web.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
version="3.1">
66
<display-name>Topjava</display-name>
77

8+
k <context-param>
9+
<param-name>spring.profiles.default</param-name>
10+
<param-value>postgres,datajpa</param-value>
11+
</context-param>
12+
13+
<context-param>
14+
<param-name>contextConfigLocation</param-name>
15+
<param-value>
16+
classpath:spring/spring-app.xml
17+
classpath:spring/spring-db.xml
18+
</param-value>
19+
</context-param>
20+
21+
<listener>
22+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
23+
</listener>
824
<servlet>
925
<servlet-name>userServlet</servlet-name>
1026
<servlet-class>ru.javawebinar.topjava.web.UserServlet</servlet-class>

0 commit comments

Comments
 (0)