Spring MVC框架是一个基于请求驱动的Web框架,而且使用了前端控制器模式来进行设计,再根据请求映射规则分发给相应的页面控制器(动做/处理器)进行处理。
Spring MVC框架将数据、业务和视图进行分离,减小不一样模块以前的耦合,能够快速的开发Web项目。
注意事项:
1. 启动。Spring MVC先启动contextConfigLocation装载bean,最后启动servlet装载Controller。
2. 事务。在applicationContext.xml中不能扫描Controller,由于此时Service尚未装配事务,此时获得的Service是没有事务的,servlet.xml中只扫描Controller,设置use-default-filters为false或者使用exclude-filter排除Service。
3. Controller返回json。Controller返回json必需依赖jackson。
建立Spring MVC项目:
1. 建立webapp项目
建立webapp项目后会在webapp/WEB-INF下默认生成web.xml,web.xml为服务的启动配置文件。
2. 配置web.xmlcss
<web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>log4jConfig</param-name> <param-value>classpath:/log4j.properties</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
3. 配置applicationContext.xml前端
<context:component-scan base-package="com.aaron"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--rdx.xml中配置事务--> <import resource="classpath:/spring/rds.xml"/> <import resource="classpath:/spring/redis.xml"/>
4. 配置SpringMVC-servlet.xmljava
<context:component-scan base-package="com.aaron"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <mvc:annotation-driven/> <mvc:resources mapping="/image/**" location="/static/image/" /> <mvc:resources mapping="/css/**" location="/static/css/" /> <mvc:resources mapping="/js/**" location="/static/js/" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean>
5. rds.xml配置事务web
<!-- database transaction manager --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <tx:annotation-driven transaction-manager="transactionManager" />
6. 建立Controller和Serviceredis
@Controller public class HelloController { } @Service @Transactional(value= “transactionManager”) public class HelloService { }
7. 配置pom.xml选择Jetty或者Tomcat pluginspring
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jetty.version}</version> <configuration> <httpConnector> <port>8080</port> </httpConnector> <webAppConfig> <contextPath>${server.path}</contextPath> </webAppConfig> <scanIntervalSeconds>5</scanIntervalSeconds> </configuration> </plugin> <!--<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>${tomcat.version}</version> <configuration> <port>8080</port> <path>${server.path}</path> <uriEncoding>UTF-8</uriEncoding> <server>tomcat7</server> </configuration> </plugin>-->
8. 运行和打包
运行:
使用Jetty:run 或者 Tomcat7:run
打包:
jar包:
pom.xml:
<packaging>jar</packaging>
war包:
pom.xml:
<packaging>war</packaging>express