SpringMVC框架学习(三)——IDEA不使用骨架搭建SpringMVC环境

SpringMVC的实现方式2

不使用idea的骨架创建一个SpringMVC的入门项目,具体步骤如下:

1、创建一个新的maven工程
在这里插入图片描述
不选骨架,直接next
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
创建完成如图所示,默认是一个JavaSE工程,而我们需要的是一个web工程,所以还需要进行一些配置:

2、web工程配置
(1)、在pom文件中声明打包方式为war包,也就是说项目最终要打包为war包。
在这里插入图片描述
(2)、创建webapp目录
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
剪切\WEB-INF\web.xml至webapp目录下
在这里插入图片描述
在这里插入图片描述
基本搭建完成
在这里插入图片描述
3、添加依赖
在这里插入图片描述
4、创建配置文件
在这里插入图片描述
注意:一定要先加依赖,再添加配置文件,否则没有Spring Config这个选项
在这里插入图片描述
spring容器一般来扫描controller之外的目录,Spring容器和SpringMVC容器是两个容器,SpringMVC是Spring的子容器,子容器可以访问父容器里的东西,所以SpringMVC可以访问到Spring中的东西,Spring不可以访问到SpringMVC中的东西,所以一般来说我们在Spring中扫描controller之外的目录,SpringMVC中扫描controller就可以了。

配置spring的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配置中的use-default-filters用来指示是否自动扫描带有@Component、@Repository、@Service和@Controller的类。默认为true,即默认扫描。-->
    <context:component-scan base-package="com.yunzhong" use-default-filters="true">
   <!--不包含controller-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

配置springmvc的springmvc.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/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!--都不扫描,但扫描controller-->
    <context:component-scan base-package="com.yunzhong" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
   <!--作用:将reques 请求上面的数据绑定到controller 上-->
    <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>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--SpringMVC的配置-->
    <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:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

创建一个controller:

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello() {
        return helloService.hello();
    }
}

创建一个service:

@Service
public class HelloService {
    public String hello() {
        return "hello world yunzhongmanbu";
    }
}

配置tomcat并加载项目:
在这里插入图片描述
在这里插入图片描述
启动tomcat,并访问http://localhost:8080/hello

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值