1. 程式人生 > >springMVC框架簡單搭建

springMVC框架簡單搭建

springmvc類似於struts。是開源專案springframework下的一個框架。

1.jar包

依賴於spring所以需要匯入spring的jar包。

我使用maven管理jar包

<!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

2.目錄結構



3.基本配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    
   <!--載入全域性變數,resources目錄配置的bean可以在這裡訪問到-->
     <!--<context-param>
        <param-name>parentContextKey</param-name>
        <param-value>guiceioc</param-value>
    </context-param> -->
   
    <!--ContextLoaderListener監聽器的作用就是啟動Web容器時,自動裝配ApplicationContext的配置資訊
--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
  <!--編碼過濾器-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 開啟非同步支援
        <async-supported>true</async-supported>-->
        <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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--GZIP壓縮返回資料-->
    <filter>
        <filter-name>GzipFilter</filter-name>
        <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
        <init-param>
            <param-name>methods</param-name>
            <param-value>GET,POST</param-value>
        </init-param>
        <init-param>
            <param-name>mimeTypes</param-name>
            <param-value>
                text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,application/json,image/svg+xml
            </param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>GzipFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
    <!--流程排程,springmvc核心-->
    <servlet>
        <servlet-name>ads</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <!-- 開啟非同步支援
        <async-supported>true</async-supported>-->
    </servlet>

    <servlet-mapping>
        <servlet-name>ads</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
        <url-pattern>*.css</url-pattern>
        <url-pattern>*.png</url-pattern>
        <url-pattern>*.jpg</url-pattern>
        <url-pattern>*.gif</url-pattern>
        <url-pattern>*.ico</url-pattern>
        <url-pattern>*.swf</url-pattern>
    </servlet-mapping>

    <error-page>
        <error-code>404</error-code>
        <location>/err404.jsp</location>
    </error-page>
    <error-page>
        <error-code>405</error-code>
        <location>/err405.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/err500.jsp</location>
    </error-page>
    <error-page>
        <error-code>503</error-code>
        <location>/err503.jsp</location>
    </error-page>

    <welcome-file-list>
        <welcome-file>/</welcome-file>
    </welcome-file-list>
</web-app>