1.第一步首先创建一个maven工程
2.创建成功后默认打开pom文件,选择Enable Auto Import ,这样默认自动引入我们的依赖,然后打成war包引入spring mvc依赖
3.这里看到IDEA自动吧相关依赖都引入了
5.我们准备导入web.xml配置文件过程如下:选择open models settings
点击web resource directory 创建webapp目录
点击+号,选择web.xml
剪切web.xml路径
放到web.app目录下面
点击ok创建web.xml配置文件
pom文件中我们引入spring mvc依赖后,这里就自动可以选择创建spring 的xml配置,如果pom没有导入spring 依赖这里就无法选择spring的xml配置
我们选择创建spring的配置文件appilicationContext.xml和spring Mvc的配置文件 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"
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的配置文件-->
<!--这个是父容器,我们用来扫描除了controller之外的包-->
<context:component-scan base-package="org.javaboy" use-default-filters="true">
<!--这里我们使用默认和的过滤器,但是controller除外-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
<?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">
<!--spring mvc的配置文件-->
<!--这个是子容器,我们只用来扫描controller,因为子容器可以访问父容器里面的东西,反之父容器不可以访问子容器-->
<context:component-scan base-package="org.javaboy" use-default-filters="false">
<!--这里我们不使用默认的过滤器,但是controller除外-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--名称空间 mvc 开启drivern-->
<mvc:annotation-driven/>
</beans>
然后我们在之前创建的web.xml文件中引入刚刚创建的两个配置文件
<?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_4_0.xsd"
version="4.0">
<!--加载spring的配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--添加spring的监听-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--加载spingmvc的配置-->
<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>
验证我之前的配置,在java下创建controller和service
controller选择RestController注解,注入service选择Autowired注解
package org.javaboy.controller;
import org.javaboy.service.SayHello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
//注入service
@Autowired
SayHello sayHello;
@GetMapping(value = "/hello",produces = "text/html;charset=utf-8")
public String sayHello(){
return sayHello.sayHello();
}
}
package org.javaboy.service;
import org.springframework.stereotype.Service;
@Service
public class SayHello {
public String sayHello() {
return "Hello java boy!";
}
}
完成代码部分,本地部署 点击Edit Configurations,选择tomncat 选择local
这里选择自己本地的tomcat安装目录
点击Deployment 选择+号选择war包部署,修改默认路径为/.OK部署完成。
启动tomcat运行工程