Loading...
墨滴

楼仔

2021/08/04  阅读:45  主题:橙心

【SpringMVC系列1】SpringMVC + Intellij + Tomcat环境部署

讲述SpringMVC + Intellij IDEA + Tomcat环境搭建。

前言

上周五转岗到小米其它部门,第一件事情就是需要搭建环境,周围同事本来想用Intellij IDEA来搭建环境,但是都失败了,结果后来都用Eclipse,所以想自己倒腾一下,花了3个小时终于把环境搭建好了,特此记录一下。

使用环境

Intellij IDEA 2020.1

JDK 1.8.0_211

spring 4.3.14

Tomcat 8.5.69

Tomcat安装

参考:https://www.jianshu.com/p/69496fb3495e

文章中说的方法,有几个点需要注意:

  1. 文中说是在网址中输入“localhost”,这个其实不对,应该输入“localhost:8080”;
  2. 不一定需要把Tomcat拷贝到/Library/Tomcat中,因为根目录或导致权限问题,为了更简单,我直接把Tomcat放入我本地目录下,比如"/Users/mengloulv/Documents/Java/apache-tomcat-8.5.69",一样用的飞起。

SpringMVC

代码结构

其它代码我都删掉了,只保留最核心的部分:

代码和配置

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-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd"
>
        <!--使用context命名空间,通知spring扫描指定目录,进行注解的解析-->
        <context:component-scan base-package="com.lvmenglou"/>
        <!--启用@Autowired等注解-->
        <context:annotation-config/>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

         version="3.0">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!--DispatcherServlet-->
  <servlet>
    <servlet-name>springmvc-deom</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!--配置XML文件路径 第2步新建的文件-->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc-deom</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

index.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

pom.xml

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

<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>org.example</groupId>
  <artifactId>springmvc-deom</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>demo4 Maven Webapp</name>
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.version>4.3.14.RELEASE</spring.version>
  </properties>

  <dependencies>
    <!-- Spring核心依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- Spring web依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
  </dependencies>

  <build>
    <!-- 如果不加这个,执行时,会找不到*.xml文件,参考:https://blog.csdn.net/wt_better/article/details/90261220 -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.*</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>
</project>

HelloWord.java

@Controller
public class HelloWord {
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        return "My first test!";
    }
}

Tomcat配置

我的Tomcat安装路径为:/Users/mengloulv/Documents/Java/apache-tomcat-8.5.69

这里说明一下Fix中,选择两者的区别:

  • war模式:将WEB工程以包的形式上传到服务器 ;
  • war exploded模式:将WEB工程以当前文件夹的位置关系上传到服务器;

你选择“war exploded模式”后,会在Application context中给出指定路径,如果你在Server中配置的URL为“http://localhost:8080”,那么在Application context需要修改为“/”,这里千万不要加路径,否则到时你访问“http://localhost:8080”时会失败。

启动服务

访问“http://localhost:8080/”:

访问“http://localhost:8080/test”

欢迎大家多多点赞,更多文章,请关注微信公众号“楼仔进阶之路”,点关注,不迷路~~

楼仔

2021/08/04  阅读:45  主题:橙心

作者介绍

楼仔