Maven+SpringMVC+SpringJDBC搭建web项目FAQ

Maven+SpringMVC+SpringJDBC搭建web项目FAQ
3.1Web项目基本结构
按照 Java EE 规范的规定,一个典型的Web 应用程序有四个部分:
1. 公开目录
2. WEB-INF/web.xml 文件,发布描述符(必选)
3. WEB-INF/classes 目录,编译后的Java 类文件(可选)
4. WEB-INF/lib 目录,Java 类库文件(*.jar)(可选)
经典的结构:
Maven+SpringMVC+SpringJDBC搭建web项目FAQ_第1张图片

备注:应用服务器把WEB-INF指为禁访目录,即直接在浏览器里是不能直接访问的;可通过servlet访问。
3.2web项目启动顺序
1、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取和两个结点。

2、紧急着,容器创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。

3、容器将转换为键值对,并交给servletContext。

4、容器创建中的类实例,创建监听器。

web.xml 的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet
而同个类型之间的实际程序调用的时候的顺序是根据对应mapping的顺序进行调用的。

3.3Eclipse创建Maven web项目注意事项
详见文档:eclipse创建MavenWebProject.doc
3.4maven+springMVC+springJDBC配置
关键点:约定大于配置
关键配置:web.xml application.xml spring-servlet.xml pom.xml
3.4.1web.xml配置 (web容器的配置文件)
1.配置Context-param


<context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>

备注:部署到tomcat后,eclipse中的src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下,当然这个deploy path是可以配置的,默认为classes目录 。
classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。
2.配置listener

  
  <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  

3.配置filter

  
  <filter>
    <filter-name>setcharacterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>setcharacterfilter-name>
    <url-pattern>/url-pattern>
  filter-mapping>
  

4.配置Spring MVC Servlet

 
  <servlet>
    <servlet-name>springservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:spring-servlet.xmlparam-value>
    init-param>
    
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping> 
  

3.4.2 applicationContext.xml配置 (spring的核心配置文件)
使用Spring IOC ,相应的bean一般配在此文件中
例如JDBC相关的bean。


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

    
    <context:property-placeholder location="classpath:jdbc.properties" />

    
    <context:component-scan base-package="com.service" />
    <context:component-scan base-package="com.dao" />

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass">
            <value>${jdbc.driverClassName}value>
        property>
        <property name="jdbcUrl">
            <value>${jdbc.url}value>
        property>
        <property name="user">
            <value>${jdbc.username}value>
        property>
        <property name="password">
            <value>${jdbc.password}value>
        property>
        
        <property name="minPoolSize">
            <value>5value>
        property>
        
        <property name="maxPoolSize">
            <value>30value>
        property>
        
        <property name="initialPoolSize">
            <value>10value>
        property>
        
        <property name="maxIdleTime">
            <value>60value>
        property>
        
        <property name="acquireIncrement">
            <value>5value>
        property>
        
        <property name="maxStatements">
            <value>0value>
        property>
        
        <property name="idleConnectionTestPeriod">
            <value>60value>
        property>
        
        <property name="acquireRetryAttempts">
            <value>30value>
        property>
        
        <property name="breakAfterAcquireFailure">
            <value>truevalue>
        property>
        
        <property name="testConnectionOnCheckout">
            <value>falsevalue>
        property>
    bean>

    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource">property>
    bean>

    
    <bean id="nativeJdbcExtractor"
        class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
        lazy-init="true" />

    
    <bean id="lobHandler"
        class="org.springframework.jdbc.support.lob.DefaultLobHandler"
        lazy-init="true" />

    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource" />

    
   

beans>

3.4.3 spring-servlet.xml 配置 (Spring MVC配置文件)

 
    <mvc:resources mapping="/resources/**" location="/resources/" />

     
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    bean>

      
    <bean id="jsonConverter"  
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">bean>  

      
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

      
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="webBindingInitializer">  
              
            <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">  
                <property name="validator" ref="validator" />  
            bean>  
        property>  

        <property name="messageConverters">  
            <list>  
                <ref bean="jsonConverter" />  
            list>  
        property>  
    bean> 

    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" p:maxUploadSize="5000000"/>

         
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">     
         <property name="exceptionMappings">     
             <props>     
                 <prop key="com.exception.UserException">errors/errorprop> 
                 <prop key="java.lang.Exception">errors/errorprop>     
                 <prop key="java.lang.Throwable">errors/errprop>     
             props>     
         property>     
         <property name="statusCodes">     
             <props>     
                 <prop key="errors/error">500prop>     
                 <prop key="errors/404">404prop>     
             props>     
         property>     
         
         <property name="warnLogCategory" value="WARN">property>     
              
         <property name="defaultErrorView" value="errors/error">property>     
           
         <property name="defaultStatusCode" value="500">property>   
     bean>      
        

3.4.4pom.xml 配置 (maven )

<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.0modelVersion>
  <groupId>com.typ.springgroupId>
  <artifactId>maven-springmvc-springjdbcartifactId>
  <packaging>warpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>maven-springmvc-springjdbc Maven Webappname>
  <url>http://maven.apache.orgurl>

  <properties>
       <spring.version>4.1.4.RELEASEspring.version>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>${spring.version}version>
        <scope>testscope>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>${spring.version}version>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-coreartifactId>
        <version>${spring.version}version>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>${spring.version}version>
    dependency>

    <dependency>
        <groupId>org.aspectjgroupId>
        <artifactId>aspectjweaverartifactId>
        <version>1.8.5version>
    dependency>

     <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.35version>
    dependency>

    <dependency>
        <groupId>com.mchangegroupId>
        <artifactId>c3p0artifactId>
        <version>0.9.5version>
    dependency>

    <dependency>
        <groupId>jstlgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
    dependency>

   <dependency>
    <groupId>org.hibernategroupId>
    <artifactId>hibernate-validatorartifactId>
    <version>5.1.2.Finalversion>
   dependency>

    <dependency>
        <groupId>commons-fileuploadgroupId>
        <artifactId>commons-fileuploadartifactId>
        <version>1.3.1version>
    dependency>


    
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-databindartifactId>
        <version>2.5.2version>
    dependency>
    <dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-core-aslartifactId>
      <version>1.9.13version>
    dependency>

    <dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-mapper-aslartifactId>
      <version>1.9.13version>
    dependency>
    

  dependencies>
  <build>
    <finalName>maven-springmvc-springjdbcfinalName>

    <plugins>
        <plugin>
          <groupId>org.apache.maven.pluginsgroupId>
          <artifactId>maven-compiler-pluginartifactId>
          <version>2.3.2version>
          <configuration>
          <source>1.7source>
          <target>1.7target>
          configuration>
      plugin>
    plugins>
  build>
project>

当然还有更详细的配置,请自行探索

下一章会把SpringMVC+SpringJDBC搭建的项目Demo传上来= }=

你可能感兴趣的:(JavaWeb,tomcat)