Spring MVC Hello World 例子

2023-06-26 15:24 更新

一、運行環(huán)境:

1、操作系統(tǒng):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創(chuàng)建步驟

1、創(chuàng)建一個名稱為 HelloWeb 的動態(tài) Web 項目




2、在已創(chuàng)建的項目的 src 文件夾中創(chuàng)建一個包 com.tutorialspoint


3、將庫文件拖拽到文件夾 WebContent/WEB-INF/lib 中。



 然右鍵點擊Helloweb項目\刷新。




 4、在 com.tutorialspoint 包下創(chuàng)建一個 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 文件夾下創(chuàng)建 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 文件夾下創(chuàng)建 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 文件夾下創(chuàng)建名稱為 jsp 的子文件夾。在這個子文件夾下創(chuàng)建一個視圖文件 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 創(chuàng)建一個名稱為 HelloWeb 的動態(tài) Web 項目,并且在已創(chuàng)建的項目的 src 文件夾中創(chuàng)建一個包 com.tutorialspoint。
2 將上面提到的 Spring 和其他庫拖拽到文件夾 WebContent/WEB-INF/lib 中。
3 在 com.tutorialspoint 包下創(chuàng)建一個 Java 類 HelloController。
4 在 WebContent/WEB-INF 文件夾下創(chuàng)建 Spring 的配置文件 Web.xml 和 HelloWeb-servlet.xml。
5 在 WebContent/WEB-INF 文件夾下創(chuàng)建名稱為 jsp 的子文件夾。在這個子文件夾下創(chuàng)建一個視圖文件 hello.jsp。
6 最后一步是創(chuàng)建所有的源代碼和配置文件的內(nèi)容,并導出該應用程序,正如下面解釋的一樣。

這是 HelloController.java 文件的內(nèi)容:

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 的內(nèi)容

<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 的內(nèi)容

<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 的內(nèi)容

<%@ 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

一旦你完成了創(chuàng)建源代碼和配置文件后,導出你的應用程序。右鍵單擊你的應用程序,并且使用 Export > WAR File 選項,在 Tomcat 的 webapps 文件夾中保存你的 HelloWeb.war 文件。

現(xiàn)在啟動你的 Tomcat 服務器,并且確保你能夠使用標準的瀏覽器訪問 webapps 文件夾中的其他 web 頁面。現(xiàn)在嘗試訪問該 URL http://localhost:8080/HelloWeb/hello。如果你的 Spring Web 應用程序一切都正常,你應該看到下面的結果:

你應該注意,在給定的 URL 中,HelloWeb 是這個應用程序的名稱,并且 hello 是我們在控制器中使用 @RequestMapping("/hello") 提到的虛擬子文件夾。當使用 @RequestMapping("/") 映射你的 URL 時,你可以使用直接 root,在這種情況下,你可以使用短 URL http://localhost:8080/HelloWeb/ 訪問相同的頁面,但是建議在不同的文件夾下有不同的功能。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號