当前位置: 首页 > 文档资料 > Spring 中文教程 >

Spring MVC Hello World Example

优质
小牛编辑
126浏览
2023-12-01

以下示例说明如何使用Spring MVC框架编写基于Web的简单Hello World应用程序。 首先,让我们使用一个有效的Eclipse IDE,并采取以下步骤使用Spring Web Framework开发动态Web应用程序 -

脚步描述
1使用名称HelloWeb创建一个Dynamic Web Project ,并在创建的项目中的src文件夹下创建一个包cn.xnip
2将下面提到的Spring和其他库拖放到WebContent/WEB-INF/lib文件夹中。
3cn.xnip包下创建一个Java类HelloController
4WebContent/WEB-INF文件夹下创建Spring配置文件web.xmlHelloWeb-servlet.xml
5WebContent/WEB-INF文件夹下创建名为jsp的子文件夹。 在此子文件夹下创建一个视图文件hello.jsp
6最后一步是创建所有源文件和配置文件的内容,并导出应用程序,如下所述。

这是HelloController.java文件的内容

package cn.xnip;
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";
   }
}

以下是Spring Web配置文件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>

以下是另一个Spring Web配置文件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 = "cn.xnip" />
   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>
</beans>

以下是Spring视图文件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文件夹中即可。

  • commons-logging-x.y.z.jar
  • org.springframework.asm-x.y.z.jar
  • org.springframework.beans-x.y.z.jar
  • org.springframework.context-x.y.z.jar
  • org.springframework.core-x.y.z.jar
  • org.springframework.expression-x.y.z.jar
  • org.springframework.web.servlet-x.y.z.jar
  • org.springframework.web-x.y.z.jar
  • spring-web.jar

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

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

Spring Web Hello World

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