SpringMvc执行原理:
DispatcherServlet 接受请求交给 HandlerMapping
HandlerMapping处理映射器 通过url找到对应的controller接口返回给DispatcherServlet
DispatcherServlet吧对应的接口交给HandlerAdapter处理适配器
HandlerAdapter执行contrller请求 通过model返回DispatcherServlet
springMVC
M model 模型(service dao)
V view 视图 (jsp html)
C controller 控制层
搭建ssm项目步骤:
pom.xml
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
web.xml
配置DispatchServlet
<!-- 配置MVC --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--绑定spring的配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!--启动级别--> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!--sessiom--> <session-config> <session-timeout>15</session-timeout> </session-config>
<!-- 配置SPRING --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
spring-mvc.xml
<!--1.注解驱动-->
<mvc:annotation-driven/>
<!-- 2.静态资源过滤-->
<mvc:default-servlet-handler/>
<!--3.扫描包 controller-->
<context:component-scan base-package="com.moto.controller"/>
<!-- 注册自定义拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/public/**"/>
<bean class="com.moto.util.interceptor.LoginInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.moto.util.interceptor.InterfaceInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!--4.试图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880" />
<property name="maxInMemorySize" value="4096" />
</bean>
applicationContext.xml
<context:annotation-config/> <context:component-scan base-package="com.moto" /> <context:component-scan base-package="ztweb.common.entitys"/> <context:component-scan base-package="ztweb.web.monitorcmd.bpo"/> <!-- 配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- 数据库\cache配置文件 --> <value>classpath:conn.properties</value> <value>classpath:wxmemcached.properties</value> <value>classpath:db_mysql.properties</value> </list> </property> <property name="fileEncoding" value="utf-8"/> </bean> <import resource="classpath:spring-dao.xml"/> <import resource="classpath:spring-service.xml"/> <import resource="classpath:spring-memcached.xml"/>