搭建SSM框架出现的问题

昨天花了一晚上的时间,参考下面博客地址,来搭建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的。

 

新手上路不久,资历尚且。如有问题,望指正。

 

转载于:https://www.cnblogs.com/DaCheng91/p/5573783.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值