Spring MVC框架实例
Spring MVC 背景介绍
Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,能够选择是使用内置的 Spring Web 框架还是 Struts 这种 Web 框架。通过策略接口。Spring 框架是高度可配置的。并且包括多种视图技术,比如 JavaServer Pages(JSP)技术、Velocity、Tiles、iText 和 POI。Spring MVC 框架并不知道使用的视图,所以不会强迫您仅仅使用 JSP 技术。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更easy进行定制。
下载源代码:Spring MVC框架实例
常见MVC框架比較
执行性能上:
Jsp+servlet>struts1>spring mvc>struts2+freemarker>>struts2,ognl,值栈。
开发效率上,基本正好相反。值得强调的是,spring mvc开发效率和struts2不相上下。
Struts2的性能低的原因是由于OGNL和值栈造成的。
所以,假设你的系统并发量高,能够使用freemaker进行显示,而不是採用OGNL和值栈。这样。在性能上会有相当大得提高。
基于spring2.5的採用XML配置的spring MVC项目
注:本项目所有基于XML配置。同一时候,集成了hibernate。採用的是:spring MVC+hibernate+spring的开发架构。
1. 建立web项目
2. 导入jar包(spring.jar, spring-webmvc.jar, commons-logging.jar。其它jar包为hibernate相关jar包)
3. 改动web.xml例如以下:
|
<? xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/hib-config.xml,/WEB-INF/web-config.xml,/WEB-INF/service-config.xml,/WEB-INF/dao-config.xml</param-value> </init-param> </load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> |
4. 添加web-config.xml(这里包括spring mvc相关的相关配置)
|
xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://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方法调用规则定义 --> <bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="action"/> <property name="defaultMethodName" value="list"/> </bean> <!-- 页面View层基本信息设定 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!--<property name="prefix" value="/myjsp/"/>--> <property name="suffix" value=".jsp"/> </bean> <!-- servlet映射列表,全部控制层Controller的servlet在这里定义 --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="user.do">userController</prop> </props> </property> </bean> <bean id="userController" class="com.sxt.action.UserController"> <property name="userService" ref="userService"></property> </bean> </beans> |
5. 在WEB-INF下添加service-config.xml(这里包括service层类的相关配置)
|
xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://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"> <bean id="userService" class="com.sxt.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> </beans> |
6. 在WEB-INF下添加hib-config.xml(这里包括spring集成hibernate相关的配置)
|
<?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: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 "> <context:component-scan base-package="com.sxt"/> <!-- 支持aop注解 --> <aop:aspectj-autoproxy /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/myhib"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <!-- key的名字前面都要加hibernate. --> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="packagesToScan"> <value>com.sxt.po</value> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--配置一个JdbcTemplate实例--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务管理 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager" /> <aop:config> <aop:pointcut expression="execution(public * com.sxt.service.impl.*.*(..))" id="businessService"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager" > <tx:attributes> <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" /> <!-- get开头的方法不须要在事务中执行 。 有些情况是没有必要使用事务的,比方获取数据。开启事务本身对性能是有一定的影响的--> <tx:method name="*"/> <!-- 其它方法在实务中执行 --> </tx:attributes> </tx:advice> </beans> |
7. 在WEB-INF下添加dao-config.xml(这里包括dao层类的相关配置)
|
xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://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"> <bean id="userDao" class="com.sxt.dao.UserDao"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> </beans> |
8. 建立相关类和包结构,例如以下图所看到的:
9. 各类代码例如以下:
|
package com.sxt.po; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String uname; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } } |
|
import org.springframework.orm.hibernate3.HibernateTemplate; import com.sxt.po.User; public class UserDao { private HibernateTemplate hibernateTemplate; public void add(User u){ System.out.println("UserDao.add()"); hibernateTemplate.save(u); } public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } } |
|
package com.sxt.service; import com.sxt.dao.UserDao; import com.sxt.po.User; public class UserService { private UserDao userDao; public void add(String uname){ System.out.println("UserService.add()"); User u = new User(); u.setUname(uname); userDao.add(u); } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } } |
|
package com.sxt.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.sxt.service.UserService; public class UserController implements Controller { private UserService userService; @Override public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { System.out.println("HelloController.handleRequest()"); req.setAttribute("a", "aaaa"); userService.add(req.getParameter("uname")); return new ModelAndView("index"); } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } } |
10. 执行測试:
uname=zhangsan">http://locahost:8080/springmvc01/user.do? uname=zhangsan
结果:数据库中添加zhangsan的记录。页面跳转到index.jsp上,显示:
下载源代码:Spring MVC框架实例
Spring MVC框架实例的更多相关文章
- 【WEB】初探Spring MVC框架
Spring MVC框架算是当下比较流行的Java开源框架.但实话实说,做了几年WEB项目,完全没有SpringMVC实战经验,乃至在某些交流场合下被同行严重鄙视“奥特曼”了.“心塞”的同时,只好默默 ...
- 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端。前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架)。
本文转自http://www.cnblogs.com/davidwang456/p/4090058.html 感谢作者 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并 ...
- Spring MVC篇一、搭建Spring MVC框架
本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容. 一.项目结构 本项目使用idea intellij创建,配合maven管理.整体的目录结构如图: 其中ja ...
- Spring MVC 框架的架包分析,功能作用,优点
由于刚搭建完一个MVC框架,决定分享一下我搭建过程中学习到的一些东西.我觉得不管你是个初级程序员还是高级程序员抑或是软件架构师,在学习和了解一个框架的时候,首先都应该知道的是这个框架的原理和与其有关j ...
- 从零开始学 Java - 搭建 Spring MVC 框架
没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...
- Spring mvc框架 controller间跳转 ,重定向 ,传参
一.需求背景 1. 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. @Req ...
- PHP: 手把手编写自己的 MVC 框架实例教程
1 什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...
- spring mvc 框架搭建及详解
现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不 ...
- Spring MVC框架搭建
Spring MVC篇一.搭建Spring MVC框架 本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容. 一.项目结构 本项目使用idea intellij ...
随机推荐
- hashCode 和 equals 方法
hashCode 和 equals 方法 hashCode()和equals()定义在Object类中,这个类是所有java类的基类,所以所有的java类都继承这两个方法. 使用hashCode()和 ...
- Network Stack : HTTP authentication
HTTP authentication As specified in RFC 2617, HTTP supports authentication using the WWW-Authenticat ...
- 如何利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例
前几天给大家分享了利用Python网络爬虫抓取微信朋友圈的动态(上)和利用Python网络爬虫爬取微信朋友圈动态——附代码(下),并且对抓取到的数据进行了Python词云和wordart可视化,感兴趣 ...
- 重写prototype原型后哪些东西改变了
参考<JavaScript高级教程>实例看: 1.重写原型对象后,首先原型对象的constructor属性值(constructor的指向)会发生改变. function Person() ...
- jq PC做滚动效果经常用到的各类参数【可视区判断】
获取 浏览器显示区域 (可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 : $(window).width(); 获取页面的文档高度: $( ...
- lsblk---列出所有可用块设备的信息,
lsblk命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息.块设备有硬盘,闪存盘,cd-ROM等等.lsblk命令包含在util-linux-ng包中,现 ...
- POJ 3613 Cow Relays 恰好n步的最短路径
http://poj.org/problem?id=3613 题目大意: 有T条路.从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min ...
- 新手学,java使用分水岭算法进行图像切割(一)
近期被图像切割整的天昏地暗的,在此感谢老朋友周洋给我关于分水岭算法的指点!本来打算等彩色图像切割有个完满的结果再写这篇文章,可是考虑到到了这一步也算是一个阶段,所以打算对图像切割做一个系列的博文,于是 ...
- 【java】itoo项目实战之大数据查询之使用 new map 优化hibernate之级联查询
在我的上一篇博客<[java]itoo项目实战之hibernate 懒载入优化性能>中,我曾提到过学生数据有2万条,查询数据十分的慢,这是让人非常受不了的事情.看着页面进度条一直转着圈圈, ...
- 设计模式实例(Lua)笔记之六(Adapter模式)
1.描写叙述 "我"在 2004 年的时候带了一个项目,做一个人力资源管理,该项目是我们总公司发起的项目,公司一共同拥有 700 多号人,包含子公司,这个项目还是比較简单的,分为三 ...