首先是项目和所须要的包截图:

改动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>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>
</web-app>

添加web-config.xml

<?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><property name="suffix" value=".jsp"></property></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>

添加service-config.xml

<?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>

添加dao-config.xml

<?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>

添加hib-config.xml

<?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.xsdhttp://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<context:component-scan base-package="com.sxt"></context:component-scan>
<!-- 支持aop注解 -->
<aop:aspectj-autoproxy></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/crud"></property><property name="username" value="root"></property><property name="password" value="root"></property>
</bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource"><ref bean="dataSource"></ref></property><property name="hibernateProperties"><props><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"></property>
</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>

各类包的代码例如以下:

package com.sxt.po;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class User {private int id ;private String name ;@Id@GeneratedValue(strategy=GenerationType.AUTO)public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

userDao

package com.sxt.dao;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;}}

userService

package com.sxt.service;import com.sxt.dao.UserDao;
import com.sxt.po.User;public class UserService {private UserDao userDao ;public void add(String name){System.out.println("UserService.add()");User u = new User() ;u.setName(name) ;userDao.add(u) ;}public UserDao getUserDao() {return userDao;}public void setUserDao(UserDao userDao) {this.userDao = userDao;}}

userController

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 ;@Overridepublic ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception {// TODO Auto-generated method stubSystem.out.println("HelloController.handleRequest()");req.setAttribute("a", "bbb") ;userService.add(req.getParameter("name")) ;return new ModelAndView("index2");}public UserService getUserService() {return userService;}public void setUserService(UserService userService) {this.userService = userService;}}

以下是显示层的jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form action="user.do" >姓名:<input type="text" name="name" />登录:<input type="submit" value="登录"></form></body>
</html>

index2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index2.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><%=request.getAttribute("a") %></body>
</html>

执行结果測试:

http://locahost:8080/springmvc01/user.do?uname=zhangsan。

转载于:https://www.cnblogs.com/gcczhongduan/p/4315265.html

Springmvc案例1----基于spring2.5的採用xml配置相关推荐

  1. Shiro 核心功能案例讲解 基于SpringBoot 有源码

    Shiro 核心功能案例讲解 基于SpringBoot 有源码 从实战中学习Shiro的用法.本章使用SpringBoot快速搭建项目.整合SiteMesh框架布局页面.整合Shiro框架实现用身份认 ...

  2. Mr.张小白(案例:基于Spring MVC实现后台登陆系统验证)

    基于Spring MVC实现后台登陆系统验证 一.步骤 1.引入相关依赖pom.xml <?xml version="1.0" encoding="UTF-8&qu ...

  3. Mr.张小白(案例:基于Spring MVC实现文件上传和下载)

    基于Spring MVC实现文件上传和下载 一.步骤 1.引入相关依赖pom.xml <?xml version="1.0" encoding="UTF-8&quo ...

  4. 2021年大数据Flink(二十):案例二 基于数量的滚动和滑动窗口

    目录 案例二 基于数量的滚动和滑动窗口 需求 代码实现 案例二 基于数量的滚动和滑动窗口 需求 需求1:统计在最近5条消息中,各自路口通过的汽车数量,相同的key每出现5次进行统计--基于数量的滚动窗 ...

  5. 2021年大数据Flink(十九):案例一 基于时间的滚动和滑动窗口

    目录 案例一 基于时间的滚动和滑动窗口 需求 代码实现 案例一 基于时间的滚动和滑动窗口 需求 nc -lk 9999 有如下数据表示: 信号灯编号和通过该信号灯的车的数量 9,3 9,2 9,7 4 ...

  6. TF学习:Tensorflow基础案例、经典案例集合——基于python编程代码的实现

    TF学习:Tensorflow基础案例.经典案例集合--基于python编程代码的实现 目录 Tensorflow的使用入门 1.TF:使用Tensorflow输出一句话 2.TF实现加法 3.TF实 ...

  7. Oracle Study案例之--基于表空间的时间点恢复(TSPITR)

     Oracle Study案例之--基于表空间的时间点恢复(TSPITR) TSPITR(表空间时间点恢复)用于将一个或多个表空间恢复到过去某个时间点的状态,而其他表空间仍然保持现有状态. TSPIT ...

  8. SpringMvc 04 基于注解的映射器与适配器配置

    SpringMvc的两种基于注解的映射器与适配器配置: 1.通过显式的配置映射器与适配器,并通过自动扫描标签去加载Controller类. <?xml version="1.0&quo ...

  9. 安卓案例:基于HttpClient下载文本与图片

    安卓案例:基于HttpClient下载文本与图片 一.利用HttpClient访问网络资源 1.创建http请求(get方式.post方式)  2.创建http客户端 

最新文章

  1. 一分钟AI | Numpy将放弃Python2.7全面支持Python3,柯洁苦战终结AI41连胜深夜失眠发文感慨
  2. 一个女生写的如何追mm.看完后嫩头青变高手.zz(转贴)
  3. AI模糊测试:下一个重大网络安全威胁
  4. Redis 的性能幻想与残酷现实(转)
  5. 《电路分析导论(原书第12版)》一2.5.1 电池
  6. JavaScript substr() 和 substring() 方法的区别
  7. 多线程基础-实现多线程的两种方式(二)
  8. ECharts 仪表盘的轴线宽度修改
  9. WWDC 2015大会到来了
  10. SharedMaterial的一些问题
  11. ActiveMQ 下载历史版本
  12. mysql获取字符串长度函数
  13. java char a z_java中,char A,char a的值各是多少?
  14. 阿里云Nginx配置站点403Forbidden问题
  15. PHP微信公众平台开发高级篇--群发接口
  16. 公众号文章留言评论功能开通方法(详解)
  17. 逻辑表达式 -- 对蕴含的理解(举例更清晰、明白哦)
  18. arista eos系统从零开始研究(1)
  19. Arduino温度传感器全系列使用详解
  20. Google Play的QUERY_ALL_PACKAGES或REQUEST_INSTALL_PACKAGES权限问题

热门文章

  1. UVA 701 The Archeologists' Dilemma
  2. MemCached java client 1.5.1 性能测试
  3. 「MTA」的「錯誤訊息代碼」
  4. webpack.config.js配置遇到Error: Cannot find module '@babel/core'问题
  5. JQuery UI之Autocomplete(3)属性与事件
  6. JS——EasyuiCombobox三级联动
  7. 射频识别技术漫谈(14)——S50与S70存取控制【worldsing笔记】
  8. 发现了一个很好的做excel、ppt 水晶易表、spss的好网站
  9. ListView Viewholder的坑 线性布局的坑
  10. android 无线调试