SpringMVC教程.docx

上传人:b****6 文档编号:8599908 上传时间:2023-02-01 格式:DOCX 页数:18 大小:266.80KB
下载 相关 举报
SpringMVC教程.docx_第1页
第1页 / 共18页
SpringMVC教程.docx_第2页
第2页 / 共18页
SpringMVC教程.docx_第3页
第3页 / 共18页
SpringMVC教程.docx_第4页
第4页 / 共18页
SpringMVC教程.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

SpringMVC教程.docx

《SpringMVC教程.docx》由会员分享,可在线阅读,更多相关《SpringMVC教程.docx(18页珍藏版)》请在冰豆网上搜索。

SpringMVC教程.docx

SpringMVC教程

目录

项目目录结构图:

2

第一步:

准备2

Sprng中的jar包2

Hibernate中的jar包3

其它jar包4

第二步:

配置文件5

从hibernate中复制并修改文件5

修改hibernate.properties文件6

修改log4j.properties文件6

编写spring配置文件7

新建applicationContext.xml文件7

新建spring-servlet.xml文件9

修改web.xml文件9

第三步:

业务逻辑10

Model层10

Dao层11

Service层12

Controller层(Action层)13

View层14

第四步:

测试15

开启服务器15

在浏览器中浏览16

运行结果:

16

------------------------------------------------------------刘云生

项目目录结构图:

项目结构图

第一步:

准备

Sprng中的jar包

首先从spring官网http:

//www.springsource.org/download/community中下载最新版本的spring版本,本例采用的是spring-framework-3.1.1.RELEASE。

初学者最好采用和此版本一样的版本。

下载后如图:

解压得到文件夹:

打开文件夹:

选择如下jar包:

将以上jar包复制到你的/web-INF下的/lib中(以下简称lib)。

Hibernate中的jar包

准备hibernate的jar包,从hibernate官网http:

//www.hibernate.org/downloads下载hibernate版本,本例采用的是hibernate-distribution-3.6.10.Final,下载后如下:

解压后:

打开hibernate文件夹,先选择如图中的jar包:

再打开hibernate文件夹中的lib文件夹:

将此文件夹的jar包全部复制到web项目的lib目录中并将jpa中的jar也复制到lib中。

将bytecode中的cglib复制到lib中。

分别如图:

其它jar包

以上是hibernate和spring中所要加入的包,除些之外还要加入如下包:

以上是所有的springMVC+hibernate的所需jar包。

将所有jar包复制到web项目的lib目录中。

第二步:

配置文件

从hibernate中复制并修改文件

首先从hibernate中的project文件夹中打到etc文件夹,复制其中如图所示的两个文件到classpath下,如图:

图2-1

图2-2

修改hibernate.properties文件

如下:

##HypersonicSQL

dataSource.password=

dataSource.username=root

dataSource.databaseName=test

dataSource.driverClassName=com.mysql.jdbc.Driver

dataSource.dialect=org.hibernate.dialect.MySQL5Dialect

dataSource.serverName=localhost:

3306

dataSource.url=jdbc:

mysql:

//localhost:

3306/test

dataSource.properties=user=${dataSource.username};databaseName=${dataSource.databaseName};serverName=${dataSource.serverName};password=${dataSource.password}

dataSource.hbm2ddl.auto=update

#hibernate.connection.urljdbc:

hsqldb:

hsql:

//localhost

#hibernate.connection.urljdbc:

hsqldb:

test

修改log4j.properties文件

如下:

log4j.rootLogger=warn,stdout

#log4j.logger.org.hibernate=info

#log4j.logger.org.hibernate=debug

以上两个文件未帖出来的表示不改。

编写spring配置文件

新建applicationContext.xml文件

在classpath下新建applicationContext.xml文件内容如下:

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

aop="http:

//www.springframework.org/schema/aop"xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

p="http:

//www.springframework.org/schema/p"xmlns:

tx="http:

//www.springframework.org/schema/tx"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beanshttp:

//www.springframework.org/schema/beans/spring-beans-3.0.xsd

http:

//www.springframework.org/schema/contexthttp:

//www.springframework.org/schema/context/spring-context-3.0.xsd

http:

//www.springframework.org/schema/aophttp:

//www.springframework.org/schema/aop/spring-aop-3.0.xsd

http:

//www.springframework.org/schema/txhttp:

//www.springframework.org/schema/tx/spring-tx-3.0.xsd">

component-scanbase-package="com.mvc"/>

--支持aop注解-->

aspectj-autoproxy/>

property-placeholderlocation="classpath:

/hibernate.properties"/>

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

