SSH全注解-annotation详细配置

本文详细介绍如何在Spring、Hibernate及Struts框架中使用注解进行配置,包括Spring与Hibernate的整合配置、Struts注解驱动的Action配置,以及如何使用通配符配置Result。
SSH全注解-annotation详细配置  http://www.iteye.com/topic/816574  
使用 Spring 2.5 注释驱动的 IoC 功能  https://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/ 
只是参考. 

先根据 http://panyongzheng.iteye.com/blog/1103591配置好无注解,然后在根据下面的设定来实现全注解。当然,你可以只直接Spring和Hibernate,Struts.xml的东西依然保存。这个看你喜欢。 

@Results也可以用于整个Action注解,跟在@ParentPackage("struts-default") 注解的后面,那么这个@Result就相对应整个类。 

1.只注解Sprint & Hibernate的内容。 
2.Struts,Spring,Hibernate全部注解。 
3.使用通配符配置result。 



1.实现Spring & Hibernate注解(这里不针对Struts使用注解): 
web.xml 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0"   
  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_3_0.xsd">  
  7.       <display-name></display-name>   
  8.       <context-param>    
  9.           <param-name>contextConfigLocation</param-name>    
  10.           <param-value>/WEB-INF/classes/applicationContext.xml</param-value>    
  11.       </context-param>    
  12.     
  13.       <listener>    
  14.           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  15.       </listener>  
  16.       
  17.       
  18.       <filter>  
  19.         <filter-name>struts2</filter-name>  
  20.         <filter-class>  
  21.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  22.         </filter-class>  
  23.       </filter>  
  24.       <filter-mapping>  
  25.         <filter-name>struts2</filter-name>  
  26.         <url-pattern>/*</url-pattern>  
  27.       </filter-mapping>  
  28.         
  29.        
  30.       <welcome-file-list>  
  31.         <welcome-file>login.jsp</welcome-file>  
  32.         <welcome-file>index.jsp</welcome-file>  
  33.       </welcome-file-list>   
  34. </web-app>  



struts.xml,class定义的Action,应该跟Spring的注解@Controller("LoginAction")对应。 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.     <package name="struts2" extends="struts-default" namespace="/">  
  5.         <action name="login" class="LoginAction">  
  6.             <result>/index.jsp</result>  
  7.             <result name="input">/login.jsp</result>  
  8.         </action>  
  9.     </package>  
  10.   
  11. </struts>  
    


applicationContext.xml 
Xml代码   收藏代码
  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:p="http://www.springframework.org/schema/p"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"    
  6.     xmlns:aop="http://www.springframework.org/schema/aop"    
  7.     xmlns:context="http://www.springframework.org/schema/context"   
  8.     xsi:schemaLocation="  
  9.         http://www.springframework.org/schema/beans   
  10.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  13.         http://www.springframework.org/schema/context   
  14.         http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  15.         http://www.springframework.org/schema/tx   
  16.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
  17.         ">  
  18.   
  19.     <aop:aspectj-autoproxy proxy-target-class="true"/>    
  20.     <context:annotation-config />  
  21.     <context:component-scan base-package="com" />  
  22.     <tx:annotation-driven transaction-manager="transactionManager" />   
  23.       
  24.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  25.         <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver">  
  26.         </property>  
  27.         <property name="url"  
  28.             value="jdbc:sqlserver://localhost:1433;databaseName=CITYU_DEV_TEST">  
  29.         </property>  
  30.         <property name="username" value="sa"></property>  
  31.         <property name="password" value="asl12345"></property>  
  32.     </bean>  
  33.     <bean id="sessionFactory"  
  34.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"  
  35.         p:dataSource-ref="dataSource" p:packagesToScan="com.pojo"  
  36.         >  
  37.         <property name="hibernateProperties">  
  38.             <props>  
  39.                 <prop key="hibernate.dialect">  
  40.                     org.hibernate.dialect.SQLServerDialect  
  41.                 </prop>  
  42.             </props>  
  43.         </property>  
  44.     </bean>  
  45.   
  46.     <bean id="transactionManager"    
  47.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">    
  48.         <property name="sessionFactory" ref="sessionFactory" />    
  49.     </bean>  
  50. </beans>  


DAO: 
Java代码   收藏代码
  1. package com.dao.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.hibernate.SessionFactory;  
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  8. import org.springframework.stereotype.Repository;  
  9.   
  10. import com.dao.IDAO_TEST_TABLE;  
  11. import com.pojo.TestTable;  
  12.   
  13. @Repository("DAO_TEST_TABLE")  
  14. public class DAO_TEST_TABLE extends HibernateDaoSupport implements IDAO_TEST_TABLE {  
  15.       
  16.     /* (non-Javadoc) 
  17.      * @see com.dao.impl.IDAO_TEST_TABLE#list() 
  18.      */  
  19.     @Autowired  
  20.     public void setSessionFactoryOverride(SessionFactory sessionFactory){  
  21.         super.setSessionFactory(sessionFactory);  
  22.     }  
  23.       
  24.       
  25.     @Override  
  26.     public List<TestTable> list(){  
  27.         return getHibernateTemplate().find("from TestTable order by id.userName");  
  28.     }  
  29.       
  30.     /* (non-Javadoc) 
  31.      * @see com.dao.impl.IDAO_TEST_TABLE#save(com.pojo.TestTable) 
  32.      */  
  33.     @Override  
  34.     public void save(TestTable t){  
  35.         getHibernateTemplate().save(t);  
  36.     }  
  37.   
  38. }  


