Spring MVC(二)

Spring MVC配置

约束

beans约束:spring必须
context约束:注解和扫描
spring-mvc约束:静态资源、允许跨域以及拦截器

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

扫包

必须配置context约束

<context:component-scan base-package="com.mysite.web" />

前端控制映射器

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <!--前缀-->
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <!--后缀-->
    <property name="suffix" value=".jsp"/>
</bean>

静态资源放行

如果要使用mvc配置,需要引入mvc约束,
如果配置静态资源过滤放行,需要加一句

<mvc:annotation-driven />

过滤:

<mvc:resources mapping="/editor/**" location="/editor/"/>
<mvc:resources mapping="/bootstrap/**" location="/bootstrap/"/>
<mvc:resources mapping="/imgs/**" location="/imgs/"/>

跨域

<mvc:cors>
    <mvc:mapping path="/**" allowed-origins="*" allowed-methods="*" allowed-headers="*"/>
</mvc:cors>

拦截器

<!--配置拦截器-->
<mvc:interceptors>
    <mvc:interceptor>
        <!--配置拦截器作用的路径-->
        <mvc:mapping path="/**"/>
        <bean class="com.mysite.interceptor.LoginInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>
posted @ 2023-04-19 08:58  挖洞404  阅读(6)  评论(0编辑  收藏  举报