I created Hello World
App by Spring Web MVC framework . This article is a description of sample code for Spring Web MVC.
🗽 Directory Structure
😎 pom.xml Main Configuration by maven is ./pom.xml
like this:
<project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd " > <modelVersion > 4.0.0modelVersion > <groupId > examplegroupId > <artifactId > firstappartifactId > <packaging > warpackaging > <version > 0.0.1-SNAPSHOTversion > <name > firstapp Maven Webappname > <url > http://maven.apache.orgurl > <dependencyManagement > <dependencies > <dependency > <groupId > io.spring.platformgroupId > <artifactId > platform-bomartifactId > <version > 2.0.5.RELEASEversion > <type > pomtype > <scope > importscope > dependency > dependencies > dependencyManagement > <dependencies > <dependency > <groupId > javax.servletgroupId > <artifactId > javax.servlet-apiartifactId > <scope > providedscope > dependency > <dependency > <groupId > org.apache.taglibsgroupId > <artifactId > taglibs-standard-jstlelartifactId > dependency > <dependency > <groupId > org.springframeworkgroupId > <artifactId > spring-webmvcartifactId > dependency > <dependency > <groupId > org.hibernategroupId > <artifactId > hibernate-validatorartifactId > dependency > <dependency > <groupId > org.slf4jgroupId > <artifactId > jcl-over-slf4jartifactId > dependency > <dependency > <groupId > ch.qos.logbackgroupId > <artifactId > logback-classicartifactId > dependency > dependencies > <build > <finalName > firstappfinalName > <pluginManagement > <plugins > <plugin > <artifactId > maven-compiler-pluginartifactId > <configuration > <source > 1.8source > <target > 1.8target > configuration > plugin > <plugin > <groupId > org.mortbay.jettygroupId > <artifactId > maven-jetty-pluginartifactId > <version > 6.1.7version > plugin > plugins > pluginManagement > build > project >
🐠 web.xml In src/main/java/WENB-INF/web.xml
, there are configurations for initialize servlet and etc.
<web-app xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation =" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version ="3.0" > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener listener-class > listener > <context-param > <param-name > contextClassparam-name > <param-value > org.springframework.web.context.support.AnnotationConfigWebApplicationContext param-value > context-param > <context-param > <param-name > contextConfigLocationparam-name > <param-value > example.config.AppConfigparam-value > context-param > <filter > <filter-name > CharacterEncodingFilterfilter-name > <filter-class > org.springframework.web.filter.CharacterEncodingFilter filter-class > <init-param > <param-name > encodingparam-name > <param-value > UTF-8param-value > init-param > <init-param > <param-name > forceEncodingparam-name > <param-value > trueparam-value > init-param > filter > <filter-mapping > <filter-name > CharacterEncodingFilterfilter-name > <url-pattern > /*url-pattern > filter-mapping > <servlet > <servlet-name > appservlet-name > <servlet-class > org.springframework.web.servlet.DispatcherServlet servlet-class > <init-param > <param-name > contextClassparam-name > <param-value > org.springframework.web.context.support.AnnotationConfigWebApplicationContext param-value > init-param > <init-param > <param-name > contextConfigLocationparam-name > <param-value > example.config.WebMvcConfigparam-value > init-param > <load-on-startup > 1load-on-startup > servlet > <servlet-mapping > <servlet-name > appservlet-name > <url-pattern > /url-pattern > servlet-mapping > <jsp-config > <jsp-property-group > <url-pattern > *.jspurl-pattern > <page-encoding > UTF-8page-encoding > <include-prelude > /WEB-INF/include.jspinclude-prelude > jsp-property-group > jsp-config > web-app >
🚕 AppConfig A configuration for the specific app is in src/main/java/example/config/AppConfig.java
:
package example.config;import org.springframework.context.annotation.Configuration;@Configuration public class AppConfig {}
🎃 WebMvcConfig A configuration for Spring Mvc is in src/main/java/example/config/WebMvcConfig.java
:
package example.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration @EnableWebMvc @ComponentScan ("example.app" )public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void configureViewResolvers (ViewResolverRegistry registry) { registry.jsp(); } }
😀 Taglibs for view You should write list of Taglibs
which you want to use like this:
<%@ taglib prefix ="c" uri ="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix ="spring" uri ="http://www.springframework.org/tags" %> <%@ taglib prefix ="form" uri ="http://www.springframework.org/tags/form" %>
You can use the tags in your all view jsp files.
🎉 WelcomeController A controller(src/main/java/example/app/WelcomeController.java
) to get a request in MVC is as follows:
package example.app;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;@Controller public class WelcomeController { @RequestMapping ("/" ) public String home () { return "index" ; } }
🐡 View A view for the helloController#home
is in src/webapp/WEB-INF/index.jsp
like this:
<html > <body > <h1 > Hello World!h1 > body > html >
🐰 Run Hello AppConfig You can run your web app by the following command:
So, you can see your hello world apps:
🐹 Sample source morizyun/spring_web_mvc_sample_codes: Sample Codes by Spring Web MVC
🖥 Recommended VPS Service
VULTR provides high performance cloud compute environment for you.
Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour).
In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results !!