Spring MVC - Hello World示例

以下示例演示如何使用Spring MVC Framework编写基于Web的简单Web Hello World 应用程序.首先,让我们使用一个可用的Eclipse IDE,并按照后续步骤使用Spring Web Framework开发动态Web应用程序.

Step描述
1创建一个名为 HelloWeb 的动态Web项目并创建一个包com .it1352在创建项目的src文件夹下.
2将以下Spring和其他库拖放到文件夹 WebContent/WEB-INF/lib ..
3在com.it1352包下创建一个Java类 HelloController .
4创建Spring配置文件WebContent/WEB-INF文件夹下的web.xml HelloWeb-servlet.xml .
5在WebContent/WEB-INFfolder下创建一个名为 jsp 的子文件夹.在此子文件夹下创建一个视图文件 hello.jsp .
6最后一步是创建源文件和配置文件的内容并导出应用程序,如下所述.

HelloController.java

package com.it1352; 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{
 
   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

web.xml

<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>Spring MVC 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>

HelloWeb-servlet.xml

<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.IT屋" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>
 
</beans>

hello.jsp

<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Hello World</title>
   </head>
   <body>
      <h2>${message}</h2>
   </body>
</html>

以下是要包含在Web应用程序中的Spring和其他库的列表.我们只需将这些文件拖放到 -   WebContent/WEB-INF/lib 文件夹中.

  • servlet-api-xyzjar

  • commons-logging-xyzjar

  • spring-aop-xyzjar

  • spring-beans-xyzjar

  • spring -context-xyzjar

  • spring-core-xyzjar

  • spring-expression -xyzjar

  • spring-webmvc-xyzjar

  • spring-web-xyz jar

完成创建源文件和配置文件后,导出应用程序.右键单击您的应用程序,使用 Export →  WAR文件选项并将 HelloWeb.war 文件保存在Tomcat的 webapps 文件夹中.

现在启动Tomcat服务器并确保您可以使用标准浏览器从webapps文件夹访问其他网页.现在,尝试访问URL :http://localhost:8080/HelloWeb/hello.如果使用Spring Web Application一切正常,我们将看到以下屏幕.

Spring Web Hello World

您应该注意,在给定的URL中, HelloWeb 是应用程序名称,hello是虚拟子文件夹,我们在控制器中使用@RequestMapping("/你好").使用 @RequestMapping("/")映射URL时可以使用直接root,在这种情况下,您可以使用短URL访问同一页面 http://localhost:8080/HelloWeb/,但建议在不同的文件夹下使用不同的功能.