${dataSource.dialect}

${dataSource.hbm2ddl.auto}

update

com.mvc.model

--扫描实体类,也就是平时所说的model-->

--配置事务管理-->

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

annotation-driventransaction-manager="txManager"/>

config>

pointcutexpression="execution(public*com.mvc.service.impl.*.*(..))"

id="businessService"/>

advisoradvice-ref="txAdvice"pointcut-ref="businessService"/>

config>

adviceid="txAdvice"transaction-manager="txManager">

attributes>

methodname="find*"read-only="true"propagation="NOT_SUPPORTED"/>

--get开头的方法不需要在事务中运行。

有些情况是没有必要使用事务的,比如获取数据。

开启事务本身对性能是有一定的影响的-->

methodname="*"/>

--其他方法在实务中运行-->

attributes>

advice>

新建spring-servlet.xml文件

在web-INF下新建spring-servlet.xml文件,内容如下:

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns:

p="http:

//www.springframework.org/schema/p"

xmlns:

mvc="http:

//www.springframework.org/schema/mvc"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

util="http:

//www.springframework.org/schema/util"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beanshttp:

//www.springframework.org/schema/beans/spring-beans-3.0.xsd

http:

//www.springframework.org/schema/contexthttp:

//www.springframework.org/schema/context/spring-context-3.0.xsd

http:

//www.springframework.org/schema/mvchttp:

//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http:

//www.springframework.org/schema/utilhttp:

//www.springframework.org/schema/util/spring-util-3.0.xsd">

--对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->

component-scanbase-package="com.mvc.web"/>

annotation-driven/>

--支持spring3.0新的mvc注解-->

--启动SpringMVC的注解功能,完成请求和注解POJO的映射-->

--对模型视图名称的解析,即在模型视图名称添加前后缀-->

p:

prefix="/WEB-INF/jsp/"p:

suffix=".jsp">

--如果使用jstl的话,配置下面的属性-->

修改web.xml文件

文件内容如下:

xmlversion="1.0"encoding="UTF-8"?

>

xmlns="

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocationclasspath:

applicationContext*.xml,/WEB-INF/spring-servlet.xml

1

springmvc

*.do

index.*

reg.jsp

第三步:

业务逻辑

Model层

新建实体类User.java,内容如下:

packagecom.mvc.model;

importjavax.persistence.Entity;

importjavax.persistence.GeneratedValue;

importjavax.persistence.Id;

@Entity

publicclassUser{

privateintid;

privateStringname;

privateStringpassword;

@Id

@GeneratedValue

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

}

Dao层

新建UserDao.java,内容如下:

packagecom.mvc.dao;

importjava.util.List;

importjavax.annotation.Resource;

importorg.springframework.orm.hibernate3.HibernateTemplate;

importorg.springframework.stereotype.Repository;

importcom.mvc.model.User;

@Repository("userDao")

publicclassUserDao{

@Resource

privateHibernateTemplatehibernateTemplate;

publicvoidadd(Useru){

hibernateTemplate.save(u);

}

publicbooleanfindUser(Useru){

List

>list=hibernateTemplate.find("fromUseruwhereu.name=?

",

u.getName());

if(list.size()>0){

returntrue;

}

returnfalse;

}

publicbooleanuserLogin(Useru){

List

>list=hibernateTemplate.find(

"fromUseruwhereu.name=?

andu.password=?

",u.getName(),

u.getPassword());

if(list.size()>0){

returntrue;

}

returnfalse;

}

}

Service层

新建UserService.java,内容如下:

packagecom.mvc.service;

importjavax.annotation.Resource;

importorg.springframework.stereotype.Service;

importcom.mvc.dao.UserDao;

importcom.mvc.model.User;

@Service("userService")

publicclassUserService{

@Resource

privateUserDaouserDao;

publicvoidadd(Useruser){

userDao.add(user);

}

publicbooleanexist(Useruser){

returnuserDao.findUser(user);

}

publicbooleanlogin(Useruser){

returnuserDao.userLogin(user);

}

}

Controller层(Action层)

新建UserControler.java,内容如下:

packagecom.mvc.web;

importjavax.annotation.Resource;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importcom.mvc.model.User;

importcom.mvc.service.UserService;

@Controller("userController")

@RequestMapping("/user.do")

publicclassUserController{

@Resource

privateUserServiceuserService;

@RequestMapping(params="method=reg")

publicStringreg(Useruser){

System.out.println("用户注册");

if(userService.exist(user)){

return"reg_fail";

}

userService.add(user);

return"reg_success";

}

@RequestMapping(params="method

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 教育学心理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1