springmvc配置总结

本文详细介绍了Spring MVC框架的基本配置方法,包括web.xml、springmvc-servlet.xml、application.xml和log4j.properties等核心配置文件的具体内容。通过这些配置实现了控制器扫描、视图解析、静态资源处理等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 配置web.xml
  2. 配置springmvc-servlet.xml
  3. 配置application.xml
  4. 配置log4j.properties

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app  version="3.1" 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_3_1.xsd">

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <!--<multipart-config><!–配置文件上传–>-->
      <!--<location>/tmp/uploads</location>-->
      <!--<max-file-size>2097152</max-file-size><!– KB–>-->
      <!--<max-request-size>4194304</max-request-size>-->
    <!--</multipart-config>-->
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern><!-- 如果配置为/*的话会找不到.jsp等静态资源-->
  </servlet-mapping>


  <!-- Spring配置非web的bean -->
  <!-- ====================================== -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>


  <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <!-- log4j配置,文件路径,因为是跟随项目启动 -->
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
  </context-param>

  <listener>
    <listener-class>
      org.springframework.web.util.Log4jConfigListener
    </listener-class>
  </listener>

  <!-- 配置Spring提供的字符编码过滤器 -->
  <filter>
    <filter-name>SpringCharacterEncodingFilter</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>SpringCharacterEncodingFilter</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>

</web-app>
        
springmvc-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       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">

    <!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <mvc:annotation-driven />

    <!--设置扫描的Controller的包,会递归扫描该包和子包-->
    <context:component-scan base-package="controller"/>

    <!-- 拦截器 -->
    <mvc:interceptors>
        <bean class="MyInteceptor" />
    </mvc:interceptors>

    <!-- 对静态资源文件的访问  方案一 (二选一) -->
    <mvc:default-servlet-handler/>

    <!-- 对静态资源文件的访问  方案二 (二选一)-->
    <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
    <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
    <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
    <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>

    <!-- Using a MultipartResolver with Servlet 3.0-->
    <bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>

    <!-- ========文件上传,非servlet3.0可用======= -->
    <!--200*1024*1024即200M resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="209715200" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="resolveLazily" value="true" />
    </bean>

<!--配置的视图解析器-->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

application.xml

<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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    <!-- 配置组件扫描器,使用注解方式开发,不用配置dao和service -->
    <!-- 在springmvc.xml文件中也可以配置这个属性 -->
    <context:component-scan base-package="com.edu.test"/>

    <!-- 数据源 -->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <!-- 配置数据库的session工厂,可根据hibernate和mybatis具体进行配置-->
    <bean id="sqlSessionFactory" class="org.">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>

    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置AOP通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 配置事务属性 -->
        <tx:attributes>
            <!-- 添加事务管理的方法 -->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="select*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置AOP,为添加事务管理的操作配置AOP -->
    <aop:config>
        <!-- 引入的Spring定义的事务通知,需要使用aop:advisor -->
        <!-- 下面难 -->
        <aop:advisor advice-ref="txAdvice"
                     pointcut="execution(* com.edu.test.service.*.*(..))"
        />
    </aop:config>
</beans>

log4j.properties:

log4j.rootCategory=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n

log4j.category.org.springframework.beans.factory=DEBUG


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值