SpringMVC项目的搭建

首先,eclipse中创建一个Dynamic Web Project

SpringMVC项目的搭建_第1张图片

Target runtime选择 Tomcat6
SpringMVC项目的搭建_第2张图片

一直next,最后勾选“generate web.xml deployment descriptor”
SpringMVC项目的搭建_第3张图片

导入相关的jar包(可以在我的百度网盘下载,链接:http://pan.baidu.com/s/1boT9vLP 密码:h439)
SpringMVC项目的搭建_第4张图片

规划包的目录结构
SpringMVC项目的搭建_第5张图片

修改web.xml文件:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:config/spring-context.xmlparam-value>
    context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <servlet>
        <servlet-name>springservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>springservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>
web-app>

这里配置了两个配置文件:
一个是上面的 classpath:config/spring-context.xml
这里spring-context.xml 作为系统级别的上下文,所以它的初始化需要放到 web.xml 中的标签中,同时其他的类似定时任务的配置文件等等都是放在这个标签下进行初始化的。
比如:

 <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
        classpath:spring/spring-context.xml,  
        classpath:spring/spring-quartz.xml  
        </param-value>  
    </context-param>  

或者定义某一个文件夹下符合某种规则的配置文件:

    param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:conf/spring/spring-*.xmlparam-value>
    param>

spring-context.xml具体内容:


    <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"
           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.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

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


    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"
          p:driverClassName="com.mysql.jdbc.Driver"
          p:url="jdbc:mysql://localhost:3306/springdb"
          p:username="root"
          p:password="123456"/>

    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
          p:dataSource-ref="dataSource" />


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

    
    <aop:config proxy-target-class="true">
        <aop:pointcut id="serviceMethod"
                expression="(execution(* com.dn.service..*(..))) and
                   (@annotation(org.springframework.transaction.annotation.Transactional))"/>
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
    aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        tx:attributes>
    tx:advice>
beans>

另一个配置文件是spring-servlet,它是controller 级别的上下文,说白了就是 servlet 级别的初始化,它不涉及到除了转发之外的任何实体,所以它的作用范围仅仅限制在 servlet 级别,所以它的初始化应该是跟spring 的 DispatcherServlet 初始化在一起,是在 标签中初始化的,它有一个默认值就是【/WEB-INF/remoting-servlet.xml 】,它的对应的名称是【 servlet-name】-servlet.xml,所以如果你没有给servlet 制定配置文件的位置,并且在默认位置下也没有配置文件,那么系统启动的时候就会报错。
我们在这里给的servlet-name是spring,所以,系统启动的时候会自动去找spring-servlet.xml这个文件.

spring-servlet.xml具体内容:


    <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"
           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.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

           
    <context:component-scan base-package="com.dn.web"/>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:viewClass="org.springframework.web.servlet.view.JstlView"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp"/>                    
    beans>

其中遇到的一下问题总结:
主要是类找不到 java.lang.ClassNotFoundException(参照http://blog.csdn.net/xinghui_liu/article/details/7287662)
(1)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class
‘com.microsoft.sqlserver.jdbc.SQLServerDriver’
答:sqljdbc.jar

(2)java.lang.ClassNotFoundException: org.springframework.ejb.config.JeeNamespaceHandler
答:spring-remoting.jar

(3)java.lang.ClassNotFoundException: org.springframework.scripting.config.LangNamespaceHandler
答:spring-support.jar

(4)java.lang.ClassNotFoundException: org.springframework.transaction.config.TxNamespaceHandler
答:spring-dao.jar

(5)java.lang.ClassNotFoundException: org.springframework.aop.config.AopNamespaceHandler
答:spring-aop.jar

(6)java.lang.NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap
答:commons-collections-2.1.1.jar

(7)java.lang.ClassNotFoundException:org.springframework.scripting.config.LangNamespaceHandler
答:添加spring-support.jar包,具体路径为:MyEclipse 6.0\myeclipse\eclipse\plugins\com.genuitec.eclipse.springframework_6.0.1.zmyeclipse601200710\data\2.0\dist\modules

(8)java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
答:commons-logging.jar

(9)java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
答:aopalliance.jar包

(10)java.lang.NoClassDefFoundError:org/springframework/remoting/support/RemoteInvocationTraceInterceptor
答:org.springframework.context-3.0.5.RELEASE.jar 包

(11)java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
答:com.springsource.org.aopalliance-1.0.0.jar包

(12) 使用spring 调试提示:Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs.standard.tlv.JstlCoreTLV
答:原因是:缺少.jar文件
至于是缺少standard.jar还是servlet-api.jar还是两个都缺那就看你引了哪个了。

(13) java.lang.NoSuchMethodError: com.microsoft.util.UtilByteOrderedDataWriter.writeInt32(I)V
sqlserver2000的三个jar包出问题了,注意版本对应,看tomcat的log日志,一切问题就好解决了

(14)java.lang.NoClassDefFoundError:org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
缺少的两个jar包:aspectjrt.jar;aspectjweaver.jar ,这是springAOP所依赖的包

你可能感兴趣的:(spring)