码迷,mamicode.com
首页 > 编程语言 > 详细

spring-struts-mybatis整合错误集锦

时间:2015-08-12 19:41:35      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:ssm三大框架整合

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

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

 <!-- j2ee的包 -->
		<dependency>
		  <groupId>javax.servlet</groupId>
		  <artifactId>servlet-api</artifactId>
		  <version>3.0-alpha-1</version>
		</dependency>
		<dependency>
		  <groupId>javax.servlet.jsp</groupId>
		  <artifactId>jsp-api</artifactId>
		  <version>2.2.1-b03</version>
		</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;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
         ">
   <!-- 配置action,这里beans的id就是spring中的action -->
	<bean id="studentAction" class="com.hyycinfo.ssmtest1.web.actions.StudentAction" scope="prototype">
		<property name="studentBiz" ref="studentBiz"></property>
	</bean>
</beans> 
struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
	<constant name="struts.objectFactory" value="spring"></constant>
    <package name="default" namespace="/" extends="struts-default">
    	<!-- 这里的class部分必须填写spring 的配置的action的id 名,这是由spring的ioc生成action对象 -->
		<action name="student_*" class="studentAction" method="{1}">
			<result name="success">
				add_success.jsp
			</result>
		</action>
      
    </package>
</struts>
对的,就是这样,

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


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"

	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
         ">
	<!-- 注解的读取 -->
    <context:annotation-config />
	
	<context:component-scan base-package="com.hyycinfo" />
	
	<!-- 使用注解的事务配置 -->
    <tx:annotation-driven transaction-manager="txManager"/>
	<!-- 使用spring自带的属性文件读取类完成读取配置文件的操作 -->
	<bean id="pphc"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="classpath:db.properties" />
	</bean>

	<!-- 配置dbcp数据源... 数据库联接池 ( jndi-> tomcat的数据库联接池 ) / c3p0 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<property name="maxActive" value="${jdbc.maxActive}" />
		<property name="minIdle" value="${jdbc.minIdle}" />
		<property name="maxIdle" value="${jdbc.maxIdle}" />

	</bean>
	
	<!-- 针对mybatis的整合配置 -->
	<!-- 配置sqlsessionfactory,找到项目下的mapper文件整合 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
		<property name="dataSource"  ref="dataSource"></property>
		<!-- 配置所有的mapper文件的位置 -->
		<property name="mapperLocations"  value="classpath*:com/hyycinfo/ssmtest1/dao/*.xml"></property>
	</bean>
	<!-- 配置扫描器,用于扫描所有的map文件 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置扫描的映射文件对应的接口的目录 -->
		<property name="basePackage" value="com.hyycinfo.ssmtest1.dao" />
		<!-- 指定这个scanner所使用的sqlsessionfactory -->
		<property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"></property>
	</bean>
	<!-- 注意:一定要配置与平台(dao层的实现)有关的事务管理器 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 配置增强 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<!-- 切入点: 这里要加入的就是切入点表达式 -->
		<tx:attributes>
			<!-- 查询的方法上配置只读事务.. -->
			<tx:method name="get*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="load*" read-only="true"/>
			<tx:method name="datagrid*" read-only="true"/>
			<!--其它的方法上加入事务.. -->
			<tx:method name="add*"  propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

	<!-- 配置切面 -->
	<aop:config>
		<aop:pointcut id="service"
			expression="execution(* com.hyycinfo.ssmtest1.biz.impl.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="service" />
	</aop:config>
	
</beans> 



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

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

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

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

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


开发之路马虎不得啊

       

版权声明:本文为博主原创文章,未经博主允许不得转载。

spring-struts-mybatis整合错误集锦

标签:ssm三大框架整合

原文地址:http://blog.csdn.net/u010214269/article/details/47446123

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!