SSH全注解-annotation详细配置.
SSH全注解-annotation详细配置http://www.iteye.com/topic/816574
使用Spring2.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 version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts.xml,class定义的Action,应该跟Spring的注解@Controller("LoginAction")对应。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="struts2" extends="struts-default" namespace="/"> <action name="login" class="LoginAction"> <result>/index.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <aop:aspectj-autoproxy proxy-target-class="true"/> <context:annotation-config /> <context:component-scan base-package="com" /> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"> </property> <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=CITYU_DEV_TEST"> </property> <property name="username" value="sa"></property> <property name="password" value="asl12345"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSource" p:packagesToScan="com.pojo" > <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
DAO:
package com.dao.impl; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.dao.IDAO_TEST_TABLE; import com.pojo.TestTable; @Repository("DAO_TEST_TABLE") public class DAO_TEST_TABLE extends HibernateDaoSupport implements IDAO_TEST_TABLE { /* (non-Javadoc) * @see com.dao.impl.IDAO_TEST_TABLE#list() */ @Autowired public void setSessionFactoryOverride(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } @Override public List<TestTable> list(){ return getHibernateTemplate().find("from TestTable order by id.userName"); } /* (non-Javadoc) * @see com.dao.impl.IDAO_TEST_TABLE#save(com.pojo.TestTable) */ @Override public void save(TestTable t){ getHibernateTemplate().save(t); } }
Service:
package com.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.dao.IDAO_TEST_TABLE; import com.pojo.TestTable; import com.service.ITestTableService; @Service("TestTableService") @Transactional public class TestTableService implements ITestTableService { @Resource(name="DAO_TEST_TABLE") public IDAO_TEST_TABLE dao; /* (non-Javadoc) * @see com.service.impl.ITestTableService#list() */ @Override @Transactional(propagation=Propagation.REQUIRED, readOnly=true) public List<TestTable> list(){ return dao.list(); } /* (non-Javadoc) * @see com.service.impl.ITestTableService#save(com.pojo.TestTable) */ @Override @Transactional(propagation=Propagation.REQUIRED) public void save(TestTable t){ } public IDAO_TEST_TABLE getDao() { return dao; } public void setDao(IDAO_TEST_TABLE dao) { this.dao = dao; } }
Action:
package com.action; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import com.service.impl.TestTableService; @Controller("LoginAction") public class LoginAction extends ActionSupport { /** * */ private static final long serialVersionUID = -6434128483294080524L; @Resource(name="TestTableService") public TestTableService service; private String userName; private String userPassword; @Override public String execute() throws Exception { System.out.println("Call from action."); List list = service.list(); System.out.println(list.size()); return super.SUCCESS; }; @Override public void validate() { System.out.println(getText("test.i18n")); }; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public TestTableService getService() { return service; } public void setService(TestTableService service) { this.service = service; } }
2.Struts一并使用注解:
修改web.xml
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.action,com.actionother</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
去掉struts.xml的配置:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> </struts>
Action.java增加一些注解:
package com.action; import java.util.List; import javax.annotation.Resource; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import com.service.impl.TestTableService; @Controller("LoginAction") @Namespace("/") @ParentPackage("struts-default") public class LoginAction extends ActionSupport { /** * */ private static final long serialVersionUID = -6434128483294080524L; @Resource(name="TestTableService") public TestTableService service; private String userName; private String userPassword; @Override @Action(value="/login", results={ @Result(name="success",location="/index.jsp"), @Result(name="input",location="/login.jsp") } ) public String execute() throws Exception { System.out.println("Call from action."); List list = service.list(); System.out.println(list.size()); return super.SUCCESS; }; @Override public void validate() { System.out.println(getText("test.i18n")); }; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public TestTableService getService() { return service; } public void setService(TestTableService service) { this.service = service; } }
或者换一种配置:
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="struts2" extends="struts-default" namespace="/"> </package> </struts>
Action.java
@Controller("LoginAction")/*这里是可以省略的*/ @ParentPackage("struts2") /* @Result可以在这里配置。 */ public class LoginAction extends ActionSupport { ...... ...... }
3.使用通配符配置Result:
@Override @Action(value="/*PageAction" ,results={ @Result(name="success",location="/{1}.jsp") } ) public String execute() throws Exception { // TODO Auto-generated method stub //return super.execute(); System.out.println("@@ Call-->PageAction"); return "success"; }
调用Aciton的URL:indexPageAction.action
.