新建一个Maven
项目
![image-20200806192838818](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806192838818.png)
![image-20200806192854967](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806192854967.png)
填写项目名
![image-20200806192952866](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806192952866.png)
引入依赖
<?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>webdemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <org.springframework.version>4.3.19.RELEASE</org.springframework.version> <commons-logging.version>1.2</commons-logging.version> <junit.version>4.12</junit.version> <slf4j.version>1.6.4</slf4j.version> <lombok.version>1.18.10</lombok.version> <jackson.version>2.8.2</jackson.version> </properties>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.4</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>${commons-logging.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
|
![image-20200806194056324](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806194056324.png)
然后新建目录和web.xml
![image-20200806194244660](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806194244660.png)
![image-20200806194851987](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806194851987.png)
然后编写配置
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="3.1" metadata-complete="true">
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-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:spring/springmvc.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>
|
然后我们发现springmvc
的配置文件没有,去建一个
![image-20200806195057436](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806195057436.png)
![image-20200806195206579](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806195206579.png)
编写配置
<?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-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.ruben.controller"/>
<mvc:annotation-driven/> <mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean>
<mvc:resources location="/static/" mapping="/static/**"/> </beans>
|
![image-20200806203510208](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806203510208.png)
然后创建我们的controller
并编写代码
目录在这里,完整代码放在git
仓库里
![image-20200806225636214](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806225636214.png)
然后是配置tomcat
,我们可以先打个war
包
![image-20200806204941326](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806204941326.png)
编译成功后开始配置tomcat
![image-20200806205343713](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806205343713.png)
点击添加配置
![image-20200806205421133](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806205421133.png)
![image-20200806205556308](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806205556308.png)
点击修复
![image-20200806205618791](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806205618791.png)
![image-20200806205654597](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806205654597.png)
![image-20200806205707393](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806205707393.png)
然后点击确定就可以运行了
![image-20200806205736298](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806205736298.png)
然后访问
http://localhost:8080/
发现能跳转到页面了
![image-20200806210154031](https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/image-20200806210154031.png)