Spring MVC屬於SpringFrameWork的後續產品,已經融合在Spring Web Flow裏面。Spring 框架提供了構建 Web 應用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構,從而在使用Spring進行WEB開發時,可以選擇使用Spring的SpringMVC框架或集成其他MVC開發框架,如hibernate,MyBatis等。
註解:
掃描指定的包中的類上的註解,常用的註解有:
@Controller 聲明Action組件 @RequestMapping (“/user”) @RequestMapping (“/login”)
@Service 聲明Service組件 @Service(“myMovieLister”)
@Repository 聲明Dao組件
@Component 泛指組件, 當不好歸類時.
@RequestMapping(“/menu”) 請求映射
@Resource 用於注入,( j2ee提供的 ) 默認按名稱裝配,@Resource(name=”beanName”)
@Autowired 用於注入,(srping提供的) 默認按類型裝配
@Transactional( rollbackFor={Exception.class}) 事務管理
@ResponseBody 返回數據以json數據形式返回
@Scope(“prototype”) 設定bean的作用域
1.配置web.xml , 使用Spring MVC,配置DispatcherServlet
<servlet>
<servlet-name> springmvc</servlet-name >
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class >
<init-param>
<param-name> contextConfigLocation</param-name >
<param-value> classpath:dispatcher-servlet.xml</param-value > //SpringMvc的分發配置文件
</init-param>
</servlet >
<servlet-mapping >
<servlet-name> springmvc</servlet-name >
<url-pattern> /</ url-pattern>//攔截/user/login
</servlet-mapping >
<context-param >
<param-name> contextConfigLocation</param-name >
<param-value> classpath:application-context.xml</param-value >//數據庫初始化,事務初始化,model層掃描
</context-param >
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class >
</listener >
2.dispatcher-servlet.xml或者springMVC.xml的配置
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven >
<mvc:argument-resolvers>
<bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
<bean
class= "org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven >
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- spring mvc對靜態資源的訪問 -->
<mvc:resources location= "/image/" mapping="/image/**" />
<mvc:resources location= "/js/" mapping ="/js/**"/>
<mvc:resources location= "/css/" mapping="/css/**" />
<mvc:resources location="/static/" mapping="/static/**"/>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<bean class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver" >
<constructor-arg>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value="/WEB-INF/views/" />
<property name= "suffix" value =".jsp" />
</bean>
</constructor-arg>
<property name= "enableFallback" value ="true" />
<property name= "mobilePrefix" value ="mobile/" />
<property name= "tabletPrefix" value ="tablet/" />
</bean >
<!-- json -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list >
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property >
</bean >
3.數據庫鏈接,model層掃描,事務配置
<?xml version= "1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation= "
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自動掃描web包 ,將帶有註解的類 納入spring容器管理 -->
<context:component-scan base-package="com.first" ></context:component-scan>
<!-- 引入jdbc配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "locations">
<list>
<value> classpath:jdbc.properties</value >
</list>
</property>
</bean >
<!-- data source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name= "driverClassName" value="${jdbc.driver}" > </property>
<property name= "url" value="${jdbc.url}" ></property>
<property name= "username" value="${jdbc.user}" ></property>
<property name= "password" value="${jdbc.pass}" ></property>
<!--initialSize: 初始化連接-->
<property name= "initialSize" value="5" />
<!--maxIdle: 最大空閒連接-->
<property name= "maxIdle" value ="10"/>
<!--minIdle: 最小空閒連接-->
<property name= "minIdle" value ="5"/>
<!--maxActive: 最大連接數量-->
<property name= "maxActive" value="15" />
<!--removeAbandoned: 是否自動回收超時連接-->
<property name= "removeAbandoned" value="true" />
<!--removeAbandonedTimeout: 超時時間(以秒數爲單位)-->
<property name="removeAbandonedTimeout" value="180"/>
<!--maxWait: 超時等待時間以毫秒爲單位 6000毫秒/1000等於60秒-->
<property name= "maxWait" value="3000" />
<property name= "validationQuery">
<value> SELECT 1</ value>
</property>
<property name= "testOnBorrow">
<value> true</ value>
</property>
</bean>
<bean id = "jdbcTemplate"
class = "org.springframework.jdbc.core.JdbcTemplate" >
<property name = "dataSource" ref="dataSource"/>
</bean >
<!-- 配置sessionFactory,model層掃描 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name= "dataSource" ref ="dataSource" />
<property name= "packagesToScan" value="com.first.model" />
<property name= "hibernateProperties">
<props>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect </prop>
<prop key= "hibernate.show_sql">true</prop >
<prop key="hibernate.hbm2ddl.auto" >update </prop>
<prop key= "hibernate.format_sql">true</prop >
</props>
</property>
</bean >
<!-- 配置事務管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "sessionFactory">
<ref bean= "sessionFactory" />
</property>
</bean >
<!-- 配置事務的傳播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name= "save*" propagation ="REQUIRED" />
<tx:method name= "add*" propagation ="REQUIRED" />
<tx:method name= "delete*" propagation ="REQUIRED" />
<tx:method name= "update*" propagation="REQUIRED" />
<tx:method name= "*" read-only ="true" />
</tx:attributes>
</tx:advice >
<!-- 那些類的哪些方法參與事務-->
<aop:config >
<aop:pointcut id= "allManagerMethod"
expression="bean(*Service)" />
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config >
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
4.jdbc.properties配置
jdbc.url=jdbc\:mysql \://localhost\:3306/test?useUnicode\=true&characterEncoding\=UTF-8
jdbc.user=root
jdbc.pass=
jdbc.driver=com.mysql.jdbc.Driver
更多互聯網與金融技術精華:http://www.moxianbin.com/