新手學習中,就看成筆記了...html
先來講一下配置文件版的:java
1.首先須要導入須要用到的jar包web
2.配置web.xmlspring
?xml version="1.0" encoding="UTF-8"?> <web-a<pp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 配置spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置struts2 --> <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>*.action</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
3.編寫Dao接口以及Dao的實現類sql
Dao接口:express
public interface UserDaoI { public List<User> getAll(); }
Dao實現類apache
public class UserDaoImpl extends HibernateDaoSupport implements UserDaoI { @Override public List<User> getAll() { return (List<User>) this.getHibernateTemplate().find("from User"); } }
4.編寫Service接口以及Service的實現類session
Service接口app
public interface UserServiceI { public List<User> getAll(); }
Service實現類jsp
public class UserServiceImpl implements UserServiceI { private UserDaoI userDao; public void setUserDao(UserDaoI userDao) { this.userDao = userDao; } @Override public List<User> getAll() { return userDao.getAll(); } }
5.編寫Action
public class UserAction { private UserServiceI userService; private List<User> list; public String list() { list = userService.getAll(); return "success"; } public void setUserService(UserServiceI userService) { this.userService = userService; } public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } }
6.配置applicationContext.xml
頭文件中spring-context的xsd版本不寫有時候會找不到會報錯,最好寫上,好比:spring-context-2.5.xsd
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 導入db.properties配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置數據源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc_driverClassName}"></property> <property name="url" value="${jdbc_url}"></property> <property name="username" value="${jdbc_username}"></property> <property name="password" value="${jdbc_password}"></property> </bean> <!-- 聲明式事務管理 --> <!-- 配置事務管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事務通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="save" propagation="SUPPORTS"/> <tx:method name="get*" read-only="true"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="select" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置aop --> <aop:config> <aop:pointcut expression="execution(* com.bjsxt.service.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <!-- 定義hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 依賴注入數據源 --> <property name="dataSource" ref="dataSource"></property> <!-- mappingResources指定具體的映射文件名 --> <!-- mappingLocations可使用通配符 --> <property name="mappingLocations"> <list> <value>classpath:com/bjsxt/vo/*.hbm.xml</value> </list> </property> <!-- 定義hibernate的SessionFactory的屬性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> </props> </property> </bean> <!-- 定義HibernateDaoSupport --> <bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate4.support.HibernateDaoSupport" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <import resource="config/spring/user.xml"/> </beans>
7. config/spring/user.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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userDao" class="com.bjsxt.dao.impl.UserDaoImpl" parent="hibernateDaoSupport"></bean> <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userAction" class="com.bjsxt.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> </beans>
8.配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <include file="config/struts/user.xml"></include> </struts>
9.config/struts/user.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="user" namespace="/user" extends="struts-default"> <!-- 整合spring之後action的class屬性能夠寫bean的id --> <action name="list" class="userAction" method="list"> <result>/list.jsp</result> </action> </package> </struts>
10.文件結構