当前位置:首页 > 技术分析 > 正文内容

如何搭建SpringMVC的最小化工程(springmvc工程搭建步骤)

Spring是一个轻量级的Java开源框架,它是一个Full-Stack(全栈)的分层式的Java SE/EE框架。开发者可以很方便地使用Spring的IoC(反转控制)、AOP(面向切面)和MVC(模型-视图-控制器)等功能特性。本文会在Eclipse中搭建一个最小化的SpringMVC工程,以作为后续Spring学习的入门教程。

一、环境描述

二、添加服务器

启动Eclipse,然后访问菜单Window → Preferences,然后在打开的首选项窗口中,打开Server → Runtime Environments,如下图所示:

点击上图中的Add按钮,然后在打开的新建服务器运行时环境的窗口中选择Apache → Apache Tomcat v8.0,然后点击Next按钮,如下图所示:

在下一步的窗口中,填写Tomcat服务器的名称和安装目录。本文将服务器名称指定为“Apache Tomcat v8.0”,安装目录指定为“/usr/local/Tomcat”,最后点击Finish按钮,如下图所示:

此时,会返回Eclipse的首选项窗口,点击OK按钮便能成功添加Tomcat服务器了,如下图所示:

三、新建和配置Java工程

在Eclipse中访问菜单File → New → Java Project,在新建Java工程的窗口中指定工程名为“SpringTest”,指定执行的JRE为“JavaSE-1.8”,然后点击Finish按钮,如下图所示:

现在可以在Eclipse的Package Explorer视图中看到“SpringTest”工程了。在工程名上点击鼠标右键,在右键菜单中选择Properties(或者按下Alt+Enter快捷键),打开工程的属性窗口,选择该窗口中的Project Facets,如下图所示:

因为默认的Java工程并没有被配置为使用Project Facets,所以此处需要点击Convert to faceted form...,添加对Project Facet的支持。然后,在Project Facet列表进行如下配置:

  • 将Java的版本指定为1.8;

  • 将Dynamic Web Module的版本指定为3.1;

  • 将JavaScript的版本指定为1.0。

Project Facet的配置如下图所示,然后点击OK按钮:

Project Explorer视图中,右键点击工程名称,然后在右键菜单中访问Configure → Convert to Maven Project,如下图所示:

在打开的创建POM的窗口中,工程的Maven配置如下图所示:

点击上图中的Finish按钮,工程的pom文件便创建完成了。

在Eclipse中打开和编辑工程的/SpringTest/pom.xml文件,添加对spring-webmvcspring-context的依赖关系,该文件内容最终如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>SpringTest</groupId>

<artifactId>SpringTest</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>war</packaging>

<dependencies>

<!-- springframework begins -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.2.4.RELEASE</version>

</dependency>

</dependencies>

<build>

<sourceDirectory>src</sourceDirectory>

<plugins>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

<plugin>

<artifactId>maven-war-plugin</artifactId>

<version>2.4</version>

<configuration>

<warSourceDirectory>WebContent</warSourceDirectory>

<failOnMissingWebXml>false</failOnMissingWebXml>

</configuration>

</plugin>

</plugins>

</build>

</project>

在Eclipse中打开工程的
/SpringTest/WebContent/WEB-INF
目录,删除该目录下的lib子目录。然后,在WEB-INF目录中新建web.xml文件,该文件的内容如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

<!-- 默认是
/WEB-INF/applicationContext.xml -->

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

<servlet>

<servlet-name>SpringMVC</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>

<!-- 默认是/WEB-INF/[servlet名字]-servlet.xml -->

</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>

web.xml文件还指定了applicationContext.xml文件和SpringMVC-servlet.xml文件的路径。在WEB-INF目录中新建SpringMVC-servlet.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:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- 设置使用注解的类所在的jar包 -->

<context:component-scan base-package="hello" />

</beans>

SpringMVC-servlet.xml文件将Spring注解组件的扫描根目录设置为hello包。在WEB-INF目录中新建applicationContext.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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd">

