SpringMVC项目的搭建
来源:互联网 发布:淘宝商品名称 编辑:程序博客网 时间:2024/06/04 19:28
首先,eclipse中创建一个Dynamic Web Project
Target runtime选择 Tomcat6
一直next,最后勾选“generate web.xml deployment descriptor”
导入相关的jar包(可以在我的百度网盘下载,链接:http://pan.baidu.com/s/1boT9vLP 密码:h439)
规划包的目录结构
修改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>contextConfigLocation</param-name> <param-value>classpath:config/spring-context.xml</param-value> </context-param> <!--负责启动Spring容器的监听器,引用上下文参数获得Spring配置文件的地址--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--spring MVC的主控Servlet--> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping></web-app>
这里配置了两个配置文件:
一个是上面的 <param-value>classpath:config/spring-context.xml</param-value>
这里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>
或者定义某一个文件夹下符合某种规则的配置文件:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/spring/spring-*.xml</param-value> </context-param>
spring-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: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"> <!--扫描类包,将标注Spring注解的类自动转化为Bean,同时完成Bean的注入--> <context:component-scan base-package="com.dn.dao"/> <!--扫描service类包,应用Spring的注解配置--> <context:component-scan base-package="com.dn.service"/> <!--定义一个使用DBCP实现的数据源--> <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"/> <!-- 配置Jdbc模板 --> <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配置提供事务增强,让service包下所有bean的所有方法拥有事务--> <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具体内容:
<?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: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"> <!--扫描web包,应用Spring的注解--> <context:component-scan base-package="com.dn.web"/> <!--配置视图解析器,将ModelAndViewd及字符串解析为具体的页面--> <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所依赖的包
- SpringMVC项目的搭建
- SpringMvc项目框架的搭建
- SpringMVC项目的搭建过程
- SpringMVC+SpringJDBC搭建的项目Demo
- 基于maven的springmvc项目搭建
- 搭建使用springmvc的web项目
- 搭建springmvc项目遇到的问题
- 使用IDEA 搭建springmvc环境的项目
- springmvc+mybatis+redis的maven项目搭建
- springmvc web项目搭建
- 搭建SpringMVC项目
- eclipse搭建springMVC项目
- SpringMVC项目搭建
- springmvc+redis项目搭建
- SpringMVC项目环境搭建
- SpringMVC 注解 搭建项目
- springMvc+mybatis搭建项目
- springmvc搭建web项目
- junit一直出现空指针异常 尼玛啊
- 2017.11.07第五课
- 个人网站运营故事经验分享
- iOS面试题集合(BAT及各大中小型公司)
- Springmvc学习笔记壹——springmvc概述
- SpringMVC项目的搭建
- elasticsearch 5.5.0 java操作(新增索引、分页搜索)
- Android Token过期解决方案
- 用markdown使你的博文炫酷起来
- Java
- vue.js添加一些触摸事件以及安装fastclick
- 多线程之CompletableFuture(下)
- 每日练习2017-11-07
- 嵌入式linux的mdev机制