Build:
Create simple web app using Maven, refer to this post
Dependencies:
- spring-core 4.3.4.RELEASE
- spring-context 4.3.4.RELEASE
- spring-web 4.3.4.RELEASE
- spring-webmvc 4.3.4.RELEASE
- servlet api 3.1
- jstl tag library 1.1.2
Add the following dependencies to the pom,
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.4.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency>
Structure:
config: Create config package for root and webconfig
web: Create web package for controller
view: Create home.jsp under WEB-INF/views
View
Create jsp page for view under WEB-INF/views, say home.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <h1>Hello, Welcome!</h1> <a href="<c:url value="/"/>">home</a> <a href="<c:url value="/register"/>">Register</a> </body> </html>
Config
Create Root and Web Configs under config package,
Root Config:
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.EnableWebMvc; /** * Created by zillani on 11/19/2016. * Simple RootConfig */ @Configuration @ComponentScan(basePackages = {"com.test"}, excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) }) public class RootConfig { }
Web Config:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; /** * Created by zillani on 11/19/2016. * Minimal Configuration for Spring MVC */ @Configuration @EnableWebMvc @ComponentScan("com.test") public class WebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){ configurer.enable(); } }
Application Initializer:
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; /** * Created by zillani on 11/19/2016. * The DispatcherServlet for the app */ public class TestAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{ RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{ WebConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
Controller:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import static org.springframework.web.bind.annotation.RequestMethod.GET; /** * Created by zillani on 11/19/2016. * A Simple Controller */ @Controller @RequestMapping("/") public class HomeController { @RequestMapping(method = GET) public String home() { return "home"; } }
Deploy:
build the app using Maven,
mvn clean install
deploy on tomcat server @ http://localhost:8080/
Download:
Advertisements