</beans>

因为这个最小化工程没有用到任何Bean,所以applicationContext.xml文件中的配置是空的。

若工程提示xml文件有错误,则需要在Package Explorer视图中右键点击工程名称,然后在右键菜单中访问Maven → Update Project选项,更新工程的Maven依赖关系即可,如下图所示:

在工程的src目录中新建一个名为“hello”的包,然后在这个包中再新建一个名为“HelloController”的类文件,该文件的内容如下所示:

package hello;

import org.springframework.stereotype.Controller;

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

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class HelloController {

private int count = 0;

@RequestMapping("hello")

public @ResponseBody String test() {

return String.format("hello, world! This comes from spring, visited %d times!", ++count);

}

}

至此,SpringMVC的最小化工程已经创建和配置完毕了。

四、启动应用

在Eclipse中访问菜单Window → Show View → Servers,打开Servers视图,如下图所示:

点击servers视图中的No servers are available. Click this link to create a new server,为工程新建的服务器配置如下图所示:

点击Next按钮,然后将下一步窗口左侧中的“SpringTest”资源添加到窗口的右侧,如下图所示:

此时,便能在Servers视图中看到这个工程使用的Tomcat服务器了,右键点击服务器名称,然后选择右键菜单的DebugStart命令,即可调试或运行这个最小化工程了。

打开浏览器,然后在地址栏中输入以下URL:

http://localhost:8080/SpringTest/hello

打开的测试页面如下图所示:

至此,SpringMVC的最小化工程已经运行验证完毕了!

扫描二维码推送至手机访问。

版权声明:本文由ruisui88发布,如需转载请注明出处。

本文链接:http://www.ruisui88.com/post/3917.html

标签: maven打war包
分享给朋友:

“如何搭建SpringMVC的最小化工程(springmvc工程搭建步骤)” 的相关文章

总结了Vue3的七种组件通信方式,别再说不会组件通信了

写在前面本篇文章是全部采用的<script setup>这种组合式API写法,相对于选项式来说,组合式API这种写法更加自由,具体可以参考Vue文档对两种方式的描述。本篇文章将介绍如下七种组件通信方式:propsemitv-modelrefsprovide/injecteventBusv...

使用cgroup限制进程资源

这里使用containerd项目中的cgroup包来实现进程资源限制。先写一个耗费一个CPU并且一秒增加10m内存的测试进程package mainimport ( "fmt" "math/rand" "time")func main() { go f...

多项修正 尼康D4s发布最新1.10版固件

尼康公司与2014年8月27日发布了D4s的最新固件,固件版本号为C:1.10。这次固件升级,主要解决了一些BUG,并且对拍摄菜单与相机操作做了一定调整。下面是本次新固件的具体信息:尼康发布D4s最新C固件 1.10版对C固件升级到1.10版所作的修改:当选定运动VR模式并换上 AF-S 尼克尔 4...

vue 开发规范

项目运行指南(#项目运行指南)开发本地环境(#开发本地环境)开发相关插件/工具(#开发相关插件工具)开发规范(#开发规范)vue(#vue)【数据流向】(#数据流向)【慎用全局注册】(#慎用全局注册)【组件名称】(#组件名称)【组件中的 CSS】(#组件中的-css)【统一标签顺序】(#统一标签顺序...

基于 vue3.0 小程序拖拽定制

今天给大家分享一个使用Vue3编写的自由DIY小程序页面。mbDIY 一款基于vue3.x构建的可拖拽定制小程序模板。支持新建页面、自由拖拽模块、复制/移动、自定义模块样式等功能。整个项目分为页面、模块、控件三大部分。模块里面的组件可拖拽至主面板区,编辑后保存即可预览效果。快速安装# 克隆项目 gi...

vue开发微信小程序 - 登录组件

移动端登录功能抽象为通用组件,满足:不同移动端应用中一键登录功能复用支持多种登录:微信登录、H5、QQ登录登录组件使用//引用登录组件 import login from "../components/user/login.vue" export default { compone...