ssm框架整合常见错误

  虽然三大框架特别特别的好用,但是,当我第一次把这三个框架用maven整合到一起的时候,各种错误接踵而至,下面来做一下三大框架整合的总结:

     首先是在导入三大框架的各种依赖包的时候,因为我用的是j2ee ecilpse,所以要导入j2ee的依赖包,现在这两个依赖包是这样的:

[html]  view plain copy
  1. <!-- j2ee的包 -->  
  2.     <dependency>  
  3.       <groupId>javax.servlet</groupId>  
  4.       <artifactId>servlet-api</artifactId>  
  5.       <version>3.0-alpha-1</version>  
  6.     </dependency>  
  7.     <dependency>  
  8.       <groupId>javax.servlet.jsp</groupId>  
  9.       <artifactId>jsp-api</artifactId>  
  10.       <version>2.2.1-b03</version>  
  11.     </dependency>  
如果这两个包的版本不合,一部署项目就会出现一个jsp什么Exception然后后面就是一个大大的nullpointerException,当初看到这个是十分恼火的,因为之前的上面两个依赖包不是兼容的版本,所以就报了类似的错误。所以包的导入应该像上面那样。


当然,这只是第一个错误,后面的更无语,下一个错误是:在你的项目和java源码的包上同时出现两个红叉,然后你一部署就出现各种错误,这时不要急,点开problems,发现是这个:Cannot change version of project facet Dynamic Web Module to 2.5,在j2eeeciplse中,这是啥意思呢?意思大概是你的web Module版本不能是2.5的,然后我把这个错误百度一下,结果很多,天花乱坠,其实真正的原因是你的jdk版本和javaweb  配置的版本不一致,因为eclipse会自动使用工具自带的jdk,然而你新建的maven项目是新的项目骨架,好的,那jdk自然就是跟不上节奏了,所以给一个正确操作的连接:按照这上面的操作就可以改变你当前项目的状况:http://blog.csdn.net/sunqing0316/article/details/43675837;这只是修改当前项目的状况,要治本,当然要把我们的默认的jdk设置成我们自己的jdk,

同时将这个jdk默认设置成你的安装的jdk版本,就可以解决问题了(链接博客里修改web.xml后要update  maven一下)。


还有就是如果某个jar包的包或者依赖包没有下载完全或者失败,但是maven并不会提示你的jar包出现了错误,一旦

出错了,他会提示一个你明明已经导入了包的一个类找不到,这时候  把pom.xml中的那个相应的jar包删除,再在网络好的情况下再下载,就不会有问题了。


遇到的最后一个问题就是三个框架的配置文件的配置问题,三个框架的配置文件一起放在source文件下:

最重要的是struts的action的class名要填spring的bean配置的你写得action:

sping的beans.xml;

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.          http://www.springframework.org/schema/context  
  9.         http://www.springframework.org/schema/context/spring-context.xsd  
  10.          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  11.          http://www.springframework.org/schema/tx  
  12.         http://www.springframework.org/schema/tx/spring-tx.xsd  
  13.          ">  
  14.    <!-- 配置action,这里beans的id就是spring中的action -->  
  15.     <bean id="studentAction" class="com.hyycinfo.ssmtest1.web.actions.StudentAction" scope="prototype">  
  16.         <property name="studentBiz" ref="studentBiz"></property>  
  17.     </bean>  
  18. </beans>   
struts.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  9.     <constant name="struts.devMode" value="true" />  
  10.     <constant name="struts.objectFactory" value="spring"></constant>  
  11.     <package name="default" namespace="/" extends="struts-default">  
  12.         <!-- 这里的class部分必须填写spring 的配置的action的id 名,这是由spring的ioc生成action对象 -->  
  13.         <action name="student_*" class="studentAction" method="{1}">  
  14.             <result name="success">  
  15.                 add_success.jsp  
  16.             </result>  
  17.         </action>  
  18.         
  19.     </package>  
  20. </struts>  
对的,就是这样,

下面是三个框架的整合的依赖配置:


