Spring MVC - HelloWorld 示例



以下示例演示了如何使用 Spring MVC 框架编写简单的基于 Web 的HelloWorld应用程序。首先,让我们准备好一个可用的 Eclipse IDE,并按照后续步骤使用 Spring Web 框架开发一个动态 Web 应用程序。

步骤 描述
1 创建一个名为HelloWeb的动态 Web 项目,并在创建的项目中的 src 文件夹下创建一个 com.tutorialspoint 包。
2 将以下 Spring 及其他库拖放到WebContent/WEB-INF/lib文件夹中。
3 在 com.tutorialspoint 包下创建一个名为HelloController的 Java 类。
4 在 WebContent/WEB-INF 文件夹下创建 Spring 配置文件web.xmlHelloWeb-servlet.xml
5 在 WebContent/WEB-INF 文件夹下创建一个名为jsp的子文件夹。在这个子文件夹下创建一个视图文件hello.jsp
6 最后一步是创建源代码和配置文件的内容,并按照以下说明导出应用程序。

HelloController.java

package com.tutorialspoint;

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.tutorialspoint" />

   <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-x.y.z.jar

  • commons-logging-x.y.z.jar

  • spring-aop-x.y.z.jar

  • spring-beans-x.y.z.jar

  • spring-context-x.y.z.jar

  • spring-core-x.y.z.jar

  • spring-expression-x.y.z.jar

  • spring-webmvc-x.y.z.jar

  • spring-web-x.y.z.jar

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

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

Spring Web Hello World

您应该注意,在给定的 URL 中,HelloWeb是应用程序名称,hello 是虚拟子文件夹,我们在控制器中使用 @RequestMapping("/hello") 提到了它。您可以使用直接根目录映射您的 URL,使用@RequestMapping("/"),在这种情况下,您可以使用简短 URL https://:8080/HelloWeb/访问同一页面,但建议在不同的文件夹下具有不同的功能。

广告
© . All rights reserved.