Springmvc案例1----基于spring2.5的采用xml配置

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



修改xml文件:

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.     <servlet>  
  8.         <servlet-name>dispatcherServlet</servlet-name>  
  9.         <servlet-class>  
  10.             org.springframework.web.servlet.DispatcherServlet  
  11.         </servlet-class>  
  12.         <init-param>  
  13.             <param-name>contextConfigLocation</param-name>  
  14.             <param-value>  
  15.                 /WEB-INF/hib-config.xml,/WEB-INF/web-config.xml,  
  16.                 /WEB-INF/service-config.xml,/WEB-INF/dao-config.xml  
  17.             </param-value>  
  18.         </init-param>  
  19.         <load-on-startup>1</load-on-startup>  
  20.     </servlet>  
  21.     <servlet-mapping>  
  22.         <servlet-name>dispatcherServlet</servlet-name>  
  23.         <url-pattern>*.do</url-pattern>  
  24.     </servlet-mapping>  
  25. </web-app>  

增加web-config.xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="  
  5. http://www.springframework.org/schema/beans   
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  7.   
  8.     <!-- Controller方法调用规则定义 -->  
  9.     <bean id="paraMethodResolver"  
  10.         class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">  
  11.         <property name="paramName" value="action" />  
  12.         <property name="defaultMethodName" value="list" />  
  13.     </bean>  
  14.     <!-- 页面view层基本信息设定 -->  
  15.     <bean id="viewResolver"  
  16.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  17.         <property name="viewClass"  
  18.             value="org.springframework.web.servlet.view.JstlView"></property>  
  19.         <property name="suffix" value=".jsp"></property>  
  20.     </bean>  
  21.     <!-- servlet映射列表,所有控制层Controller的servlet在这里定义 -->  
  22.     <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  23.         <property name="mappings">  
  24.             <props>  
  25.                 <prop key="user.do">userController</prop>  
  26.             </props>  
  27.         </property>  
  28.     </bean>  
  29.     <bean id="userController" class="com.sxt.action.UserController">  
  30.         <property name="userService" ref="userService"></property>  
  31.     </bean>  
  32. </beans>  

增加service-config.xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation="  
  5.     http://www.springframework.org/schema/beans   
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  7.   
  8. <bean id="userService" class="com.sxt.service.UserService">  
  9.     <property name="userDao" ref="userDao"></property>  
  10. </bean>  
  11. </beans>  

增加dao-config.xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation="  
  5. http://www.springframework.org/schema/beans   
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  7.   
  8. <bean id="userDao" class="com.sxt.dao.UserDao">  
  9.     <property name="hibernateTemplate" ref="hibernateTemplate"></property>  
  10. </bean>  
  11. </beans>  

增加hib-config.xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xsi:schemaLocation="  
  8. http://www.springframework.org/schema/beans   
  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  10. http://www.springframework.org/schema/tx  
  11. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  12. http://www.springframework.org/schema/aop   
  13. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  14.   http://www.springframework.org/schema/context     
  15.    http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  16. ">  
  17. <context:component-scan base-package="com.sxt"></context:component-scan>  
  18. <!-- 支持aop注解 -->  
  19. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
  20.   
  21. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  22.     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  23.     <property name="url" value="jdbc:mysql://localhost:3306/crud"></property>  
  24.     <property name="username" value="root"></property>  
  25.     <property name="password" value="root"></property>  
  26. </bean>  
  27.   
  28. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  29.     <property name="dataSource">  
  30.         <ref bean="dataSource"></ref>  
  31.     </property>  
  32.     <property name="hibernateProperties">  
  33.         <props>  
  34.             <prop key="hibernate.dialect">  
  35.                 org.hibernate.dialect.MySQLDialect  
  36.             </prop>  
  37.             <prop key="hibernate.show_sql">  
  38.                 true  
  39.             </prop>  
  40.             <prop key="hibernate.hbm2ddl.auto">update</prop>  
  41.         </props>  
  42.     </property>  
  43.     <property name="packagesToScan">  
  44.         <value>com.sxt.po</value>  
  45.     </property>  
  46. </bean>  
  47.   
  48. <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
  49.     <property name="sessionFactory" ref="sessionFactory"></property>  
  50. </bean>  
  51.   
  52. <!-- 配置一个JdbcTemplate实例 -->  
  53. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  54.     <property name="dataSource" ref="dataSource"></property>  
  55. </bean>  
  56.   
  57. <!-- 配置事务管理 -->  
  58. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  59.     <property name="sessionFactory" ref="sessionFactory"></property>  
  60. </bean>  
  61.   
  62. <tx:annotation-driven transaction-manager="txManager"/>  
  63. <aop:config>  
  64.     <aop:pointcut expression="execution(public * com.sxt.service.impl.*.*(..))" id="businessService"/>  
  65.     <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService"/>  
  66. </aop:config>  
  67. <tx:advice id="txAdvice" transaction-manager="txManager">  
  68.     <tx:attributes>  
  69.         <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED"/>  
  70.         <!-- get开头的方法不需要在事务中运行。有些情况是没有必要使用事务的。比如获取数据,开启事务本身对性能本身是有一定的影响的 -->  
  71.         <tx:method name="*"/>  
  72.     </tx:attributes>  
  73. </tx:advice>  
  74. </beans>  

