如今主流的Web MVC框架除了Struts這個主力 外,其次就是Spring MVC了。SpringMVC是一個基於DispatcherServlet的MVC框架,每個請求最早訪問的都是DispatcherServlet,DispatcherServlet負責轉發每個Request請求給相應的Handler,Handler處理之後再返回相應的視圖(View)和模型(Model),返回的視圖和模型均可以不指定,便可以只返回Model或只返回View或都不返回。web
DispatcherServlet是繼承自HttpServlet的,既然SpringMVC是基於DispatcherServlet的,那麼咱們先來配置一下DispatcherServlet,好讓它可以管理咱們但願它管理的內容。HttpServlet是在web.xml文件中聲明的。spring
<!-- Spring MVC配置 --><!-- ====================================== --><servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 能夠自定義servlet.xml配置文件的位置和名稱,默認爲WEB-INF目錄下,名稱爲[<servlet-name>]-servlet.xml,如spring-servlet.xml
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value> 默認
</init-param>
-->
<load-on-startup>1</load-on-startup></servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern></servlet-mapping>
<!-- Spring配置 --><!-- ====================================== --><listener>
<listenerclass>
org.springframework.web.context.ContextLoaderListener
</listener-class></listener>
<!-- 指定Spring Bean的配置文件所在目錄。默認配置在WEB-INF目錄下 --><context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value></context-param>
spring-servlet.xml配置session
spring-servlet這個名字是由於上面web.xml中<servlet-name>標籤配的值爲spring(<servlet-name>spring</servlet-name>),再加上「-servlet」後綴而造成的spring-servlet.xml文件名,若是改成springMVC,對應的文件名則爲springMVC-servlet.xml。mvc
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>">
<!-- 啓用spring mvc 註解 -->
<context:annotation-config />
<!-- 設置使用註解的類所在的jar包 -->
<context:component-scan base-package="controller"></context:component-scan>
<!-- 完成請求和註解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 對轉向頁面的路徑解析。prefix:前綴, suffix:後綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" /></beans>
applicationContext.xml配置
?1234567891011121314151617181920212223242526272829303132333435 <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 採用hibernate.cfg.xml方式配置數據源 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:config/hibernate.cfg.xml</value>
</property>
</bean>
<!-- 將事務與Hibernate關聯 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 事務(註解 )-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 測試Service -->
<bean id="loginService" class="service.LoginService"></bean>
<!-- 測試Dao -->
<bean id="hibernateDao" class="dao.HibernateDao">
<property name="sessionFactory" ref="sessionFactory"></property> </bean>
</beans>