Spring MVC Hello World 例子

2023-06-26 15:24 更新

一、运行环境:

1、操作系统:windows 10 x64

2、eclipse版本: Version: 2019-06 (4.12.0) Build id: 20190614-1200

3、Web服务器:apache-tomcat-8.5.47

4、commons-logging-1.2 下载地址:http://archive.apache.org/dist/commons/logging/binaries/#mirrors下载:commons-logging-1.2-bin.zip文件

5、spring-framework-4.1.6 下载地址:https://repo.spring.io/release/org/springframework/spring/4.1.6.RELEASE/下载

spring-framework-4.1.6.RELEASE-dist.zip。如果想其他版本:https://repo.spring.io/release/org/springframework/spring。

二、Hello World创建步骤

1、创建一个名称为 HelloWeb 的动态 Web 项目




2、在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint


3、将库文件拖拽到文件夹 WebContent/WEB-INF/lib 中。



 然右键点击Helloweb项目\刷新。




 4、在 com.tutorialspoint 包下创建一个 Java 类 HelloController,并贴入代码。



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";
   }
}

5、在 WebContent/WEB-INF 文件夹下创建 Spring 的配置文件 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>

6、在 WebContent/WEB-INF 文件夹下创建 Spring 的配置文件 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>

7、在 WebContent/WEB-INF 文件夹下创建名称为 jsp 的子文件夹。在这个子文件夹下创建一个视图文件 hello.jsp,并贴入代码。



 

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

8、配置Web服务器。








配置完成之后会如下显示。


9、启动项目



10、如果不能正常显示上图的页面检查以下配(可能性90%,不知道Eclipse为什么在这儿老是抽风。)




如果还不能正常显示,删除服务器重建。

Spring MVC Hello World 例子

下面再把例子简述一下,说明了如何使用 Spring MVC 框架来编写一个简单的基于 web 的 Hello World 应用程序。

步骤 描述
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";

   }

}

下面是 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="com.tutorialspoint" />

 

   <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 选项,在 Tomcat 的 webapps 文件夹中保存你的 HelloWeb.war 文件。

现在启动你的 Tomcat 服务器,并且确保你能够使用标准的浏览器访问 webapps 文件夹中的其他 web 页面。现在尝试访问该 URL http://localhost:8080/HelloWeb/hello。如果你的 Spring Web 应用程序一切都正常,你应该看到下面的结果:

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


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号