war packing
This commit is contained in:
14
README.MD
14
README.MD
@@ -1,8 +1,8 @@
|
|||||||
# Skeleton: SpringMVC (standalone)
|
# Skeleton: SpringMVC (war)
|
||||||
|
|
||||||
В данной ветке располагается пример *(заготовка, шаблон)*
|
В данной ветке располагается пример *(заготовка, шаблон)*
|
||||||
для создания _WebApplication_ средствами **Spring Framework**
|
для создания _WebApplication_ средствами **Spring Framework**
|
||||||
с использованием **Spring WebMVC** со встроенным сервером **Jetty**.
|
с использованием **Spring WebMVC**.
|
||||||
|
|
||||||
## Сборка
|
## Сборка
|
||||||
|
|
||||||
@@ -10,13 +10,3 @@
|
|||||||
mvn package
|
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`
|
|
||||||
|
|||||||
64
pom.xml
64
pom.xml
@@ -8,16 +8,14 @@
|
|||||||
<groupId>ru.dmitriymx.skeleton</groupId>
|
<groupId>ru.dmitriymx.skeleton</groupId>
|
||||||
<artifactId>spring-mvc</artifactId>
|
<artifactId>spring-mvc</artifactId>
|
||||||
<version>1.0.1-SNAPSHOT</version>
|
<version>1.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<slf4j.version>1.7.25</slf4j.version>
|
<slf4j.version>1.7.25</slf4j.version>
|
||||||
<log4j.version>2.11.1</log4j.version>
|
|
||||||
<spring.version>5.1.0.RELEASE</spring.version>
|
<spring.version>5.1.0.RELEASE</spring.version>
|
||||||
<jetty.version>9.4.12.v20180830</jetty.version>
|
|
||||||
<dependencies.dir>lib</dependencies.dir>
|
<dependencies.dir>lib</dependencies.dir>
|
||||||
<main.class>ru.dmitriymx.skeleton.springmvc.WebApp</main.class>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -32,16 +30,6 @@
|
|||||||
<artifactId>jcl-over-slf4j</artifactId>
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
<version>${slf4j.version}</version>
|
<version>${slf4j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
|
||||||
<artifactId>log4j-core</artifactId>
|
|
||||||
<version>${log4j.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
|
||||||
<artifactId>log4j-slf4j-impl</artifactId>
|
|
||||||
<version>${log4j.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- SPRING -->
|
<!-- SPRING -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -61,24 +49,18 @@
|
|||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- JETTY -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
|
||||||
<artifactId>jetty-server</artifactId>
|
|
||||||
<version>${jetty.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
|
||||||
<artifactId>jetty-webapp</artifactId>
|
|
||||||
<version>${jetty.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- COMPONENTS -->
|
<!-- COMPONENTS -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.18.2</version>
|
<version>1.18.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>4.0.1</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -102,38 +84,6 @@
|
|||||||
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
|
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
|
||||||
<version>3.1.0</version>
|
|
||||||
<configuration>
|
|
||||||
<archive>
|
|
||||||
<manifest>
|
|
||||||
<addClasspath>true</addClasspath>
|
|
||||||
<classpathPrefix>${dependencies.dir}/</classpathPrefix>
|
|
||||||
<mainClass>${main.class}</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-dependency-plugin</artifactId>
|
|
||||||
<version>3.1.1</version>
|
|
||||||
<configuration>
|
|
||||||
<outputDirectory>${project.build.directory}/${dependencies.dir}/</outputDirectory>
|
|
||||||
<excludeScope>provided</excludeScope>
|
|
||||||
</configuration>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>copy-dependencies</id>
|
|
||||||
<phase>package</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>copy-dependencies</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,88 +1,36 @@
|
|||||||
package ru.dmitriymx.skeleton.springmvc;
|
package ru.dmitriymx.skeleton.springmvc;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import org.springframework.web.WebApplicationInitializer;
|
||||||
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.context.ContextLoaderListener;
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
|
import org.springframework.web.filter.CharacterEncodingFilter;
|
||||||
import org.springframework.web.servlet.DispatcherServlet;
|
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
|
public class WebApp implements WebApplicationInitializer {
|
||||||
@RequiredArgsConstructor
|
@Override
|
||||||
public class WebApp {
|
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||||
private final String host;
|
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
|
||||||
private final int port;
|
appContext.register(SpringConfigMVC.class);
|
||||||
|
|
||||||
/**
|
// Dispatcher Servlet
|
||||||
* Данный класс является файлом настройки контекста для Spring
|
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
|
||||||
* однако, т.к. мы никакие бины не объявляем, то класс пустует
|
dispatcher.setLoadOnStartup(1);
|
||||||
*/
|
dispatcher.addMapping("/");
|
||||||
@Configuration
|
|
||||||
@EnableWebMvc
|
|
||||||
public static class SpringConfigMVC implements WebMvcConfigurer {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
dispatcher.setInitParameter("contextClass", appContext.getClass().getName());
|
||||||
* Создаем 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
servletContext.addListener(new ContextLoaderListener(appContext));
|
||||||
* Подготавливаем обработчик запросов
|
|
||||||
*
|
|
||||||
* @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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// UTF-8 Charactor Filter.
|
||||||
* Запуск встроенного Jetty веб-сервера
|
FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
|
||||||
*/
|
|
||||||
private void start() {
|
filterRegistration.setInitParameter("encoding", "UTF-8");
|
||||||
Log.setLog(new Slf4jLog("Jetty.Logger"));
|
filterRegistration.setInitParameter("forceEncoding", "true");
|
||||||
Server server = new Server(new InetSocketAddress(host, port));
|
filterRegistration.addMappingForUrlPatterns(null, true, "/*");
|
||||||
server.setHandler(getServletContextHandler(getWebApplicationContext()));
|
|
||||||
try {
|
|
||||||
server.start();
|
|
||||||
server.join();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Error start server", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<Configuration>
|
|
||||||
<Appenders>
|
|
||||||
<Console name="Console" target="SYSTEM_OUT">
|
|
||||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%level] (%t) \{%logger\} %msg%n"/>
|
|
||||||
</Console>
|
|
||||||
</Appenders>
|
|
||||||
<Loggers>
|
|
||||||
<Root level="info">
|
|
||||||
<AppenderRef ref="Console"/>
|
|
||||||
</Root>
|
|
||||||
</Loggers>
|
|
||||||
</Configuration>
|
|
||||||
10
src/main/webapp/WEB-INF/web.xml
Normal file
10
src/main/webapp/WEB-INF/web.xml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
||||||
|
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||||
|
id="WebApp_ID" version="3.0">
|
||||||
|
|
||||||
|
<display-name>Skeleton - Spring MVC</display-name>
|
||||||
|
|
||||||
|
</web-app>
|
||||||
Reference in New Issue
Block a user