 
- Spring MVC 基础知识
- Spring MVC - 主页
- Spring MVC - 概述
- Spring MVC - 环境设置
- Spring MVC - Hello World 示例
- Spring MVC - 表单处理
- Spring MVC - 表单处理
- Spring MVC - 页面重定向
- Spring MVC - 静态页面
- Spring MVC - 表单标签库
- Spring MVC - 文本框
- Spring MVC - 密码
- Spring MVC - 文本区域
- Spring MVC - 复选框
- Spring MVC - 复选框
- Spring MVC - 单选按钮
- Spring MVC - 单选按钮
- Spring MVC - 下拉菜单
- Spring MVC - 列表框
- Spring MVC - 隐藏
- Spring MVC - 错误
- Spring MVC - 上传
- Spring MVC - 处理程序映射
- Bean 名称 Url 处理程序映射
- 控制器类名处理程序映射
- 简单的 URL 处理程序映射
- Spring MVC - 控制器
- Spring MVC - 多动作控制器
- 属性方法名称解析器
- 参数 方法名称 解析器
- 可参数化视图控制器
- Spring MVC - 视图解析器
- 内部资源视图解析器
- Spring MVC - Xml 视图解析器
- 资源包视图解析器
- 多解析器映射
- Spring MVC - 集成
- Spring MVC - Hibernate 验证器
- Spring MVC - 生成 RSS 提要
- Spring MVC - 生成 XML
- Spring MVC - 生成 JSON
- Spring MVC - 生成 Excel
- Spring MVC - 生成 PDF
- Spring MVC - 使用 log4j
- 春季问答
- 春天 - 问题与解答
Spring MVC - Hello World 示例
以下示例演示如何使用 Spring MVC 框架编写一个简单的基于 Web 的Hello World应用程序。首先,让我们准备一个可用的 Eclipse IDE,并按照后续步骤使用 Spring Web 框架开发动态 Web 应用程序。
| 步 | 描述 | 
|---|---|
| 1 | 创建一个名为HelloWeb的动态Web项目,并在创建的项目中的src文件夹下创建一个包com.tutorialspoint。 | 
| 2 | 将以下 Spring 和其他库拖放到文件夹WebContent/WEB-INF/lib .. | 
| 3 | 在 com.tutorialspoint 包下创建一个 Java 类HelloController 。 | 
| 4 | 在WebContent/WEB-INF文件夹下创建Spring配置文件web.xml和HelloWeb-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";
   }
}
 
网络.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>
你好.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 
- 公共日志记录 xyzjar 
- spring-aop-xyzjar 
- spring-beans-xyzjar 
- spring 上下文 xyzjar 
- 弹簧核心 xyzjar 
- spring 表达式 xyzjar 
- spring-webmvc-xyzjar 
- spring-web-xyzjar 
创建源文件和配置文件后,导出您的应用程序。右键单击您的应用程序,使用Export → WAR File选项并将HelloWeb.war文件保存在 Tomcat 的webapps文件夹中。
现在启动 Tomcat 服务器并确保您能够使用标准浏览器从 webapps 文件夹访问其他网页。现在,尝试访问 URL - http://localhost:8080/HelloWeb/hello。如果 Spring Web 应用程序一切正常,我们将看到以下屏幕。
 
您应该注意,在给定的 URL 中,HelloWeb是应用程序名称,hello 是虚拟子文件夹,我们在控制器中使用 @RequestMapping("/hello") 提到过它。您可以在使用@RequestMapping("/")映射 URL 时使用直接 root ,在这种情况下,您可以使用短 URL http://localhost:8080/HelloWeb/访问同一页面,但建议在下面具有不同的功能不同的文件夹。