[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.   
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.          http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context.xsd  
  11.          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  12.          http://www.springframework.org/schema/tx  
  13.         http://www.springframework.org/schema/tx/spring-tx.xsd  
  14.          ">  
  15.     <!-- 注解的读取 -->  
  16.     <context:annotation-config />  
  17.       
  18.     <context:component-scan base-package="com.hyycinfo" />  
  19.       
  20.     <!-- 使用注解的事务配置 -->  
  21.     <tx:annotation-driven transaction-manager="txManager"/>  
  22.     <!-- 使用spring自带的属性文件读取类完成读取配置文件的操作 -->  
  23.     <bean id="pphc"  
  24.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  25.         <property name="locations" value="classpath:db.properties" />  
  26.     </bean>  
  27.   
  28.     <!-- 配置dbcp数据源... 数据库联接池 ( jndi-> tomcat的数据库联接池 ) / c3p0 -->  
  29.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  30.         destroy-method="close">  
  31.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  32.         <property name="url" value="${jdbc.url}" />  
  33.         <property name="username" value="${jdbc.username}" />  
  34.         <property name="password" value="${jdbc.password}" />  
  35.   
  36.         <property name="maxActive" value="${jdbc.maxActive}" />  
  37.         <property name="minIdle" value="${jdbc.minIdle}" />  
  38.         <property name="maxIdle" value="${jdbc.maxIdle}" />  
  39.   
  40.     </bean>  
  41.       
  42.     <!-- 针对mybatis的整合配置 -->  
  43.     <!-- 配置sqlsessionfactory,找到项目下的mapper文件整合 -->  
  44.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >  
  45.         <property name="dataSource"  ref="dataSource"></property>  
  46.         <!-- 配置所有的mapper文件的位置 -->  
  47.         <property name="mapperLocations"  value="classpath*:com/hyycinfo/ssmtest1/dao/*.xml"></property>  
  48.     </bean>  
  49.     <!-- 配置扫描器,用于扫描所有的map文件 -->  
  50.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  51.         <!-- 配置扫描的映射文件对应的接口的目录 -->  
  52.         <property name="basePackage" value="com.hyycinfo.ssmtest1.dao" />  
  53.         <!-- 指定这个scanner所使用的sqlsessionfactory -->  
  54.         <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"></property>  
  55.     </bean>  
  56.     <!-- 注意:一定要配置与平台(dao层的实现)有关的事务管理器 -->  
  57.     <bean id="txManager"  
  58.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  59.         <property name="dataSource" ref="dataSource" />  
  60.     </bean>  
  61.   
  62.     <!-- 配置增强 -->  
  63.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  64.         <!-- 切入点: 这里要加入的就是切入点表达式 -->  
  65.         <tx:attributes>  
  66.             <!-- 查询的方法上配置只读事务.. -->  
  67.             <tx:method name="get*" read-only="true"/>  
  68.             <tx:method name="find*" read-only="true"/>  
  69.             <tx:method name="load*" read-only="true"/>  
  70.             <tx:method name="datagrid*" read-only="true"/>  
  71.             <!--其它的方法上加入事务.. -->  
  72.             <tx:method name="add*"  propagation="REQUIRED" />  
  73.             <tx:method name="update*" propagation="REQUIRED" />  
  74.             <tx:method name="del*" propagation="REQUIRED" />  
  75.             <tx:method name="save*" propagation="REQUIRED" />  
  76.         </tx:attributes>  
  77.     </tx:advice>  
  78.   
  79.     <!-- 配置切面 -->  
  80.     <aop:config>  
  81.         <aop:pointcut id="service"  
  82.             expression="execution(* com.hyycinfo.ssmtest1.biz.impl.*.*(..))" />  
  83.         <aop:advisor advice-ref="txAdvice" pointcut-ref="service" />  
  84.     </aop:config>  
  85.       
  86. </beans>   



所以啊,得到一个教训!在使用各种工具开发时,一定要确保开发环境的一致性:

第一:通用一个自己安装的jdk环境。

第二:tomcat,mysql,eclipse等的开发工具安装一定要按流程走,环境变量一定要配好,不能因为“能用”就不去配环   境变量。 

第三:eclipse的工具的设定:首先字符集把工作空间的全部设定为utf-8;

          jdk的默认设定全部改成默认的自己安装的版本的jdk,确定不用eclipse自带的jdk。


开发之路马虎不得啊

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值