Spring MVC源码解读『Idea如何构建Spring MVC应用』

从工作几乎一直在使用开箱即用的Spring Boot,很少使用Spring MVC。在上篇文章介绍Spring MVC示例中搭建那个HelloWorld demo过程中,还费了一些周折。想着可能很多人的Java Web之旅都是从Spring Boot开启的,本篇文章我们就来介绍一下如何使用Idea构建Spring MVC应用,以及通过Idea部署Spring MVC应用。

1. Idea构建并部署Spring MVC应用

1.1 创建Web应用

这里我们来看一个最简单的问题,如何通过Idea构建并部署Spring MVC应用,代码我们还用上篇文章中介绍的HelloWorld,这里重点关注如何构建并部署的过程。

这里我没有用Spring脚手架搭建,而是通过JavaEE创建Web Application方式 构建的。这样,一个空的Web Application就创建好了,如下:

由于我这里想使用Maven来管理jar,所以接下来为我们的Web应用添加Maven支持。右键应用程序名,选择“Add Framework Support”,勾选“Maven”,并点击确定:

这样我们的应用就变成一个Maven应用了。

1.2 使用Spring MVC开发应用

首先我们在pom.xml中添加Spring依赖,并通过Maven讲相关依赖包加载到应用中来:

这里之所以在“pom.xml”中添加jstl依赖,是因为我们加下来使用到了视图解析器要用到这个包,不然接口调用时就会报错。

然后我们在“src/main/java” Source Root下创建package “com.zhuoli.service.spring.mvc.demo”,并添加HelloController:

然后,我们在“src/main/resources”classPath中添加上篇文章中提到的两个配置文件,spring.xml和Spring-mvc.xml:

spring.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">

    <!--启动DI管理-->
    <context:annotation-config/>

    <!-- 在父容器中不扫描@Controller注解,在子容器中只扫描@Controller注解 -->
    <!--把控制器从包扫描中排除出去-->
    <context:component-scan base-package="com.zhuoli.service.spring.mvc.demo">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

spring-mvc.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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- 启用Spring基于annotation的DI, 使用户可以在Spring MVC中使用Spring的强大功能。
        激活 @Required, @Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 等标注 -->
    <context:annotation-config/>

    <!--包扫描-->
    <!--DispatcherServlet上下文,只管理@Controller的bean,忽略其他类型的bean, 例如@Service-->
    <context:component-scan base-package="com.zhuoli.service.spring.mvc.demo">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值