spring-mvc.xml的配置文件模板
时间: 2024-05-04 15:21:16 浏览: 12
以下是一个基本的spring-mvc.xml配置文件模板:
```xml
<?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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置Spring MVC 的 DispatcherServlet -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!-- 配置静态资源的路径 -->
<mvc:resources mapping="/static/**" location="/static/"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置扫描Controller包 -->
<context:component-scan base-package="com.example.controller"/>
</beans>
```
其中:
- `<mvc:annotation-driven/>` 表示启用Spring MVC的注解驱动,可以使用 `@Controller`、`@RequestMapping`等注解。
- `<mvc:default-servlet-handler/>` 表示将静态资源的请求交给Servlet容器处理。
- `<mvc:resources>` 用于配置静态资源的路径,mapping属性表示访问路径,location属性表示资源文件所在路径。
- `<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">` 表示配置JSP视图解析器,prefix属性表示JSP文件所在路径的前缀,suffix属性表示JSP文件的后缀。
- `<context:component-scan base-package="com.example.controller"/>` 表示扫描指定的包,查找带有注解的类,例如 `@Controller`、`@Service`等。