SpringMVC HelloWorld example
In this article, I am going to share how to build the first simple HelloWorld Example using SpringMVC framework.
Execution result:
Workflow of SpringMVC:
(to Save time, I will not re-draw this workflow image, reference from: ekgc.cn)
In SpringMVC, the most important component is DispatcherServlet:
All the requests will be handled by DispatcherServlet first.
Then, it will go to HandlerMapping, and get an object Handler,
through the help of Handler, SpringMVC will know which Controler to call for the in-coming request URL.
Controller will then return object ModelAndView, in this object, it contains the information of our data, and information on which view to call
Step1. Create a Dynamic Web Project.
The structure of the Project is shown as below.
Step2. Import the needed jars
Step 3. Configue web.xml . This means, that we are letting SPRING MVC to handle all the request.
Through:
org.springframework.web.servlet.DispatcherServlet
<web-app id = "WebApp_ID" version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>HelloWeb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWeb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Step 4. config {servlet-name}-servlet.xml.
in our case it is HelloWeb-servlet.xml.
This file is letting the SPRINGMVC know which requests is for which Controller.
SpringMVC will search in the package "com.yang" for controllers, and it each controller will be mapped to certain requests.
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package = "com.yang" /> <bean id ="viewresolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value = "/WEB-INF/jsp/" /> <property name = "suffix" value = ".jsp" /> </bean> </beans>
Step 5. definine our controller.
It is mapped to “”/helloworld“”
package com.yang;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController{
@RequestMapping("/helloworld")
public ModelAndView printHello(HttpServletRequest req, HttpServletResponse rep ) {
ModelAndView mv = new ModelAndView();
mv.addObject("message", "Hello Spring MVC Framework!");
mv.setViewName("success");
return mv;
}
}
in the Console, we could see that the controller has been indeed successfully mapped to the defined url.
信息: Initializing Spring FrameworkServlet 'HelloWeb' 六月 05, 2019 10:33:00 上午 org.springframework.web.servlet.DispatcherServlet initServletBean 信息: FrameworkServlet 'HelloWeb': initialization started 六月 05, 2019 10:33:01 上午 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh 信息: Refreshing WebApplicationContext for namespace 'HelloWeb-servlet': startup date [Wed Jun 05 10:33:01 CST 2019]; root of context hierarchy 六月 05, 2019 10:33:01 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from ServletContext resource [/WEB-INF/HelloWeb-servlet.xml] 六月 05, 2019 10:33:01 上午 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 信息: Mapped URL path [/helloworld] onto handler 'helloController' 六月 05, 2019 10:33:01 上午 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 信息: Mapped URL path [/helloworld.*] onto handler 'helloController' 六月 05, 2019 10:33:01 上午 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler 信息: Mapped URL path [/helloworld/] onto handler 'helloController' 六月 05, 2019 10:33:02 上午 org.springframework.web.servlet.DispatcherServlet initServletBean 信息: FrameworkServlet 'HelloWeb': initialization completed in 1156 ms 六月 05, 2019 10:33:02 上午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-nio-8080"] 六月 05, 2019 10:33:02 上午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-nio-8009"] 六月 05, 2019 10:33:02 上午 org.apache.catalina.startup.Catalina start 信息: Server startup in 10286 ms
Success.jsp
<html> <body> <h2>Hello World!</h2> ${message} </body> </html>