├── .gitignore ├── src ├── main │ └── java │ │ ├── META-INF │ │ └── webapp │ │ │ └── WEB-INF │ │ │ └── view │ │ │ ├── css │ │ │ └── style.css │ │ │ ├── images │ │ │ └── favicon.ico │ │ │ └── index.jsp │ │ └── com │ │ └── sjl │ │ ├── config │ │ ├── ApplicationModule.java │ │ ├── WebAppInitializer.java │ │ └── WebModule.java │ │ ├── web │ │ └── Home.java │ │ ├── Main.java │ │ ├── WebServerConfig.java │ │ └── WebServer.java └── test │ └── java │ └── com │ └── sjl │ └── IDE.java ├── README.markdown └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | var 3 | dependency-reduced-pom.xml 4 | .classpath 5 | .project 6 | .settings 7 | -------------------------------------------------------------------------------- /src/main/java/META-INF/webapp/WEB-INF/view/css/style.css: -------------------------------------------------------------------------------- 1 | * { 2 | padding:0px; 3 | margin:0px; 4 | font-family:verdana; 5 | } 6 | 7 | h1 { 8 | color:blue; 9 | } -------------------------------------------------------------------------------- /src/main/java/META-INF/webapp/WEB-INF/view/images/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/steveliles/jetty-embedded-spring-mvc-noxml/HEAD/src/main/java/META-INF/webapp/WEB-INF/view/images/favicon.ico -------------------------------------------------------------------------------- /src/main/java/META-INF/webapp/WEB-INF/view/index.jsp: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 |Hi <%="me"%>!
8 | 9 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | A simple example project that sets up an embedded Jetty 8 with Spring-MVC, JSP and JSTL support. 2 | 3 | This example uses no XML configuration. 4 | 5 | Further description [here](http://steveliles.github.io/setting_up_embedded_jetty_8_and_spring_mvc_with_maven.html). 6 | -------------------------------------------------------------------------------- /src/test/java/com/sjl/IDE.java: -------------------------------------------------------------------------------- 1 | package com.sjl; 2 | 3 | public class IDE { 4 | // This class is a place-holder used to identify whether the 5 | // web-server is running in an IDE where the test class hierarchy 6 | // is available or in "production" mode (test classes removed) 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/sjl/config/ApplicationModule.java: -------------------------------------------------------------------------------- 1 | package com.sjl.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | 5 | @Configuration 6 | public class ApplicationModule 7 | { 8 | // Declare "application" scope beans here (ie., beans that are not only used by the web context) 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/sjl/web/Home.java: -------------------------------------------------------------------------------- 1 | package com.sjl.web; 2 | 3 | import org.springframework.stereotype.*; 4 | import org.springframework.web.bind.annotation.*; 5 | import org.springframework.web.servlet.*; 6 | 7 | @Controller 8 | public class Home { 9 | 10 | @RequestMapping("/") 11 | public ModelAndView home() 12 | { 13 | return new ModelAndView("index"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/sjl/Main.java: -------------------------------------------------------------------------------- 1 | package com.sjl; 2 | 3 | public class Main 4 | { 5 | public static void main(String... anArgs) throws Exception 6 | { 7 | new Main().start(); 8 | } 9 | 10 | private WebServer server; 11 | 12 | public Main() 13 | { 14 | server = new WebServer( 15 | WebServerConfig.Factory.newDevelopmentConfig("happy", 8000, "localhost")); 16 | } 17 | 18 | public void start() throws Exception 19 | { 20 | server.start(); 21 | server.join(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/sjl/config/WebAppInitializer.java: -------------------------------------------------------------------------------- 1 | package com.sjl.config; 2 | 3 | import javax.servlet.*; 4 | 5 | import org.apache.jasper.servlet.*; 6 | import org.springframework.web.*; 7 | import org.springframework.web.context.*; 8 | import org.springframework.web.context.support.*; 9 | import org.springframework.web.servlet.*; 10 | 11 | public class WebAppInitializer implements WebApplicationInitializer 12 | { 13 | private static final String JSP_SERVLET_NAME = "jsp"; 14 | private static final String DISPATCHER_SERVLET_NAME = "dispatcher"; 15 | 16 | @Override 17 | public void onStartup(ServletContext aServletContext) throws ServletException 18 | { 19 | registerListener(aServletContext); 20 | registerDispatcherServlet(aServletContext); 21 | registerJspServlet(aServletContext); 22 | } 23 | 24 | private void registerListener(ServletContext aContext) 25 | { 26 | AnnotationConfigWebApplicationContext _root = createContext(ApplicationModule.class); 27 | aContext.addListener(new ContextLoaderListener(_root)); 28 | } 29 | 30 | private void registerDispatcherServlet(ServletContext aContext) 31 | { 32 | AnnotationConfigWebApplicationContext _ctx = createContext(WebModule.class); 33 | ServletRegistration.Dynamic _dispatcher = 34 | aContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(_ctx)); 35 | _dispatcher.setLoadOnStartup(1); 36 | _dispatcher.addMapping("/"); 37 | } 38 | 39 | private void registerJspServlet(ServletContext aContext) { 40 | ServletRegistration.Dynamic _dispatcher = 41 | aContext.addServlet(JSP_SERVLET_NAME, new JspServlet()); 42 | _dispatcher.setLoadOnStartup(1); 43 | _dispatcher.addMapping("*.jsp"); 44 | } 45 | 46 | private AnnotationConfigWebApplicationContext createContext(final Class>... aModules) 47 | { 48 | AnnotationConfigWebApplicationContext _ctx = new AnnotationConfigWebApplicationContext(); 49 | _ctx.register(aModules); 50 | return _ctx; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/sjl/config/WebModule.java: -------------------------------------------------------------------------------- 1 | package com.sjl.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.servlet.ViewResolver; 7 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 8 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 9 | import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 10 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 11 | import org.springframework.web.servlet.view.JstlView; 12 | import org.springframework.web.servlet.view.UrlBasedViewResolver; 13 | 14 | @EnableWebMvc 15 | @Configuration 16 | @ComponentScan(basePackages={"com.sjl"}) 17 | public class WebModule extends WebMvcConfigurerAdapter 18 | { 19 | @Override 20 | public void addViewControllers(ViewControllerRegistry aRegistry) 21 | { 22 | aRegistry.addViewController("/").setViewName("index"); 23 | } 24 | 25 | @Override 26 | public void addResourceHandlers(ResourceHandlerRegistry aRegistry) 27 | { 28 | aRegistry.addResourceHandler("/s/*").addResourceLocations("classpath:/META-INF/webapp/WEB-INF/view/scripts/*"); 29 | aRegistry.addResourceHandler("/c/*").addResourceLocations("classpath:/META-INF/webapp/WEB-INF/view/css/*"); 30 | aRegistry.addResourceHandler("/i/*").addResourceLocations("classpath:/META-INF/webapp/WEB-INF/view/images/*"); 31 | aRegistry.addResourceHandler("/WEB-INF/view/*").addResourceLocations("classpath:/META-INF/webapp/WEB-INF/view/*"); 32 | aRegistry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/webapp/WEB-INF/view/images/favicon.ico"); 33 | } 34 | 35 | @Bean 36 | public ViewResolver viewResolver() 37 | { 38 | UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); 39 | viewResolver.setViewClass(JstlView.class); 40 | viewResolver.setPrefix("WEB-INF/view/"); 41 | viewResolver.setSuffix(".jsp"); 42 | return viewResolver; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/sjl/WebServerConfig.java: -------------------------------------------------------------------------------- 1 | package com.sjl; 2 | 3 | public interface WebServerConfig 4 | { 5 | public String getServerName(); 6 | 7 | public int getPort(); 8 | 9 | public String getHostInterface(); 10 | 11 | public int getMinThreads(); 12 | 13 | public int getMaxThreads(); 14 | 15 | public String getAccessLogDirectory(); 16 | 17 | public class Factory { 18 | 19 | public static WebServerConfig newDevelopmentConfig(String aName, int aPort, String anInterface) { 20 | return new Development(aName, aPort, anInterface); 21 | } 22 | 23 | public static WebServerConfig newProductionConfig(String aName, int aPort, String anInterface, int aMinThreads, int aMaxThreads) { 24 | return new Production(aName, aPort, anInterface, aMinThreads, aMaxThreads); 25 | } 26 | 27 | static abstract class AbstractWebServerConfig implements WebServerConfig 28 | { 29 | private String name; 30 | private int port; 31 | private String intf; 32 | private int minThreads; 33 | private int maxThreads; 34 | 35 | private AbstractWebServerConfig(String aName, int aPort, String anInterface, int aMinThreads, int aMaxThreads) 36 | { 37 | name = aName; 38 | port = aPort; 39 | intf = anInterface; 40 | minThreads = aMinThreads; 41 | maxThreads = aMaxThreads; 42 | } 43 | 44 | @Override 45 | public String getServerName() 46 | { 47 | return name; 48 | } 49 | 50 | @Override 51 | public int getPort() 52 | { 53 | return port; 54 | } 55 | 56 | @Override 57 | public String getHostInterface() 58 | { 59 | return intf; 60 | } 61 | 62 | @Override 63 | public int getMinThreads() 64 | { 65 | return minThreads; 66 | } 67 | 68 | @Override 69 | public int getMaxThreads() 70 | { 71 | return maxThreads; 72 | } 73 | 74 | @Override 75 | public String getAccessLogDirectory() 76 | { 77 | return String.format("./var/logs/$s/", name); 78 | } 79 | } 80 | 81 | public static final class Development extends AbstractWebServerConfig 82 | { 83 | public Development(String aName, int aPort, String anInterface) 84 | { 85 | super(aName, aPort, anInterface, 5, 15); 86 | } 87 | } 88 | 89 | public static final class Production extends AbstractWebServerConfig 90 | { 91 | public Production(String aName, int aPort, String anInterface, int aMinThreads, int aMaxThreads) 92 | { 93 | super(aName, aPort, anInterface, aMinThreads, aMaxThreads); 94 | } 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 |