各类包的代码如下:

[html]  view plain  copy
 print ?
  1. package com.sxt.po;  
  2.   
  3. import javax.persistence.Entity;  
  4. import javax.persistence.GeneratedValue;  
  5. import javax.persistence.GenerationType;  
  6. import javax.persistence.Id;  
  7.   
  8. @Entity  
  9. public class User {  
  10.     private int id ;  
  11.     private String name ;  
  12.     @Id  
  13.     @GeneratedValue(strategy=GenerationType.AUTO)  
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.     public void setId(int id) {  
  18.         this.id = id;  
  19.     }  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.       
  27. }  

userDao

[html]  view plain  copy
 print ?
  1. package com.sxt.dao;  
  2.   
  3. import org.springframework.orm.hibernate3.HibernateTemplate;  
  4.   
  5. import com.sxt.po.User;  
  6.   
  7. public class UserDao {  
  8.     private HibernateTemplate hibernateTemplate ;  
  9.       
  10.     public void add(User u){  
  11.         System.out.println("userDao.add()");  
  12.         hibernateTemplate.save(u) ;  
  13.     }  
  14.     public HibernateTemplate getHibernateTemplate() {  
  15.         return hibernateTemplate;  
  16.     }  
  17.   
  18.     public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {  
  19.         this.hibernateTemplate = hibernateTemplate;  
  20.     }  
  21.       
  22. }  

userService

[html]  view plain  copy
 print ?
  1. package com.sxt.service;  
  2.   
  3. import com.sxt.dao.UserDao;  
  4. import com.sxt.po.User;  
  5.   
  6. public class UserService {  
  7.     private UserDao userDao ;  
  8.     public void add(String name){  
  9.         System.out.println("UserService.add()");  
  10.         User u = new User() ;  
  11.         u.setName(name) ;  
  12.         userDao.add(u) ;  
  13.     }  
  14.     public UserDao getUserDao() {  
  15.         return userDao;  
  16.     }  
  17.     public void setUserDao(UserDao userDao) {  
  18.         this.userDao = userDao;  
  19.     }  
  20.       
  21. }  

userController

[html]  view plain  copy
 print ?
  1. package com.sxt.action;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6. import org.springframework.web.servlet.ModelAndView;  
  7. import org.springframework.web.servlet.mvc.Controller;  
  8.   
  9. import com.sxt.service.UserService;  
  10.   
  11. public class UserController implements Controller{  
  12.     private UserService userService ;  
  13.     @Override  
  14.     public ModelAndView handleRequest(HttpServletRequest req,  
  15.             HttpServletResponse resp) throws Exception {  
  16.         // TODO Auto-generated method stub  
  17.         System.out.println("HelloController.handleRequest()");  
  18.         req.setAttribute("a", "bbb") ;  
  19.         userService.add(req.getParameter("name")) ;  
  20.         return new ModelAndView("index2");  
  21.     }  
  22.     public UserService getUserService() {  
  23.         return userService;  
  24.     }  
  25.     public void setUserService(UserService userService) {  
  26.         this.userService = userService;  
  27.     }  
  28.       
  29. }  

下面是显示层的jsp代码:

[html]  view plain  copy
 print ?
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.   </head>  
  22.     
  23.   <body>  
  24.     <form action="user.do" >  
  25.         姓名:<input type="text" name="name" />  
  26.         登录:<input type="submit" value="登录">  
  27.     </form>  
  28.   </body>  
  29. </html>  

index2.jsp

[html]  view plain  copy
 print ?
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index2.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     <%=request.getAttribute("a") %>  
  27.   </body>  
  28. </html>  

运行结果测试:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值