手把手使用springmvc

手把手使用springmvc

本文主要通过实例介绍怎么从新建工程通过配置,一步一步实现使用spirngmvc建立工程,实现基本的增删查改数据库等功能。

一、新建工程

1.打开myeclipse新建Web Project

点击next输入工程名TestWeb这里自己命名

点击finish完成。

新建工程出现在右侧工程区域,这里jre最好是5.0以上,本例中使用的是6.0

二、工程配置

1.相关jar包

下载相关spring的包以及其他依赖jar包,拷贝到web-inf/lib下 Spring相关的jar包,本例使用3.0.6版本,数据库使用mysql

其他工具和依赖包

2.配置applicationContext.xml文件

文件内容如下:

数据库使用mysql,如果使用其他数据库可更换驱动driverClassName以及url的值,本例连接的地址为本机127.0.0.1,端口为3306,库名为reports。其中DbExecuter使用jdbcTemplate操作数据库,DbExecuter可改为自己的数据库处理类,本例未使用Mybatis。

切面示例使用aopLog,为com.zkxx.reportforms.demo.action包下的所有类方法,若有不同可更换切入点表达式pointcut=\,切入方式为环绕around,也可以更改为

after,before。

xmlns=\xmlns:xsi=\xmlns:p=\

xmlns:context=\xmlns:aop=\xmlns:tx=\

xsi:schemaLocation=\ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd\

3.配置web.xml文件

配置web-inf下的web.xml文件,文件内容如下:

其中springmvc拦截.do的请求,这里可以根据自己的需求修改。

xmlns=\

xmlns:xsi=\xsi:schemaLocation=\ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\

index.jsp

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

org.springframework.web.util.IntrospectorCleanupListener

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4