Service: 
Java代码   收藏代码
  1. package com.service.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.annotation.Resource;  
  6.   
  7. import org.springframework.stereotype.Service;  
  8. import org.springframework.transaction.annotation.Propagation;  
  9. import org.springframework.transaction.annotation.Transactional;  
  10.   
  11. import com.dao.IDAO_TEST_TABLE;  
  12. import com.pojo.TestTable;  
  13. import com.service.ITestTableService;  
  14.   
  15. @Service("TestTableService")  
  16. @Transactional  
  17. public class TestTableService implements ITestTableService {  
  18.     @Resource(name="DAO_TEST_TABLE")  
  19.     public IDAO_TEST_TABLE dao;  
  20.     /* (non-Javadoc) 
  21.      * @see com.service.impl.ITestTableService#list() 
  22.      */  
  23.     @Override  
  24.     @Transactional(propagation=Propagation.REQUIRED, readOnly=true)  
  25.     public List<TestTable> list(){  
  26.         return dao.list();  
  27.     }  
  28.     /* (non-Javadoc) 
  29.      * @see com.service.impl.ITestTableService#save(com.pojo.TestTable) 
  30.      */  
  31.     @Override  
  32.     @Transactional(propagation=Propagation.REQUIRED)  
  33.     public void save(TestTable t){  
  34.   
  35.     }  
  36.     public IDAO_TEST_TABLE getDao() {  
  37.         return dao;  
  38.     }  
  39.     public void setDao(IDAO_TEST_TABLE dao) {  
  40.         this.dao = dao;  
  41.     }  
  42.       
  43.       
  44. }  


Action: 
Java代码   收藏代码
  1. package com.action;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.annotation.Resource;  
  6.   
  7. import org.springframework.stereotype.Controller;  
  8.   
  9. import com.opensymphony.xwork2.ActionSupport;  
  10. import com.service.impl.TestTableService;  
  11.   
  12. @Controller("LoginAction")  
  13. public class LoginAction extends ActionSupport {  
  14.   
  15.     /** 
  16.      *  
  17.      */  
  18.     private static final long serialVersionUID = -6434128483294080524L;  
  19.     @Resource(name="TestTableService")  
  20.     public TestTableService service;  
  21.     private String userName;  
  22.     private String userPassword;  
  23.       
  24.       
  25.     @Override  
  26.     public String execute() throws Exception {  
  27.           
  28.         System.out.println("Call from action.");  
  29.         List list = service.list();  
  30.         System.out.println(list.size());  
  31.         return super.SUCCESS;  
  32.     };  
  33.       
  34.     @Override  
  35.     public void validate() {  
  36.         System.out.println(getText("test.i18n"));  
  37.     };  
  38.       
  39.       
  40.       
  41.       
  42.       
  43.       
  44.     public String getUserName() {  
  45.         return userName;  
  46.     }  
  47.     public void setUserName(String userName) {  
  48.         this.userName = userName;  
  49.     }  
  50.     public String getUserPassword() {  
  51.         return userPassword;  
  52.     }  
  53.     public void setUserPassword(String userPassword) {  
  54.         this.userPassword = userPassword;  
  55.     }  
  56.   
  57.     public TestTableService getService() {  
  58.         return service;  
  59.     }  
  60.   
  61.     public void setService(TestTableService service) {  
  62.         this.service = service;  
  63.     }  
  64.   
  65. }  




