SpringMVC 基本配置文件

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

    <!--配置SpringMVC-->

    <!--1.开启SpringMVC注解模式-->
    <!--简化配置
            1.自动注册DefaultAnnotationHandlerMappper,AnnotationMethodHandlerAdapter
            2.提供一系列:数据绑定 数字和日期的format  @NumberFormat,@DataTimeFormat,
                xml/json默认读写支持.
     -->
    <mvc:annotation-driven/>

    <!-- 2.静态资源默认servlet配置
        1.对静态资源的处理
        2.允许使用/做整体映射
     -->
    <mvc:default-servlet-handler/>

    <!-- 3.配置jsp显示VideoResolver -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="controller,intergrate.controller"/>

    <!--extention:文件上传-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="102400000"/>
    </bean>

    <!--extention:json数据-->
    <mvc:annotation-driven>
    <mvc:message-converters>
    <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>
    </mvc:annotation-driven>

    <!--extention:处理全局异常-->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
    <props>
    <prop key="java.lang.ArithmeticException">error</prop>
    </props>
    </property>
    </bean>

    <!--extention:自定义拦截器-->
    <mvc:interceptors>
    <bean class="interceptor.MyInterceptor"/>
    </mvc:interceptors>

    <!--extention:国际化-->
    <!-- configure the locale resource -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="locale"/>
    </bean>
    <!--可以直接访问WEB-INF下的jsp页面-->
    <mvc:view-controller path="/locale" view-name="locale"/>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基本Spring MVC配置文件示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置扫描的包 --> <context:component-scan base-package="com.example.controller"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 启用Spring MVC注解驱动 --> <mvc:annotation-driven/> <!-- 配置静态资源的处理 --> <mvc:resources mapping="/static/**" location="/static/"/> <!-- 配置拦截器 --> <mvc:interceptors> <bean class="com.example.interceptor.LoginInterceptor"/> </mvc:interceptors> </beans> ``` 其中, - `context:component-scan` 指定需要扫描的控制器所在的包。 - `InternalResourceViewResolver` 是Spring MVC的视图解析器,它将视图名称解析为JSP文件路径。 - `mvc:annotation-driven` 启用Spring MVC注解驱动,允许在控制器中使用注解,如 `@RequestMapping` 等。 - `mvc:resources` 配置静态资源的处理,比如CSS、JS、图片等静态文件。 - `mvc:interceptors` 配置拦截器,可以在请求到达控制器之前或之后执行一些操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值