Springmvc与Spring整合

环境搭建

引入相应的jar包

springMVC和spring包的结构发生了很大的变化,各个包都分开了,灵活,要求使用者更加深入的学习使用,当我们引入包的时候,以少为原则,少的话可以根据报错来找到相应的包,如果过多的话,包会报错异常的混乱,不容易分辨;sprinMVC和spring本身就是一家的,所以引入的包来说基本上和spring需要的架构包是一致的.
这里写图片描述

全局配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>EmpSearchPrac</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>



      <!-- 注册ContextLoaderListener -->
      <!--配置文件路径设置-->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springIOC.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>



    <!-- 注册springmvc核心控制器 -->
  <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.xml</param-value>  
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

  <!-- 注册Spring核心编码过滤器 -->
  <filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

注解:springMVC是通过dispastservlet来监听的,spring使用linstener监听的,他们的启动顺序为:
第一:context-param
第二:Listerer
第三:Filter
第四:servlet

Springmvc配置文件springMVC.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"    
 xmlns:context="http://www.springframework.org/schema/context"    
 xmlns:p="http://www.springframework.org/schema/p"    
 xmlns:mvc="http://www.springframework.org/schema/mvc"    
 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-3.0.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-3.0.xsd">  
     <!-- 注解扫描包 -->  
    <context:component-scan base-package="xxxxxxxxxxxxxxxxxx" />  

    <!-- 开启注解 --> 
    <mvc:annotation-driven/>  


  <!-- 静态资源访问 -->  
     <mvc:resources location="/img/" mapping="/img/**"/>    
     <mvc:resources location="/js/" mapping="/js/**"/>     


     <!-- 基于注解的映射器(可选) -->
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>


      <!-- 基于注解的适配器(可选) -->
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

      <!-- 视图解析器(可选)(如果为逻辑地址则需要配置) -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     </bean>
    <bean id="multipartResolver"            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
          <property name="defaultEncoding" value="utf-8" />  
          <property name="maxUploadSize" value="10485760000" />  
          <property name="maxInMemorySize" value="40960" />  
    </bean>  
 </beans>

Spring配置文件springIOC.xml

可以参考spring与其他orm框架进行整合时的配置文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值