Skip to content

Commit 72d5bfc

Browse files
ThoughtscriptKevinGilmore
authored andcommitted
* BAEL-9 eugenp#3 * pom.xml fix
1 parent b802653 commit 72d5bfc

23 files changed

Lines changed: 402 additions & 0 deletions

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
<module>spring-autowire</module>
110110
<module>spring-batch</module>
111111
<module>spring-boot</module>
112+
<module>spring-boot-servlet</module>
112113
<module>spring-cloud-data-flow</module>
113114
<module>spring-cloud</module>
114115
<module>spring-core</module>

‎spring-boot-servlet/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target/
2+
.settings/
3+
.classpath
4+
.project

‎spring-boot-servlet/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
###Relevant Articles:
2+
- [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/how-to-register-a-servlet-in-a-java-web-application/)

‎spring-boot-servlet/pom.xml‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-boot-servlet</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
<name>spring-boot-servlet</name>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-dependencies</artifactId>
13+
<version>1.5.1.RELEASE</version>
14+
</parent>
15+
16+
<dependencies>
17+
<!-- Spring Boot Dependencies -->
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-tomcat</artifactId>
25+
<scope>provided</scope>
26+
</dependency>
27+
<!-- Embedded EmbeddedTomcatExample Dependencies -->
28+
<dependency>
29+
<groupId>org.apache.tomcat.embed</groupId>
30+
<artifactId>tomcat-embed-core</artifactId>
31+
<version>${tomcat.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.tomcat.embed</groupId>
35+
<artifactId>tomcat-embed-jasper</artifactId>
36+
<version>${tomcat.version}</version>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
<properties>
50+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
51+
<java.version>1.8</java.version>
52+
<tomcat.version>8.5.11</tomcat.version>
53+
</properties>
54+
55+
</project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Manifest-Version: 1.0
2+
Main-Class: com.baeldung.ApplicationMain
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.builder.SpringApplicationBuilder;
6+
import org.springframework.boot.web.support.SpringBootServletInitializer;
7+
8+
@SpringBootApplication
9+
public class ApplicationMain extends SpringBootServletInitializer {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(ApplicationMain.class, args);
13+
}
14+
15+
@Override
16+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
17+
return application.sources(ApplicationMain.class);
18+
}
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.configuration;
2+
3+
import org.springframework.web.WebApplicationInitializer;
4+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
5+
import org.springframework.web.context.support.XmlWebApplicationContext;
6+
import org.springframework.web.servlet.DispatcherServlet;
7+
import javax.servlet.ServletContext;
8+
import javax.servlet.ServletException;
9+
import javax.servlet.ServletRegistration;
10+
11+
public class WebAppInitializer implements WebApplicationInitializer {
12+
13+
public void onStartup(ServletContext container) throws ServletException {
14+
15+
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
16+
ctx.register(WebMvcConfigure.class);
17+
ctx.setServletContext(container);
18+
19+
ServletRegistration.Dynamic servletOne = container.addServlet("SpringProgrammaticDispatcherServlet", new DispatcherServlet(ctx));
20+
servletOne.setLoadOnStartup(1);
21+
servletOne.addMapping("/");
22+
23+
XmlWebApplicationContext xctx = new XmlWebApplicationContext();
24+
xctx.setConfigLocation("/WEB-INF/context.xml");
25+
xctx.setServletContext(container);
26+
27+
ServletRegistration.Dynamic servletTwo = container.addServlet("SpringProgrammaticXMLDispatcherServlet", new DispatcherServlet(xctx));
28+
servletTwo.setLoadOnStartup(1);
29+
servletTwo.addMapping("/");
30+
}
31+
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.configuration;
2+
3+
import org.springframework.boot.web.support.ErrorPageFilter;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
8+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
import org.springframework.web.servlet.resource.PathResourceResolver;
11+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
12+
13+
@Configuration
14+
public class WebMvcConfigure extends WebMvcConfigurerAdapter {
15+
16+
@Bean
17+
public ViewResolver getViewResolver() {
18+
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
19+
resolver.setPrefix("/WEB-INF/");
20+
resolver.setSuffix(".jsp");
21+
return resolver;
22+
}
23+
24+
@Override
25+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
26+
configurer.enable();
27+
}
28+
29+
30+
@Override
31+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
32+
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver());
33+
}
34+
35+
@Bean
36+
public ErrorPageFilter errorPageFilter() {
37+
return new ErrorPageFilter();
38+
}
39+
}
40+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.props;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
5+
import java.util.Properties;
6+
7+
public final class Constants {
8+
9+
@Autowired
10+
PropertySourcesLoader psl;
11+
12+
public static final String breakLine = System.getProperty("line.separator");
13+
private static final PropertyLoader pl = new PropertyLoader();
14+
private static final Properties mainProps = pl.getProperties("custom.properties");
15+
public static final String DISPATCHER_SERVLET_NAME = mainProps.getProperty("dispatcher.servlet.name");
16+
public static final String DISPATCHER_SERVLET_MAPPING = mainProps.getProperty("dispatcher.servlet.mapping");
17+
private final String EXAMPLE_SERVLET_NAME = psl.getProperty("example.servlet.name");
18+
private final String EXAMPLE_SERVLET_MAPPING = psl.getProperty("example.servlet.mapping");
19+
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.props;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.util.Properties;
9+
10+
public class PropertyLoader {
11+
private static final Logger log = LoggerFactory.getLogger(PropertyLoader.class);
12+
13+
public Properties getProperties(String file) {
14+
Properties prop = new Properties();
15+
InputStream input = null;
16+
try {
17+
input = getClass().getResourceAsStream(file);
18+
prop.load(input);
19+
if (input != null) {
20+
input.close();
21+
}
22+
} catch (IOException ex) {
23+
log.error("IOException: " + ex);
24+
}
25+
return prop;
26+
}
27+
}

0 commit comments

Comments
 (0)