springMVC配置

一、每一個jar包得用處以下:html

org.springframework.aop--------------------Spring的面向切面編程,提供AOP(面向切面編程)實現java

org.springframework.asm--------------------Spring獨立的asm程序,相遇Spring2.5.6的時候須要asmJar 包.3.0開始提供他本身獨立的asmJarweb

org.springframework.aspects----------------Spring提供對AspectJ框架的整合spring

org.springframework.beans------------------SpringIoC(依賴注入)的基礎實現數據庫

org.springframework.context.support--------Spring-context的擴展支持,用於MVC方面express

org.springframework.context----------------Spring提供在基礎IoC功能上的擴展服務,此外還提供許多企業級服務的支持,如郵件服務、任務調度、JNDI定位、EJB集成、遠程訪問、緩存以及各類視圖層框架的封裝等編程

org.springframework.core-------------------Spring3.0的核心工具包json

org.springframework.expression-------------Spring表達式語言spring-mvc

org.springframework.jdbc-------------------對JDBC的簡單封裝緩存

org.springframework.orm--------------------整合第三方的ORM框架,如hibernate,ibatis,jdo,以及spring的JPA實現

org.springframework.test--------------------對Junit等測試框架的簡單封裝

org.springframework.web--------------------SpringWeb下的工具包

web-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:mvc="http://www.springframework.org/schema/mvc"
    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">

    <!-- 啓動掃描jlc下全部的controller --> 
    <context:component-scan base-package="com.jlc"/>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

    <!-- 避免IE執行AJAX時,返回JSON出現下載文件 -->
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <!-- 啓動Spring MVC 的註解功能,完成請求和註解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />  <!-- json轉換器 -->
            </list>
        </property>
    </bean>

     <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
            <props>  
                <prop key="org.springframework.web.servlet.PageNotFound">error/pageNotFound</prop>  
                <prop key="org.springframework.dao.DataAccessException">error/dataAccessFailure</prop>  
                <prop key="org.springframework.transaction.TransactionException">error/dataAccessFailure</prop>  
            </props>  
        </property>  
    </bean>  

     <!-- jsp頁面解析器,當Controller返回XXX字符串時,先經過攔截器,而後該類就會在/WEB-INF/views/目錄下,查找XXX.jsp文件-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:*-context.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <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>
    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:web-context.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-context爲空,後面會加入數據庫

<?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">


</beans>

部分註解說明

@Autowired  自動注入[根據類型注入 能夠對成員變量、方法以及構造函數進行註釋
@Qualifier  標註對象是成員變量、方法入參、構造函數入參。
@Resource   自動注入[根據名稱注入],可寫參數name=""  
@Controller 表示控制器  
@Service    表示業務處理層[通常在serviceImpl]  
@Repository 表示持久層[通常在daoImpl]  
@Component  當你的類不清楚是哪一層的時候使用該註解  
@ResponseBody  異步返回數據類型爲json  
@RequestMapping  路徑,請求類型等設置  
@InitBinder   數據綁定
相關文章
相關標籤/搜索