Spring MVC -02- 第一个 Spring MVC 应用 Hello world!

版权声明:本文为博主原创文章,欢迎转载,转载请注明出处 https://blog.csdn.net/qq_40147863/article/details/85323413

Spring MVC -02- 第一个 Spring MVC 应用 Hello world!

Spring MVC 当前的最新版是 5.1.3
Go to Spring MVC 官网

Spring MVC 的下载与安装

Spring 是一个独立的框架,它不需要依赖于任何的 Web 服务器或者容器。它既可以在独立的 Java EE 项目中使用,也可以在 Java Web 项目中使用。

下面介绍如何为 Java 项目和 Java Web 项目添加 Spring 支持

下载和安装 Spring 框架可按如下步骤进行:

(1)打开 https://repo.spring.io/libs-release-local/org/springframework/spring/ 站点,下载 Spring 的最新稳定版

2.下载完成,得到一个 spring-framework 5.x.x RELEASE-DIST zip 压缩文件,解压该压文件得到一个名为 sping- framework 5.x.x RELEASE 文件夹,该文件夹下有如下几个子文件夹:

  • docs。该文件夹下存放 Spring的相关文档,包含开发指南、API参考文档。
  • bs。该文件夹下的 JAR 分为三类:① Spring框架 class文件的 JAR 包;② Spring 框源文件的压缩包,文件名以- source 结尾;③ Spring 框架 API 文档的压缩包,文件名以 -javadoc 结尾。
  • schemas。该文件夹下包含了 Spring 各种配置文件的 XML Schema 文档。
  • readme.txt、 notice.txt、 license.txt 等说明性文档。

3.将 libs 文件夹下所需模块的 class 文件的 JAR 包复制添加到项目的类加载路径中,既可以通过加环境变量的方式来添加,也可使用 Amt 或 IDE 工具来管理应用程序的类加载路径。

如果需要发布该应用,则将这些 JAR 包一同发布即可。如果没有太多要求,建议将 libs 文件夹下所有模块的 class 文件的 JAR 包添加进去。

4.除此之外, Spring 的核心容器必须依赖于 common- logging 的 JAR 包,因此还应该录 http://commons.apache.org/proper/commons-logging/download_logging.cgi 站点,下载最新 commons- logging 工具,下载完成得到一个 commons-logging-12- bin. zip 压缩文件,将该文件解压路径下的 commons-logging-i2jar 也添加到项目的类加载路径中。

完成上面4个步骤后,接下来即可在 Java Web 应用中使用 Spring MVC 框架了。

!!! 示例:第一个 Spring MVC 应用 Hello world!

先看一下项目完整目录,很多人都错在目录上:

扫描二维码关注公众号,回复: 4750592 查看本文章

创建下项目,增加 Spring 的支持

  • (1)使用 Eclipse 新建一个 Dynamic Web Project,也就是新建一个动态 Web 项目,命名为 SpringMVC01:
  • (2)将 Spring 所依赖的 commons-logging-1.2.jar 复制 Web 上述目录,同样【右键】>【Add to path】。

  • (3)为了让 web 应用具有 Spring 支持的功能:
    将 sping- framework RELEASE 解压文件中的 libs 文件夹下所有 jar 包,复制到 WebContent\WEB-INF\lib 目录下。

都看到这里了,也可以按照上述方法进行,除了上述方法外,还有一个更方便的,就是当多个 jar 包引入时会非常比便捷!

在这里插入图片描述

  • (4)配置 web.xml 文件

删除全部内容后,粘贴下面代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>
  
    <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-config.xml</param-value>
        </init-param>
        <!-- <load-on-startup>1</load-on-startup> -->
  </servlet>

  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>
  • (5)配置 Spring MVC 的 Controler
    默认是没有这个文件的,需要自己创建 xml 文件(放在 src 目录下),命名必须为:springmvc-config.xml ,粘贴下面内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">                    

    <!-- scan the package and the sub package -->
    <context:component-scan base-package="test.SpringMVC"/>

    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />

    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />
    
    <!-- configure the InternalResourceViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            id="internalResourceViewResolver">
        <!-- 前缀 要自己新建 jsp 目录,注意位置-->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
  • (6)Controller 类的实现
    在 src 目录,新建 test.SpringMVC 包,中 mvcController.java 类
package test.SpringMVC;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class mvcController {
	
	@RequestMapping("/hello")
    public String hello(){
        
		// hello 和 jsp 的文件名一致!
        return "hello";
    }

}

  • (7) /WEB-INF/jsp/ 目录下,新建 hello.jsp (名字必须这样,不要上面有内容需要修改):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h2>Test </h2>
<hr>
<h3>Hello Xiaopengwei !</h3>
<h3>博客:https://blog.csdn.net/qq_40147863</h3>

</body>
</html>
  • (8)右键在 jsp 文件,右键 Run on Server!
  • (9)查看效果:

更多文章链接:

猜你喜欢

转载自blog.csdn.net/qq_40147863/article/details/85323413