2.Struts一并使用注解: 
修改web.xml 
Xml代码   收藏代码
  1. <filter>  
  2.         <filter-name>struts2</filter-name>  
  3.         <filter-class>  
  4.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  5.         </filter-class>  
  6.         <init-param>    
  7.             <param-name>actionPackages</param-name>    
  8.             <param-value>com.action,com.actionother</param-value>    
  9.        </init-param>   
  10.       </filter>  
  11.       <filter-mapping>  
  12.         <filter-name>struts2</filter-name>  
  13.         <url-pattern>/*</url-pattern>  
  14.       </filter-mapping>  


去掉struts.xml的配置: 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4. </struts>     


Action.java增加一些注解: 
Java代码   收藏代码
  1. package com.action;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.annotation.Resource;  
  6.   
  7. import org.apache.struts2.convention.annotation.Action;  
  8. import org.apache.struts2.convention.annotation.Namespace;  
  9. import org.apache.struts2.convention.annotation.ParentPackage;  
  10. import org.apache.struts2.convention.annotation.Result;  
  11. import org.springframework.stereotype.Controller;  
  12.   
  13. import com.opensymphony.xwork2.ActionSupport;  
  14. import com.service.impl.TestTableService;  
  15.   
  16. @Controller("LoginAction")  
  17. @Namespace("/")  
  18. @ParentPackage("struts-default")  
  19. public class LoginAction extends ActionSupport {  
  20.   
  21.     /** 
  22.      *  
  23.      */  
  24.     private static final long serialVersionUID = -6434128483294080524L;  
  25.     @Resource(name="TestTableService")  
  26.     public TestTableService service;  
  27.     private String userName;  
  28.     private String userPassword;  
  29.       
  30.       
  31.     @Override  
  32.     @Action(value="/login",  
  33.         results={  
  34.             @Result(name="success",location="/index.jsp"),  
  35.             @Result(name="input",location="/login.jsp")  
  36.             }  
  37.     )  
  38.     public String execute() throws Exception {  
  39.           
  40.         System.out.println("Call from action.");  
  41.         List list = service.list();  
  42.         System.out.println(list.size());  
  43.         return super.SUCCESS;  
  44.     };  
  45.       
  46.     @Override  
  47.     public void validate() {  
  48.         System.out.println(getText("test.i18n"));  
  49.     };  
  50.       
  51.       
  52.       
  53.       
  54.       
  55.       
  56.     public String getUserName() {  
  57.         return userName;  
  58.     }  
  59.     public void setUserName(String userName) {  
  60.         this.userName = userName;  
  61.     }  
  62.     public String getUserPassword() {  
  63.         return userPassword;  
  64.     }  
  65.     public void setUserPassword(String userPassword) {  
  66.         this.userPassword = userPassword;  
  67.     }  
  68.   
  69.     public TestTableService getService() {  
  70.         return service;  
  71.     }  
  72.   
  73.     public void setService(TestTableService service) {  
  74.         this.service = service;  
  75.     }  
  76. }  


或者换一种配置
struts.xml 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.     <package name="struts2" extends="struts-default" namespace="/">  
  5.     </package>  
  6. </struts>   
  


Action.java 
Java代码   收藏代码
  1. @Controller("LoginAction")/*这里是可以省略的*/  
  2. @ParentPackage("struts2")  
  3. /* 
  4.    @Result可以在这里配置。 
  5. */  
  6. public class LoginAction extends ActionSupport {  
  7.       ......  
  8.       ......  
  9. }  



3.使用通配符配置Result: 
Java代码   收藏代码
  1. @Override  
  2.     @Action(value="/*PageAction"  
  3.         ,results={  
  4.                 @Result(name="success",location="/{1}.jsp")  
  5.             }  
  6.     )  
  7.     public String execute() throws Exception {  
  8.         // TODO Auto-generated method stub  
  9.         //return super.execute();  
  10.         System.out.println("@@ Call-->PageAction");  
  11.         return "success";  
  12.     }  

调用Aciton的URL: indexPageAction.action 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值