天天看點

springmvc注解開發

關于SpringMvc+spring+hibernate 注解開發的配置檔案

web.xml中的配置

<!--放置順序 (有時候會導緻有點小錯誤但不影響運作,我這人有強迫症是以都是按順序放置的)
(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,
     listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,
     error-page*,taglib*,resource-env-ref*, resource-ref*,security-constraint*,login-config?,
     security-role*, env-entry*,ejb-ref*,ejb-local-ref*)-->
<web-app>

<display-name>Archetype Created Web Application</displayname>

    <!-- Spring config檔案 注冊多個配置檔案 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring.xml,
            classpath:spring-hibernate.xml
        </param-value>
    </context-param>

    <!--字元編碼設定-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 使用監聽器引入spring架構 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- spring mvc config檔案 -->
    <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:spring-mvc.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>

    <!--指明下面檔案不需要springMVC過濾-->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.ico</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>logion.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>500</error-code>
        <location>/view/error.jsp</location>
    </error-page>

    <error-page>
        <error-code>404</error-code>
        <location>/view/error.jsp</location>
    </error-page>

</web-app>
           

springMvc配置檔案中的配置

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- spring mvc 自動掃描 -->
    <context:component-scan base-package="com.men" />
    <!--aop注解配置-->
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <!--視圖解析器-->
    <bean id="viewResoler" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <!--視圖解析器辨別。如果有多個的話表示此解析器-->
        <!--<property name="viewNames" value=""/>-->
        <property name="prefix" value="/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--json轉換配置-->
    <mvc:annotation-driven>
         <mvc:message-converters register-defaults="true">
          <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
          <property name="supportedMediaTypes" value="application/json"/></bean>
         </mvc:message-converters>
    </mvc:annotation-driven>
    <!--自定義攔截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**/"/>
            <bean class="com.men.fillter.MyFillter"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>
           

spring-hibernate.xml

<!--這裡面配置了與持久層dao相關的-->
<?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:cache="http://www.springframework.org/schema/cache"
       xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd">
    <!--讀取配置檔案-->
    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:config.properties</value>
        </property>
    </bean>
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="root" />
        <!--驅動會根據url自動識别-->
        <property name="password" value="123456" />
        <property name="initialSize" value="0" />
        <property name="maxActive" value="20" />
        <property name="minIdle" value="0" />
        <property name="maxWait" value="60000" />
    </bean>
    <!-- hibernate session -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <!--緩存配置-->
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            </props>
        </property>
        <!-- autoscan annotation for hiberante config -->
        <property name="packagesToScan">
            <list>
                <value>com.men.model</value>
            </list>
        </property>
    </bean>
    <!--開啟注解緩存-->
    <cache:annotation-driven/>
    <!--緩存的bean-->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager"><ref local="ehcache"/></property>
    </bean>
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"/>
</beans>
           

spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--驗證碼配置-->
    <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha" >
        <property name="config">
            <bean class="com.google.code.kaptcha.util.Config">
                <constructor-arg>
                    <props>
                        <prop key="kaptcha.border">yes</prop>
                        <prop key="kaptcha.border.color">105,179,90</prop>
                        <prop key="kaptcha.textproducer.font.color">blue</prop>
                        <prop key="kaptcha.image.width">100</prop>
                        <prop key="kaptcha.image.height">40</prop>
                        <prop key="kaptcha.textproducer.font.size">30</prop>
                        <prop key="kaptcha.session.key">code</prop>
                        <prop key="kaptcha.textproducer.char.length">4</prop>
                        <prop key="kaptcha.textproducer.font.names">宋體,楷體</prop>
                    </props>
                </constructor-arg>
            </bean>
        </property>
    </bean>
</beans>
           

ehcache.xml hibernate二級緩存需要的配置檔案

<?xml version="1.0" encoding="UTF-8"?>

<ehcache>
    <diskStore path="c:\\ehcache\"/>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            />
    <!-- 設定Category類的緩存的資料過期政策 -->
    <cache name="org.qiujy.domain.cachedemo.Category"
           maxElementsInMemory="100"
           eternal="true"
           timeToIdleSeconds="0"
           timeToLiveSeconds="0"
           overflowToDisk="false"
            />
    <!-- 設定Category類的products集合的緩存的資料過期政策 -->
    <cache name="org.qiujy.domain.cachedemo.Category.products"
           maxElementsInMemory="500"
           eternal="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           overflowToDisk="true"
            />
    <cache name="org.qiujy.domain.cachedemo.Product"
           maxElementsInMemory="500"
           eternal="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           overflowToDisk="true"
            />
    <cache name="workUnitHisCache"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="2400"
           timeToLiveSeconds="2400"
           overflowToDisk="false"/>

</ehcache>
           

config.properties

hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://:/schoolmanagementsystem
jdbc_username=root
jdbc_password=
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false
           

繼續閱讀