Spring基础 专题
专题目录
您的位置:java > Spring基础专题 > Spring MVC Hello World 例子
Spring MVC Hello World 例子
作者:--    发布时间:2019-11-20

下面的例子说明了如何使用 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/ 访问相同的页面,但是建议在不同的文件夹下有不同的功能。

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册