Spring-传统方式(XML)创建webapp
如何搭建一个传统的webapp项目【Java后端】
使用xml 来搭建 SSM 环境,要求 Tomcat 的版本必须在 7 以上
QuickStart
1创建工程
创建一个新模块【普通的 Maven 工程】

这里我们选择webapp作为maven的archetype

创建完成之后,项目默认的层级如下:

我们发现根本就没有java目录,这时候就需要手动创建一个java目录:


引入本次项目所需的依赖:

引入依赖之后,创建一个HelloController,该接口用于测试:

/**
* @Author Coder_Pans
* @Date 2022/11/23 13:26
* @PackageName:org.panstack
* @ClassName: HelloController
* @Description: TODO 测试接口
* @Version 1.0
*/
@RestController
public class HelloController {
@GetMapping("hello")
public String hello(){
return "Hello SSM";
}
}
创建一个普通的 Maven 工程(注意,这里可以不必创建 Web 工程),并添加 SpringMVC 的依赖,同时,这里环境的搭建需要用到 Servlet ,所以我们还需要引入 Servlet 的依赖(一定不能使用低版本的 Servlet),最终的 pom.xml 文件如下
<dependencies>
<!-- TODO 纯Java搭建SSM环境 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- junit用于测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
2 添加Spring配置
工程创建成功之后,我们需要在resources中创建配置文件,这里我们需要两个配置文件,一个是Spring的另外一个是servlet接口的

添加一个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:context="http://www.springframework.org/schema/context"
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.xsd">
<!-- 在Spring的配置文件中 使用默认扫描机制,指定扫描的包名 -->
<context:component-scan base-package="org.panstack" use-default-filters="true">
<!-- 这里表示,除了Controller接口,其他都要扫描 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
3添加SpringMVC配置
在添加一个servlet的配置文件spring-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: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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- SpringMVC配置文件,与Spring中的相反:不启用默认扫描,但是要去包括Controller -->
<context:component-scan base-package="org.panstack" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 注意:导入mvc的driven -->
<mvc:annotation-driven/>
</beans>
4配置web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 首先指定Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 添加过滤器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定servlet的配置文件 -->
<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:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5配置tomcat,启动项目
在本地环境安装tomcat(目前使用的版本是apache-tomcat-9.0.69)


创建好之后默认情况下,会让你选择一个项目作为启动的项目:

这里我们点击Deployment,选择:


选择好之后,修改项目的默认访问路径:http://localhost:8080

启动项目

输入请求接口localhost:8080/hello

出现如上图所示,即表明环境搭建完成。
Spring-传统方式(XML)创建webapp的更多相关文章
- 移动开发的捷径:3种方式轻松创建webapp
移动开发行业发展迅速,为迎合用户的需求,大多数传统互联网公司在主导web网站的同时还需兼顾移动开发方向.在已有PC端网站的基础上,考虑到人力.成本和技术.开发周期等因素,许多公司会选择开发快速.维护便 ...
- C#基础巩固(2)-Linq To XML创建XML
一.首先要清楚一个正确的XML基本格式是怎样的. 1.后缀名.xml结尾 2.有一行描述 3.有且仅有一个根节点. 如图: 一个正确的xml文件能够被浏览器打开且显示.所以判断一个xml文件有没有错误 ...
- spring注解和xml方式区别详解
一.spring常规方式. 在使用注释配置之前,先来回顾一下传统上是如何配置 Bean 并完成 Bean 之间依赖关系的建立.下面是 3 个类,它们分别是 Office.Car 和 Boss,这 3 ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- Spring bean三种创建方式
spring共提供了三种实例化bean的方式:构造器实例化(全类名,反射).工厂方法(静态工厂实例化 动态工厂实例化)和FactoryBean ,下面一一详解: 1.构造器实例化 City.jav ...
- 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)
Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...
- Spring IOC 依赖注入的两种方式XML和注解
依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...
- Spring MVC 的 XML 配置方式
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml solution/webapi/pom.xml solution/mapper/ ...
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...
- Spring 中使用XML配置方式和使用注解方式实现DI
Spring容器给我们提供了很好的环境,我们只关注主要业务即可,其他的无需关注太多.今天刚学的DI DI(Dependency Injection):依赖注入 使用XML配置文件完成依赖注入 1.1普 ...
随机推荐
- macOS 10.14安装win10教程 bootcamp篇
由于工作以及系统使用习惯上的原因,拥有Mac电脑的用户常常需要用到windows系统,这个时候我们就需要在Mac上安装双系统来满足这一需求,一起来看看macOS 10.14安装win10教程吧. ma ...
- pg数组类型
数据库版本 postgres=# SELECT version(); version---------------------------------------------------------- ...
- 【STM32】TIM定时器
TIM定时器(TIM3为例) 初始化: A:结构体TIM_HandleTypeDef的成员: 1.*Instance:类型为TIM_TypeDef,即对TIM的寄存器的映射,通过这个成员可以操作寄存器 ...
- vue的易错点合集
关于vue的操作,可以借鉴到一些Ajax的方法和思路,但是因为语法的不一样,所以易错点多在语法. 第一步要引用相对的方法 div的id名称应该与下文的el名称一致 挂载方法created,相当于aja ...
- 微信小程序:微信web开发阶段性学习总结
小程序运行机制 前台/后台状态 小程序启动后,界面被展示给用户,此时小程序处于前台状态. 当用户点击右上角胶囊按钮关闭小程序,或者按了设备 Home 键离开微信时,小程序并没有完全终止运行,而是进入了 ...
- ANSYS Electronics Suite 19.2下载地址及其安装教程
ANSYS Electronics Suite 19.2下载安装教程 1.下载地址https://getintopc.site/ansys-electronics-suite-19-2-free-do ...
- Hive. 函数 instr 的用法
INSTR(C1,C2,I,J) 在一个字符串中搜索指定的字符,返回发现指定的字符的位置; C1 被搜索的字符串 C2 希望搜索的字符串 I 搜索的开始位置,默认为1 J 出现的位置,默认为1 sel ...
- 20220712 第一小组 张明旭 JS学习记录
心情 今天是正式学习的第四天,感觉上午任务不是特别重,身心放松,知识点都掌握的不错:一到下午刚上课就画风突变,内容突然就男了,后来依旧是男上加男... 重点: 四大循环(理解) 内置函数(了解) 在s ...
- 项目实训DAY6
今天主要的工作是把功能界面丰富了一下,查阅了一下论文,将页面中添加了可视化元素:同时决定了最后几天的工作计划.
- 字符串转换为Base64,作为token 传入到后台
一.Base64 转换的方法 function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc ...