1.创建maven项目
2. 点击next
3.点击next
4. 点击finish
5. 在创建好的maven项目的pom.xml文件中添加依赖包,下面这些是可能用到的jar包,有些可能多余的,但是不影响。如果想要导入自己想要的版本的包,就百度搜索maven,点击第一个,就进入到maven的官方的仓库中。在这里面可以搜索到各种各样的所有版本的jar包,打开你想要的,把里面的代码复制到pom.xml的标签里面即可。
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aspectj/aspectjrt -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
6.刚新建的项目的目录是不完整的,所以需要自己添加,楼主创建的目录如下,文件夹的名字是可以随便起的
7.在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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!--使用SpringMVC,配置DispatcherServlet是第一步,DispatcheServlet是一个Servlet,所以可以配置多个DispatcherServlet-->
<!--DispatcherServlet是前置控制器,配置在web.xml文件中,拦截匹配的请求,Servlet拦截匹配规则要自己定义,把拦截下来的请求
,依据某某规则分发到目标Controller(我们写的Action)来处理。-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认的加载方式 * 文件命名: <servlet-name>-servlet.xml ->springmvc-servlet.xml
* 路径规范:必须在WEB-INF下 ,查找配置文件springmav.xml(这个名字是文件的名字)-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern><!--拦截URL中带“/”的请求-->
</servlet-mapping>
<!--编码过滤器 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
8.在java目录下创建一个文件resources,这是一个配置文件夹,在创建后需要修改文件夹的类型。修改步骤如下,File->Project Structure…–>Modules->springmvc->Sources->选择你要修改的文件的->选择要修改的类型->OK
9.在resources文件夹里面添加一个springmvc.xml文件夹,代码如下
注意:base-package里面的包名,是你对应创建的controller文件夹,如何不存在这个文件夹会报错。
<?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-4.3.xsd">
<!-- 开启注解扫描 -->
<context:component-scan
base-package="com.lss.controller"></context:component-scan>
<!-- 配置注解处理器映射器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置注解处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 配置springmvc的视图解析器:解析逻辑视图 假设要跳转到某个jsp jsp是放在 /WEB-INF/jsps/index.jsp
(物理地址,物理视图) localhost:8080/项目名/index(逻辑地址,逻辑视图) localhost:8080/项目名/WEB-INF/jsps/index.jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
10.在java目录下创建一个controller目录,这个目录存放后台的数据处理的类。在目录下创建一个MyController.java文件。代码如下
package com.lss.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping("nihao")
public String nihao(){
return "index";
}
}
11.添加Tomcat,按下图点击后,点击“+”,选择“Tomcat server”->local,进入第二页面。点击Deployment,进入第三张图片,点击右边“+”号,选择后面为exploded的那个。Tomcat配置完成
12.运行程序,在浏览器输入地址,前面是默认生成的,nihao是MyController中的字符串。