如何搭建一个传统的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

出现如上图所示,即表明环境搭建完成。

原文地址:http://www.cnblogs.com/atwood-pan/p/16920201.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性