基于spring3.0注解实现的spring MVC项目

来源:互联网 发布:大众软件电子版下载 编辑:程序博客网 时间:2024/06/06 09:03

链接:https://item.taobao.com/item.htm?spm=a230r.1.14.1.76bf523wIsO2k&id=547598359683&ns=1&abbucket=15#detail


我们采用sprng MVC开发项目时,通常都会采用注解的方式,这样可以大大提高我们的开发效率。实现零配置。下面我们从零开始重新做一个spring MVC的配置。这个项目完全采用注解的方式开发。

前面的建立项目导入jar包不再叙述。

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

[html] view plain copy
  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>springmvc</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>/WEB-INF/hib-config.xml,/WEB-INF/springmvc-servlet.xml</param-value>  
  15.         </init-param>  
  16.         <load-on-startup>1</load-on-startup>  
  17.     </servlet>  
  18.   
  19.     <servlet-mapping>  
  20.         <servlet-name>springmvc</servlet-name>  
  21.         <url-pattern>*.do</url-pattern>  
  22.     </servlet-mapping>  
  23.   
  24. </web-app>  

2、springmvc-servlet.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:p="http://www.springframework.org/schema/p"       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/context       http://www.springframework.org/schema/context/spring-context-2.5.xsd">             <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->      <context:component-scan base-package="com.sxt"/>        <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>        <!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:suffix=".jsp"/>     </beans> 

3、 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>  

view plai co

4、javaBean类

[java] view plain copy
  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.     @Id  
  11.     @GeneratedValue(strategy=GenerationType.AUTO)  
  12.     private int id;  
  13.     private String uname;  
  14.     private String pwd;  
  15.       
  16.       
  17.     public String getPwd() {  
  18.         return pwd;  
  19.     }  
  20.     public void setPwd(String pwd) {  
  21.         this.pwd = pwd;  
  22.     }  
  23.     public int getId() {  
  24.         return id;  
  25.     }  
  26.     public void setId(int id) {  
  27.         this.id = id;  
  28.     }  
  29.     public String getUname() {  
  30.         return uname;  
  31.     }  
  32.     public void setUname(String uname) {  
  33.         this.uname = uname;  
  34.     }  
  35.       
  36. } 

5、dao类

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


6、service类

[java] view plain copy
  1. package com.sxt.service;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Service;  
  6.   
  7. import com.sxt.dao.UserDao;  
  8. import com.sxt.po.User;  
  9.   
  10. @Service("userService")  
  11. public class UserService {  
  12.     @Resource  
  13.     private UserDao userDao;  
  14.       
  15.     public void add(String uname){  
  16.         System.out.println("UserService.add()");  
  17.         User u = new User();  
  18.         u.setUname(uname);  
  19.         userDao.add(u);  
  20.     }  
  21.   
  22.     public UserDao getUserDao() {  
  23.         return userDao;  
  24.     }  
  25.   
  26.     public void setUserDao(UserDao userDao) {  
  27.         this.userDao = userDao;  
  28.     }  
  29.       
  30. }  


7、control类

[java] view plain copy
  1. package com.sxt.web;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.ui.ModelMap;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RequestParam;  
  9. import org.springframework.web.bind.annotation.SessionAttributes;  
  10.   
  11. import com.sxt.po.User;  
  12. import com.sxt.service.UserService;  
  13.   
  14.   
  15. @Controller("userController")  
  16. @RequestMapping("/user.do")       
  17. public class UserController  {  
  18.   
  19.     @Resource  
  20.     private UserService userService;  
  21.       
  22.     @RequestMapping(params="method=reg")   
  23.     public String reg(String uname) {  
  24.         System.out.println("HelloController.handleRequest()");  
  25.         userService.add(uname);   
  26.         return "index";  
  27.     }  
  28.       
  29.     public UserService getUserService() {  
  30.         return userService;  
  31.     }  
  32.   
  33.     public void setUserService(UserService userService) {  
  34.         this.userService = userService;  
  35.     }  
  36.   
  37.       
  38. }  


8、WEB-INF下建立index.jspIndex.jsp的内容如下:

<body>  <h1>**********${params.uname}</h1>  <h1>**********${requestScope.u}</h1>  <h1>**********${requestScope.user}</h1>  </body>  

好了,基于spring3.0注解实现的spring MVC项目也已经完成了。相比配置xml更少一点,不过对于没有经验的朋友来说结构不是很清晰,建立没有经验的朋友先从配置xml学起,然后再学习注解方法。

阅读全文
0 0