配置xml时一些jar和命名空间问题,如mvc:annotation-driven

本文记录了在使用Spring MVC搭建项目时遇到的一些配置问题,包括XML命名空间未绑定和缺失jar包导致的错误。针对"mvc:annotation-driven"和"context:component-scan"的命名空间问题,解决方案是在beans标签内引入相应命名空间。对于ClassNotFoundException: org.springframework.dao.support.DaoSupport,需要引入spring-tx.jar;PropertyAccessException问题则是因为缺少spring-jdbc包。

暑假用springmvc搭了一些小demo,刚开始和以前搭建ssh一样,出现了一些jar未加载和xml文件的命名空间没引入的问题。想想每次都是出了问题去网上百度,有点烦,就记下来了。

Q1.元素 "mvc:annotation-driven" 的前缀 "mvc"未绑定

办法:这是我在spring-servlet.xml文件里使用<mvc>开头的标签时,忘记引入了命名空间。在xml的beans里面加入如下代码即可

xmlns:mvc="http://www.springframework.org/schema/mvc"

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd


Q2.元素 "context:component-scan" 的前缀 "context"未绑定

办法:同样的原因,引入即可

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

(注意:spring的命名空间如果不加入版本号,比如spring-mvc-3.0.xsd去掉3.0版本号,就默认是最新的版本,在myeclipse里面可以按住CTRL键跟进去,查看具体的版本信息)


Q3.java.lang.ClassNotFoundException: org.springframework.dao.support.DaoSupport

办法:缺少spring-tx.jar,引入即可


Q4.PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource

办法:配置dao之后出现的问题,原因是缺少spring-jdbc包,引入即可

<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启用注解扫描 --> <context:component-scan base-package="com.example.productms" /> <!-- 启用Spring MVC注解 --> <mvc:annotation-driven /> <!-- 静态资源映射 --> <mvc:resources mapping="/resources/**" location="/resources/" /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 数据源配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/productdb?useSSL=false&serverTimezone=UTC" /> <property name="username" value="admin" /> <property name="password" value="admin" /> </bean> <!-- JDBC模板 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> </beans>为什莫开始报错了,这个应该怎末解决,详细的解决方法
最新发布
05-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值