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