Spring和SpringMVC整合

10 篇文章 0 订阅

SpringMVC和Spring整合的目的是为了分工明确。例如:SpringMVC的配置文件就来配置和网站转发逻辑以及网站功能有关的配置,如:视图解析器、文件上传解析器、支持ajax…

而Spring的配置文件用来配置和业务有关的,如:事物控制、数据源…

import

可以在resources文件夹下创建三个配置文件:include-config.xmlspring-configspringmvc-confg

include-config.xml配置文件中使用spring提供的import标签引入并合并另外两个配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <import resource="spring-config.xml"/>
  <import resource="springmvc-config.xml"/>
</beans>

这样就搞定了。。。

Spring和SpringMVC分容器

Spring管理业务逻辑组件,SpringMVC管理控制器组件

SpringMVC配置文件中指定扫描对象并且禁用默认规则

  • type 指定排除方式
<context:component-scan base-package="top.ctong.springmvc" use-default-filters="false">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

Spring 配置文件中指定接管所有业务逻辑组件

<context:component-scan base-package="top.ctong.springmvc">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

最后需要在web.xml文件中启动两个容器

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring-config.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
SpringSpring MVC整合的基本思路是将Spring作为Spring MVC应用程序的核心容器,Spring MVC作为Web层框架,通过配置文件将它们连接在一起。 具体来说,整合的基本步骤如下: 1. 在Spring配置文件中引入Spring MVC配置文件 ```xml <import resource="spring-mvc.xml"/> ``` 2. 配置Spring MVC的DispatcherServlet 在web.xml中配置DispatcherServlet,将Spring MVC的请求交给它来处理。 ```xml <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 3. 配置Spring MVC的视图解析器 在Spring配置文件中配置视图解析器,将Spring MVC的视图解析器注册到Spring容器中。 ```xml <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> ``` 4. 配置Spring MVC的控制器 在Spring MVC配置文件中定义控制器,配置请求映射和处理方法。 ```xml <bean class="com.example.controller.HelloController"> <property name="message" value="Hello, Spring MVC!"/> </bean> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean id="/hello" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="hello"/> </bean> ``` 5. 在Spring配置文件中配置组件扫描 ```xml <context:component-scan base-package="com.example"/> ``` 通过以上步骤,就可以将SpringSpring MVC整合起来,实现一个完整的Web应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值