web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" version="3.0"
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:config/properties/log4j.properties</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!-- context-param中配置的contextConfigLocation只对ContextLoaderListener有效 不会对DispatcherServlet有效 -->
- <!-- DispatcherServlet用来加载mvc相关的bean ContextLoaderListener用来加载其他bean -->
- <!-- ContextLoaderListener会生成一个spring的context 注:DispatcherServlet不会识别@controller注解-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/config/spring-bean/*.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置DispatcherServlet load-on-startup要為1 它會根据contextConfigLocation生成一個spring的context-->
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/config/spring-servlet.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <filter>
- <filter-name>encodingFilter</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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
spring-servlet.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:tx="http://www.springframework.org/schema/tx" 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/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd">
- <!-- 配置扫描的包 这里配置的包和它的下级包都会被扫描 这样不仅会扫描@controller @service @repository 还会扫描@component-->
- <context:component-scan base-package="com.zjf" />
- <!-- 注册HandlerMapper、HandlerAdapter两个映射类 这是spring为@Controller分发请求所必须的 同时也是表单数据转换为对象(如xml json等)必须的-->
- <mvc:annotation-driven />
- <!-- 访问静态资源 如果没有这个配置 那么静态的js css文件将会报错 因为在web.xml中我们配置了<url-pattern>/</url-pattern> -->
- <mvc:default-servlet-handler />
- <!-- 视图解析器 这里使用InternalResourceViewResolver -->
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/view/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
- </beans>
时间: 2024-11-05 01:06:31