From b4f4ab89c70d9ff19b70824fcdfb5544797f749a Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sun, 23 Sep 2018 18:00:15 +0300 Subject: [PATCH] war packing --- README.MD | 14 +-- pom.xml | 64 ++----------- .../skeleton/springmvc/SpringConfigMVC.java | 27 ++++++ .../dmitriymx/skeleton/springmvc/WebApp.java | 96 +++++-------------- src/main/resources/log4j2.xml | 13 --- src/main/webapp/WEB-INF/web.xml | 10 ++ 6 files changed, 68 insertions(+), 156 deletions(-) create mode 100644 src/main/java/ru/dmitriymx/skeleton/springmvc/SpringConfigMVC.java delete mode 100644 src/main/resources/log4j2.xml create mode 100644 src/main/webapp/WEB-INF/web.xml diff --git a/README.MD b/README.MD index 8316cff..2899b67 100644 --- a/README.MD +++ b/README.MD @@ -1,8 +1,8 @@ -# Skeleton: SpringMVC (standalone) +# Skeleton: SpringMVC (war) В данной ветке располагается пример *(заготовка, шаблон)* для создания _WebApplication_ средствами **Spring Framework** -с использованием **Spring WebMVC** со встроенным сервером **Jetty**. +с использованием **Spring WebMVC**. ## Сборка @@ -10,13 +10,3 @@ mvn package ``` -## Запуск - -``` -java -jar spring-mvc-1.0.1-SNAPSHOT.jar -``` - -По-умолчанию используются ip:port `127.0.0.1:8080`. Для изменения требуется добавить следующие параметры: - -- `-Dhost=0.0.0.0` - для указания IP адреса `0.0.0.0` -- `-Dport=80` - для указания порта `80` diff --git a/pom.xml b/pom.xml index da60991..8a17ce7 100644 --- a/pom.xml +++ b/pom.xml @@ -8,16 +8,14 @@ ru.dmitriymx.skeleton spring-mvc 1.0.1-SNAPSHOT + war UTF-8 1.8 1.7.25 - 2.11.1 5.1.0.RELEASE - 9.4.12.v20180830 lib - ru.dmitriymx.skeleton.springmvc.WebApp @@ -32,16 +30,6 @@ jcl-over-slf4j ${slf4j.version} - - org.apache.logging.log4j - log4j-core - ${log4j.version} - - - org.apache.logging.log4j - log4j-slf4j-impl - ${log4j.version} - @@ -61,24 +49,18 @@ ${spring.version} - - - org.eclipse.jetty - jetty-server - ${jetty.version} - - - org.eclipse.jetty - jetty-webapp - ${jetty.version} - - org.projectlombok lombok 1.18.2 + + javax.servlet + javax.servlet-api + 4.0.1 + provided + @@ -102,38 +84,6 @@ -Dfile.encoding=${project.build.sourceEncoding} - - org.apache.maven.plugins - maven-jar-plugin - 3.1.0 - - - - true - ${dependencies.dir}/ - ${main.class} - - - - - - org.apache.maven.plugins - maven-dependency-plugin - 3.1.1 - - ${project.build.directory}/${dependencies.dir}/ - provided - - - - copy-dependencies - package - - copy-dependencies - - - - diff --git a/src/main/java/ru/dmitriymx/skeleton/springmvc/SpringConfigMVC.java b/src/main/java/ru/dmitriymx/skeleton/springmvc/SpringConfigMVC.java new file mode 100644 index 0000000..09775e6 --- /dev/null +++ b/src/main/java/ru/dmitriymx/skeleton/springmvc/SpringConfigMVC.java @@ -0,0 +1,27 @@ +package ru.dmitriymx.skeleton.springmvc; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@EnableWebMvc +@ComponentScan("ru.dmitriymx.skeleton.springmvc") +public class SpringConfigMVC implements WebMvcConfigurer { + private static final int ONE_YEAR = 365*24*60*60; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(ONE_YEAR); + registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(ONE_YEAR); + registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(ONE_YEAR); + } + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } +} diff --git a/src/main/java/ru/dmitriymx/skeleton/springmvc/WebApp.java b/src/main/java/ru/dmitriymx/skeleton/springmvc/WebApp.java index ce306d6..97dfb48 100644 --- a/src/main/java/ru/dmitriymx/skeleton/springmvc/WebApp.java +++ b/src/main/java/ru/dmitriymx/skeleton/springmvc/WebApp.java @@ -1,88 +1,36 @@ package ru.dmitriymx.skeleton.springmvc; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Slf4jLog; -import org.springframework.context.annotation.Configuration; +import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; -import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.DispatcherServlet; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import java.net.InetSocketAddress; +import javax.servlet.FilterRegistration; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; -@Slf4j -@RequiredArgsConstructor -public class WebApp { - private final String host; - private final int port; +public class WebApp implements WebApplicationInitializer { + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); + appContext.register(SpringConfigMVC.class); - /** - * Данный класс является файлом настройки контекста для Spring - * однако, т.к. мы никакие бины не объявляем, то класс пустует - */ - @Configuration - @EnableWebMvc - public static class SpringConfigMVC implements WebMvcConfigurer { - } + // Dispatcher Servlet + ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext)); + dispatcher.setLoadOnStartup(1); + dispatcher.addMapping("/"); - /** - * Создаем Spring-контекст - * - * @return {@link WebApplicationContext} - */ - private WebApplicationContext getWebApplicationContext() { - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(SpringConfigMVC.class); - context.register(WebAppController.class); - // или - // context.setConfigLocation("ru.dmitriymx.skeleton.springmvc"); - // в этом случае будет просканирован весь пакет на @Configuration - return context; - } + dispatcher.setInitParameter("contextClass", appContext.getClass().getName()); - /** - * Подготавливаем обработчик запросов - * - * @param context Spring-контекст веб-приложения {@link WebApplicationContext} - * @return {@link ServletContextHandler} - */ - private ServletContextHandler getServletContextHandler(WebApplicationContext context) { - ServletContextHandler contextHandler = new ServletContextHandler(); - contextHandler.setErrorHandler(null); - contextHandler.setContextPath("/"); - contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/*"); - contextHandler.addEventListener(new ContextLoaderListener(context)); - return contextHandler; - } + servletContext.addListener(new ContextLoaderListener(appContext)); - /** - * Запуск встроенного Jetty веб-сервера - */ - private void start() { - Log.setLog(new Slf4jLog("Jetty.Logger")); - Server server = new Server(new InetSocketAddress(host, port)); - server.setHandler(getServletContextHandler(getWebApplicationContext())); - try { - server.start(); - server.join(); - } catch (Exception e) { - log.error("Error start server", e); - } - } + // UTF-8 Charactor Filter. + FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class); - public static void main(String[] args) { - final String host = System.getProperty("host", "127.0.0.1"); - final int port = Integer.parseInt(System.getProperty("port", "8080")); - - log.info("Web app listen: {}:{}", host, port); - WebApp app = new WebApp(host, port); - app.start(); + filterRegistration.setInitParameter("encoding", "UTF-8"); + filterRegistration.setInitParameter("forceEncoding", "true"); + filterRegistration.addMappingForUrlPatterns(null, true, "/*"); } } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml deleted file mode 100644 index 3be0d2c..0000000 --- a/src/main/resources/log4j2.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..5a2e2fe --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,10 @@ + + + + Skeleton - Spring MVC + + \ No newline at end of file