多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

中國最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2

wkspring教程

Spring MVC Hello World 例子

閱讀 (2339)

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

下面是 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/ 訪問相同的頁面,但是建議在不同的文件夾下有不同的功能。

關閉
程序員人生
主站蜘蛛池模板: 国产一区私人高清影院 | 在线播放69热精品视频 | aⅴ天堂网| 在线播放亚洲视频 | 久久天天躁狠狠躁夜夜2020一 | 欧美亚洲综合视频 | 亚洲精品一区二区三区四区 | 欧美成人一区二区 | 99re热久久| 亚洲欧美日韩久久精品第一区 | 免费国产一区二区三区四区 | 国产精品揄拍100视频 | 网友自拍视频悠悠在线 | 老司机午夜免费福利视频 | 久久免费精品国产72精品剧情 | 精品国产福利在线观看网址2022 | 亚洲一区二区精品推荐 | 337p日本大胆欧美人术艺术精品 | 日韩精品亚洲人成在线播放 | 亚洲欧美日韩精品久久亚洲区色播 | 一级aa免费视频毛片 | 亚洲欧洲无码一区二区三区 | 最近中文版字幕在线观看 | 国产欧美日韩综合在线一 | 欧美高清免费一级在线 | 国产成人久久久精品毛片 | 亚洲91av| 嫩草影院久久精品 | 亚洲图片小说综合 | 毛片国产 | 久久精品女人毛片国产 | yellow中文字幕官网是什么 | 伊人久久五月天综合网 | 中文字幕精品在线视频 | 欧美一页| 伊人网成人| 国产精品久久一区一区 | 日本一区二区在线不卡 | xxfree性人妖hd | 亚洲欧美综合国产精品一区 | 亚洲视频免|