• Spring MVC 学习 之


    1、环境参数:

     Maven:3.1.1

     JDK  :1.6

    2、项目文件结构图:

    3、各文件配置:

    3.1、 pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>rhythmk_web</groupId>
      <artifactId>web1</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>web1 Maven Webapp</name>
      <url>http://maven.apache.org</url>
      
          <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
               <spring.version>3.2.4.RELEASE</spring.version>
               <log4j.version>1.2.14</log4j.version>
            </properties>
            
      
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
        <!-- spring   begin-->
            <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-jdbc</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>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</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-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>${spring.version}</version>
            </dependency>
        <!-- spring   end -->
        
                <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            
                <dependency>
                <groupId>c3p0</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.1</version>
            </dependency>
                <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.1</version>
            </dependency>
            <dependency>
                <groupId>org.jsoup</groupId>
                <artifactId>jsoup</artifactId>
                <version>1.7.3</version>
            </dependency>
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-core-lgpl</artifactId>
                <version>1.9.13</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-lgpl</artifactId>
                <version>1.9.13</version>
            </dependency>
            <dependency>
                <groupId>commons-validator</groupId>
                <artifactId>commons-validator</artifactId>
                <version>1.4.0</version>
            </dependency>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.8</version>
            </dependency>
            <dependency>
                <groupId>com.github.axet</groupId>
                <artifactId>kaptcha</artifactId>
                <version>0.0.8</version>
            </dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
                <version>2.2.3</version>
            </dependency>
            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
                <classifier>jdk15</classifier>
            </dependency>
                
                
                <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
      </dependencies>
      <build>
        <finalName>web1</finalName>
      </build>
    </project>

    3.2 、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">
        <display-name>web1</display-name>
    
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    
        <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
            <!--指定spring配置文件的位置 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- 所有以.html和.json的请求都由dispatcherServlet来处理的配置 -->
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>
    
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>*.json</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>encodingFilter</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>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <!-- 让web应用程序启动那个时自动添加属性文件 -->
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:log4j.properties</param-value>
        </context-param>
    
        <!-- 以Listener方式启动LOG4j -->
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
    
        <!-- 以Listener方式启动spring -->
        <listener>
            <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
        </listener>
    
    -     <filter>
            <filter-name>globalFilter</filter-name>
            <filter-class>com.rhythmk.web1.filter.GlobalFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>globalFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    
        <session-config>
            <session-timeout>20</session-timeout>
        </session-config>
    
        <welcome-file-list>
            <welcome-file>index.htm</welcome-file>
        </welcome-file-list>
    
        <error-page>
            <error-code>404</error-code>
            <location>/404.htm</location>
        </error-page>
    
        <error-page>
            <error-code>500</error-code>
            <location>/500.htm</location>
        </error-page>
        <!-- 前端控制器的配置 -->
    
    
    </web-app>

    3.3 、applicationContext.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:p="http://www.springframework.org/schema/p"
        xmlns:lang="http://www.springframework.org/schema/lang" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans.xsd
                 http://www.springframework.org/schema/lang
                 http://www.springframework.org/schema/lang/spring-lang.xsd      
                 http://www.springframework.org/schema/tx   
                 http://www.springframework.org/schema/tx/spring-tx.xsd    
                 http://www.springframework.org/schema/aop     
                 http://www.springframework.org/schema/aop/spring-aop.xsd    
                 http://www.springframework.org/schema/mvc     
                 http://www.springframework.org/schema/mvc/spring-mvc.xsd   
                 http://www.springframework.org/schema/context     
                 http://www.springframework.org/schema/context/spring-context.xsd
                 http://www.springframework.org/schema/cache
                 http://www.springframework.org/schema/cache/spring-cache.xsd
                 http://www.springframework.org/schema/task 
                 http://www.springframework.org/schema/task/spring-task.xsd">
    
        <!-- 自动扫描的包名 -->
        <context:component-scan base-package="com.rhythmk.web1"></context:component-scan>
        <!-- spring mvc 基于注解在方法上 控制映射 配置 -->
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        </bean>
        <bean
            class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    
    <mvc:annotation-driven>  
        <mvc:message-converters>  
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <property name="supportedMediaTypes">  
                    <list>  
                        <value>text/plain;charset=UTF-8</value>  
                        <value>text/html;charset=UTF-8</value>  
                    </list>  
                </property>  
            </bean>  
            <bean  
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
                <property name="supportedMediaTypes">  
                    <list>  
                        <value>application/json; charset=UTF-8</value>  
                        <value>application/x-www-form-urlencoded; charset=UTF-8</value>  
                    </list>  
                </property>  
            </bean>  
        </mvc:message-converters>  
    </mvc:annotation-driven>  
    
    
        <mvc:default-servlet-handler />
    
        <tx:annotation-driven />
    
        <task:annotation-driven />
    
    
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
    </beans>

    代码下载地址:

      http://pan.baidu.com/s/1gduzeiZ

        

  • 相关阅读:
    QA的几个经典问题(1)
    QA的几个经典问题(2)
    通过Android录音进行简单音频分析
    Android在TextView中实现RichText风格
    Handler Should be static or leaks Occur?
    如何查看华为EMUI系统APK源码?
    ServiceManager: Permmission failure: android.permission.RECORD_AUDIO
    Android 4.4以上的存储读写权限
    如何使用Android中hide的类和方法进行开发?
    在Mac mini上编译Android源码
  • 原文地址:https://www.cnblogs.com/rhythmK/p/3971131.html
Copyright © 2020-2023  润新知