Spring MVC(二)
阿新 • • 發佈:2020-10-12
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>