首页 > 编程语言 > 详细

SpringMVC HelloWorld example

时间:2019-06-05 10:32:24      阅读:82      评论:0      收藏:0      [点我收藏+]

In this article, I am going to share how to build the first simple HelloWorld Example using SpringMVC framework. 

 Execution result:

技术分享图片

 

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>

 

SpringMVC HelloWorld example

原文:https://www.cnblogs.com/codingyangmao/p/10977284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!