昨天花了一晚上的時間,參考下面博客地址,來搭建SSM框架,其中遇到了不少問題,我把所有的問題都記錄的下來。
http://blog.csdn.net/gebitan505/article/details/44455235/#comments
1.Failed to load ApplicationContext,IOException parsing XML document from class path resource [spring-mybatis.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring-mybatis.xml] cannot be opened because it does not exist
找不到spring-mybatis.xml文件
原因:classpath的路徑是在src/main/resource下,檢查后發現路徑寫錯了。classpath:config/spring-mybatis.xml寫成了classpath:spring-mybatis.xml
2.Error creating bean with name 'userService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [F:\workspace\testSpring\target\classes\cn\testSpring\dao\UserDao.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [config/spring-mybatis.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [F:\workspace\testSpring\target\classes\config\map\userMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'User'. Cause: java.lang.ClassNotFoundException: Cannot find class: User
原因:初讀前幾句以為是,注入userService失敗的問題,覺得是@Service @Resource private UserService userService 沒寫的原因,結果發現都寫了的。讀最后一句發現Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class.Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'User'. Cause: java.lang.ClassNotFoundException: Cannot find class: User 發現是mybatis中resultType那里的User出了問題
解決方案有兩種(1)<select id="queryUserByUserId" parameterType="String" resultType="User">
改為<select id="queryUserByUserId" parameterType="String" resultType="cn.testSpring.model.User">
(2) 在spring-mybatis配置文件中增加配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動掃描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:config/map/*.xml"></property>
<property name="typeAliasesPackage" value="cn.testSpring.model" />
</bean>
如果需要配置多個包,用逗號隔開如
<property name="typeAliasesPackage" value="cn.testSpring.model,cn.testSpring.vo" />
3.Cannot load JDBC driver class 'com.mysql.jdbc.Driver
原因:由於配置文件是由網上復制下來的,導致properties文件中的driver后面留有空格,去掉properties文件中value后面的空格即可
4.org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
原因:properties文件中的url有問題,我的是端口號寫錯了。導致jdbc驅動不能從服務器收到任何數據包,不能獲取jdbc連接,不能創建連接池。
5.org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 2 in XML document from class path resource [config/spring-mvc.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 10; 不允許有匹配 "[xX][mM][lL]" 的處理指令目標。
原因:XML文件格式不規范,由於我的web.xml是從網上復制下來的,前面保留有空格。
解決方式:就是去掉 <?xml version="1.0" encoding="UTF-8"?> 前面的空格。
6.404問題
訪問項目WBE-INFO下的jsp文件。由於在spring-mvc中有配置action的方法return的字符串加上前綴和后綴,所以不需要自己加上。
return “jsp的名字” 而不是/WEB-INF/jsp/"jsp的名字".jsp
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
訪問除了WEB-INFO之外的jsp文件可以直接通過URL訪問。對於外部訪問來說,web-inf下的文件都是不可見的(即不能通過url獲得web-info下的任何文件),所以,直接訪問WEB-INFO下的jsp是不允許的。
一般的可以把所有的頁面展示的jsp,js,css,圖片都放到Webapp下面。放到WEB-INF下面的資源,都是要通過servlet去跳轉頁面,才可以訪問。如放在webapp下的index.jsp可以直接訪問http://localhost:8080/testSpring/index.jsp,不用通過servlet.不過,訪問一定要落實到某個jsp資源上,不然會報404的。
新手上路不久,資歷尚且。如有問題,望指正。