Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架。 Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
下载JDK,根据自己的系统32位还是64位 安装过程一路NEXT到底。
配置环境变量
1.新建变量名:JAVA_HOME 变量值:G:\Java\jdk
2.编辑变量名:Path 在后面加上:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin
3 .新建变量名:CLASSPATH 变量值: .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
创建新的project
springmvc-config.xml代码:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 配置扫描器 -->
<context:component-scan base-package="com.zq.controller" />
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<!-- 配置视图解释器ViewResolver -->
<bean id="jspViewResolver" class="
org.springframework.web.servlet.view.InternalResourceViewResolver ">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置拦截器,先不加 -->
<!-- <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean
class="com.zq.interceptor.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors> -->
</beans>
jdbc.properties 代码:
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:8080/shop
user=root
password=root
applicationContext.xml 代码:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 配置扫描@Service @@Repository注解 -->
<context:component-scan base-package="com.zq.service " />
<!--读取jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 打开aspectj的注解开发方式 -->
<aop:aspectj-autoproxy />
<!-- 配置数据库连接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 注册一下事务管理器的驱动 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>