SpringMVC部署步驟

1.導包(可以是用Maven)

2.配置Web.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>SCUT_M</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
     <!-- 配置監聽器 -->  
    <listener>  
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
     <!-- 在項目啓動時,加載spring配置文件 -->  
   <context-param>  
       <param-name>contextConfigLocation</param-name>  
       <param-value>
            classpath:application-context.xml
       </param-value>  
   </context-param>  
    <!-- 聲明servlet -->
    <servlet>
        <servlet-name>SCUT_M</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
     <!-- 配置servlet映射 -->
    <servlet-mapping>
        <servlet-name>SCUT_M</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

3.配置spring的配置文件
分別是
1)application-context.xml

<?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:context="http://www.springframework.org/schema/context"
       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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
       default-lazy-init="false">
    <!-- 元數據掃描 -->  
     <context:component-scan base-package="com.allimu.*"></context:component-scan>
     <context:annotation-config/>

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
            </list>
        </property>
    </bean>
    <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
          destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- 連接的空閒存活時間,當連接空閒時間大於該閥值時,清除該連接 -->
        <property name="idleMaxAge" value="240"/>
        <!-- 每個分區含有的最大連接數 -->
        <property name="maxConnectionsPerPartition" value="100"/>
        <!-- 每個分區含有的最小連接數 -->
        <property name="minConnectionsPerPartition" value="30"/>
        <!-- 分區數量 -->
        <property name="partitionCount" value="3"/>
        <!-- 每次新增連接的數量 -->
        <property name="acquireIncrement" value="5"/>
        <!-- 語句緩存個數 -->
        <property name="statementsCacheSize" value="50"/>
        <!-- 連接池助手線程數量,可設置爲0,該參數會降低運行速度,但程序有大量連接時,有助於提升高併發程序的性能 -->
        <property name="releaseHelperThreads" value="3"/>
        <!-- 語句助手線程數,可設置爲0,該參數會降低運行速度,但程序有大量的查詢語句時,有助於提升高併發程序的性能 -->
        <property name="statementReleaseHelperThreads" value="3"/>
        <!-- 測試連接有效性的間隔時間,單位分鐘 -->
        <property name="idleConnectionTestPeriod" value="60"/>
        <!-- 連接測試語句 -->
        <property name="connectionTestStatement" value="select 1 from dual"/>
        <!-- 連接超時時間,單位毫秒 -->
        <property name="connectionTimeout" value="10000"/>
    </bean>


    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>


            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
                <!-- hibernate.dialect=org.hibernate.dialect.OracleDialect -->
                hibernate.show_sql=false
                hibernate.query.substitutions=true 1, false 0
                hibernate.jdbc.fetch_size=100
                hibernate.jdbc.batch_size=200
                hibernate.cache.use_query_cache=false
            </value>
        </property>

    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>


    <!-- 支持 @AspectJ -->
    <aop:aspectj-autoproxy/>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionInterceptor"
          class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <!-- 配置事務屬性 -->
        <property name="transactionAttributes">
            <props>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="add*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <bean id="namingStrategy" class="org.hibernate.cfg.ImprovedNamingStrategy"/>

    <!-- spring hibernate工具類模板 -->
    <bean id="hibernateTemplate"
          class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

</beans>
    2)servlet-context.xml
        需要配置FreeMarker的prefix屬性(html路徑)
<?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: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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    default-lazy-init="false">


    <context:component-scan base-package="com.allimu.web,com.allimu.webapps,com.allimu.webservice"></context:component-scan>
    <!--<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/"/> <property name="suffix" 
        value=".jsp"/> </bean> --><!-- freemarker的配置 -->

    <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">0</prop>
                <prop key="defaultEncoding">UTF-8</prop>
                <prop key="url_escaping_charset">UTF-8</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="boolean_format">true,false</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="time_format">HH:mm:ss</prop>
                <prop key="number_format">0.######</prop>
                <prop key="whitespace_stripping">true</prop>
                <!-- <prop key="classic_compatible">true</prop> -->
            </props>
        </property>
    </bean>
    <!-- FreeMarker視圖解析 如返回userinfo。。在這裏配置後綴名ftl和視圖解析器。。 -->
    <bean id="freemarkerViewResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
        <property name="cache" value="true" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".html" />
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="requestContextAttribute" value="request" />
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="exposeSpringMacroHelpers" value="true" />
    </bean>


    <bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize">  
            <value>104857600</value>  
        </property>  
        <property name="maxInMemorySize">  
            <value>4096</value>  
        </property>
    </bean>
    <bean id="viewNameTranslator"
        class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator">
        <property name="stripExtension" value="false" />
    </bean>
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="cookieName" value="clientlanguage" />
        <property name="cookieMaxAge" value="-1" />
    </bean>
    <bean class="com.allimu.exception.ImuExceptionResolver" />
    <context:annotation-config />


</beans>

4.配置dev/online配置文件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章