SpringMVC框架学习笔记.docx

上传人:b****5 文档编号:6898402 上传时间:2023-01-12 格式:DOCX 页数:55 大小:392.49KB
下载 相关 举报
SpringMVC框架学习笔记.docx_第1页
第1页 / 共55页
SpringMVC框架学习笔记.docx_第2页
第2页 / 共55页
SpringMVC框架学习笔记.docx_第3页
第3页 / 共55页
SpringMVC框架学习笔记.docx_第4页
第4页 / 共55页
SpringMVC框架学习笔记.docx_第5页
第5页 / 共55页
点击查看更多>>
下载资源
资源描述

SpringMVC框架学习笔记.docx

《SpringMVC框架学习笔记.docx》由会员分享,可在线阅读,更多相关《SpringMVC框架学习笔记.docx(55页珍藏版)》请在冰豆网上搜索。

SpringMVC框架学习笔记.docx

SpringMVC框架学习笔记

SpringMVC框架学习笔记

1、SpringMVC背景介绍

Spring框架提供了构建Web 应用程序的全功能MVC模块。

使用Spring可插入的MVC架构,可以选择是使用内置的SpringWeb框架还是Struts这样的Web框架。

通过策略接口,Spring框架是高度可配置的,而且包含多种视图技术,例如JavaServerPages(JSP)技术、Velocity、Tiles、iText和POI。

SpringMVC框架并不知道使用的视图,所以不会强迫您只使用JSP技术。

SpringMVC分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

2、常见MVC框架比较

运行性能上:

Jsp+servlet>struts1>springmvc>struts2+freemarker>>struts2,ognl,值栈。

开发效率上,基本正好相反。

值得强调的是,springmvc开发效率和struts2不相上下。

Struts2的性能低的原因是因为OGNL和值栈造成的。

所以,如果你的系统并发量高,可以使用freemaker进行显示,而不是采用OGNL和值栈。

这样,在性能上会有相当大得提高。

3、基于spring2.5的采用XML配置的springMVC项目

注:

本项目全部基于XML配置。

同时,集成了hibernate。

采用的是:

springMVC+hibernate+spring的开发架构。

1.建立web项目

2.导入jar包(spring.jar,spring-webmvc.jar,commons-logging.jar。

其他jar包为hibernate相关jar包)

3.修改web.xml如下:

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

>

xmlns="

xmlns:

xsi="http:

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

xsi:

schemaLocation="

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/hib-config.xml,/WEB-INF/web-config.xml,/WEB-INF/service-config.xml,/WEB-INF/dao-config.xml

1

dispatcherServlet

*.do

4.增加web-config.xml(这里包含springmvc相关的相关配置)

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

>

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

xmlns:

xsi="http:

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

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd">

--Controller方法调用规则定义-->

class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">

--页面View层基本信息设定-->

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.JstlView"/>

---->

--servlet映射列表,所有控制层Controller的servlet在这里定义-->

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

userController

5.在WEB-INF下增加service-config.xml(这里包含service层类的相关配置)

PS:

这里主要配置业务逻辑层的内容

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

>

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

xmlns:

xsi="http:

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

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd">

6.在WEB-INF下增加hib-config.xml(这里包含spring集成hibernate相关的配置)

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

>

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

xmlns:

xsi="http:

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

xmlns:

aop="http:

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

xmlns:

tx="http:

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

xmlns:

context="http:

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

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beans

http:

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

http:

//www.springframework.org/schema/tx

http:

//www.springframework.org/schema/tx/spring-tx-2.5.xsd

http:

//www.springframework.org/schema/aop

http:

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

http:

//www.springframework.org/schema/context

http:

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

">

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

--支持aop注解-->

aspectj-autoproxy/>

class="mons.dbcp.BasicDataSource">

value="com.mysql.jdbc.Driver">

mysql:

//localhost:

3306/myhib">

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

--key的名字前面都要加hibernate.-->

org.hibernate.dialect.MySQLDialect

true

update

com.sxt.po

--配置一个JdbcTemplate实例-->

 

--配置事务管理-->

annotation-driventransaction-manager="txManager"/>

config>

pointcutexpression="execution(public*com.sxt.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>

7.在WEB-INF下增加dao-config.xml(这里包含dao层类的相关配置)

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

>

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

xmlns:

xsi="http:

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

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd">

8.建立相关类和包结构,如下图所示:

9.各类代码如下:

packagecom.sxt.po;

importjavax.persistence.Entity;

importjavax.persistence.GeneratedValue;

importjavax.persistence.GenerationType;

importjavax.persistence.Id;

@Entity

publicclassUser{

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

privateintid;

privateStringuname;

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicStringgetUname(){

returnuname;

}

publicvoidsetUname(Stringuname){

this.uname=uname;

}

}

packagecom.sxt.dao;

importorg.springframework.orm.hibernate3.HibernateTemplate;

importcom.sxt.po.User;

publicclassUserDao{

privateHibernateTemplatehibernateTemplate;

publicvoidadd(Useru){

System.out.println("UserDao.add()");

hibernateTemplate.save(u);

}

publicHibernateTemplategetHibernateTemplate(){

returnhibernateTemplate;

}

publicvoidsetHibernateTemplate(HibernateTemplatehibernateTemplate){

this.hibernateTemplate=hibernateTemplate;

}

}

packagecom.sxt.service;

importcom.sxt.dao.UserDao;

importcom.sxt.po.User;

publicclassUserService{

privateUserDaouserDao;

publicvoidadd(Stringuname){

System.out.println("UserService.add()");

Useru=newUser();

u.setUname(uname);

userDao.add(u);

}

publicUserDaogetUserDao(){

returnuserDao;

}

publicvoidsetUserDao(UserDaouserDao){

this.userDao=userDao;

}

}

packagecom.sxt.action;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.springframework.web.servlet.ModelAndView;

importorg.springframework.web.servlet.mvc.Controller;

importcom.sxt.service.UserService;

publicclassUserControllerimplementsController{

privateUserServiceuserService;

@Override

publicModelAndViewhandleRequest(HttpServletRequestreq,

HttpServletResponseresp)throwsException{

System.out.println("HelloController.handleRequest()");

req.setAttribute("a","aaaa");

userService.add(req.getParameter("uname"));

returnnewModelAndView("index");

}

publicUserServicegetUserService(){

returnuserService;

}

publicvoidsetUserService(UserServiceuserService){

this.userService=userService;

}

}

10.运行测试:

http:

//locahost:

8080/springmvc01/user.do?

uname=zhangsan。

结果:

数据库中增加zhangsan的记录。

页面跳转到index.jsp上,显示:

4、基于spring2.5注解实现的springMVC项目

我们采用sprngMVC开发项目时,通常都会采用注解的方式,这样可以大大提高我们的开发效率。

实现零配置。

下面我们从零开始重新做一个springMVC的配置。

这个项目完全采用注解的方式开发。

同时,我们以后的springMVC项目也都会采用注解的方式。

1.建立web项目

2.导入jar包(spring.jar,spring-webmvc.jar,commons-logging.jar。

其他jar包为hibernate相关jar包)

 

3.修改web.xml,文件内容如下:

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

>

xmlns="

xmlns:

xsi="http:

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

xsi:

schemaLocation="

springmvc

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

当前位置:首页 > 高中教育 > 其它课程

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

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