SSI整合--搭建Struts2+Spring+Ibatis框架
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app 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_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-config.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> </web-app>
spring-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--如非必要, 请不要改动这个文件, 扩充spring的配置请新建spring-context*.xml--> <!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句--> <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:spring-jdbc.properties</value> </list> </property> </bean> <!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${jdbc.driver}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <!--根据dataSource和configLocation创建一个SqlMapClient--> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="sqlMapClientProperties"> <props> <prop key="jdbcDriver">${jdbc.driver}</prop> </props> </property> <property name="configLocation"> <value>classpath:SqlMapConfig.xml</value> </property> </bean> <!--根据sqlMapClien创建一个SqlMapClient模版类--> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> <!--DAO--> <bean id="addressDAO" class="cn.hhit.gwap.dao.ibatis.AddressDAOIbatis"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> <bean id="bookDAO" class="cn.hhit.gwap.dao.ibatis.BookDAOIbatis"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> <bean id="categoryDAO" class="cn.hhit.gwap.dao.ibatis.CategoryDAOIbatis"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> <bean id="itemDAO" class="cn.hhit.gwap.dao.ibatis.ItemDAOIbatis"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> <bean id="orderDAO" class="cn.hhit.gwap.dao.ibatis.OrderDAOIbatis"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> <bean id="productDAO" class="cn.hhit.gwap.dao.ibatis.ProductDAOIbatis"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> <bean id="userDAO" class="cn.hhit.gwap.dao.ibatis.UserDAOIbatis"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> <!--Action--> <!--User部分--> <bean id="checkEmailAction" class="cn.hhit.gwap.action.user.CheckEmailAction" scope="prototype"> <property name="userDAO"> <ref bean="userDAO"/> </property> </bean> <bean id="loginAction" class="cn.hhit.gwap.action.user.LoginAction" scope="prototype"> <property name="userDAO"> <ref bean="userDAO"/> </property> </bean> <bean id="registAction" class="cn.hhit.gwap.action.user.RegistAction" scope="prototype"> <property name="userDAO"> <ref bean="userDAO"/> </property> </bean> <bean id="imageAction" class="cn.hhit.gwap.action.user.ImageAction" scope="prototype"></bean> <bean id="varifyAction" class="cn.hhit.gwap.action.user.VarifyAction" scope="prototype"> <property name="userDAO"> <ref bean="userDAO"/> </property> </bean> <!--main部分--> <bean id="bookDetailAction" class="cn.hhit.gwap.action.main.BookDetailAction" scope="prototype"> <property name="productDAO"> <ref bean="productDAO"/> </property> <property name="bookDAO"> <ref bean="bookDAO"/> </property> </bean> <bean id="bookListAction" class="cn.hhit.gwap.action.main.BookListAction" scope="prototype"> <property name="categoryDAO"> <ref bean="categoryDAO"/> </property> </bean> <bean id="categoryAction" class="cn.hhit.gwap.action.main.CategoryAction" scope="prototype"> <property name="categoryDAO"> <ref bean="categoryDAO"/> </property> </bean> <bean id="logoutAction" class="cn.hhit.gwap.action.main.LogoutAction" scope="prototype"></bean> <bean id="shoppingAction" class="cn.hhit.gwap.action.main.ShoppingAction" scope="prototype"> <property name="productDAO"> <ref bean="productDAO"/> </property> </bean> <!--order部分--> <bean id="addrAction" class="cn.hhit.gwap.action.order.AddrAction" scope="prototype"> <property name="addressDAO"> <ref bean="addressDAO"/> </property> <property name="itemDAO"> <ref bean="itemDAO"/> </property> <property name="orderDAO"> <ref bean="orderDAO"/> </property> </bean> <bean id="orderAction" class="cn.hhit.gwap.action.order.OrderAction" scope="prototype"> <property name="addressDAO"> <ref bean="addressDAO"/> </property> </bean> <bean id="orderItemViewAction" class="cn.hhit.gwap.action.order.OrderItemViewAction" scope="prototype"> <property name="itemDAO"> <ref bean="itemDAO"/> </property> </bean> <bean id="orderViewAction" class="cn.hhit.gwap.action.order.OrderViewAction" scope="prototype"> <property name="orderDAO"> <ref bean="orderDAO"/> </property> </bean> <!--cart部分--> <bean id="showCartAction" class="cn.hhit.gwap.action.cart.ShowCartAction" scope="prototype"></bean> </beans>
spring-jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/gwap jdbc.username=root jdbc.password=123456
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> <include file="struts-main.xml" /> <include file="struts-user.xml" /> <include file="struts-order.xml" /> <include file="struts-cart.xml" /> <!--用到json必须继承"json-default"--> <package name="gwap-default" extends="json-default"> <global-results> <result>/WEB-INF/jsp/fail.jsp</result> </global-results> </package> </struts>
struts-main.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="dang-main" namespace="/main" extends="gwap-default"> <action name="index"> <result>/WEB-INF/jsp/main/main.jsp</result> </action> <action name="login"> <result type="redirectAction"> <param name="actionName">index</param> <param name="namespace">/user</param> </result> </action> <action name="logout" class="logoutAction"> <result name="success">/WEB-INF/jsp/main/main.jsp</result> <result name="next" type="json"></result> </action> <action name="detail" class="bookDetailAction"> <result name="success">/WEB-INF/jsp/main/book_detail.jsp</result> </action> <action name="category" class="categoryAction"> <result name="success">/WEB-INF/jsp/main/category.jsp</result> </action> <action name="booklist" class="bookListAction"> <param name="size">5</param> <result name="success">/WEB-INF/jsp/main/book_list.jsp</result> </action> <action name="shopping" class="shoppingAction"> <result type="json"></result> </action> </package> </struts>