Spring,Struts2.0,Ibatis框架整合_3
到配置文件部分了,这个是把所有dao,service,action等都分开来写,applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?><beans default-autowire="byType" 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 以下为 所有配置action,service,dao 分开写文件引入,也可以在web.xml中引入 --><import resource="actionContext.xml"/><import resource="daoContext.xml"/><import resource="dataAccessContext.xml"/><import resource="serviceContext.xml"/></beans>
<?xml version="1.0" encoding="UTF-8"?><beans default-autowire="byType" 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 以下为将所有配置action,service,dao集中写在一起的情况 --><bean id="dataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/zhiming"/><property name="username" value="root"/><property name="password" value="123456"/></bean><bean id="sqlMapClient" scope="prototype"><property name="userService" ref="userService" /></bean><bean id="userService"ref="userDao"></property></bean><bean id="userDao" ref="sqlMapClient"/></bean></beans>
<?xml version="1.0" encoding="UTF-8"?><beans default-autowire="byType" 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- Struts2 action类注入 --><bean id="userAction" scope="prototype"><property name="userService" ref="userService" /></bean></beans>
<?xml version="1.0" encoding="UTF-8"?><beans default-autowire="byType" 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- IBATIS的DAO的配置注入 --><bean id="userDao" ref="sqlMapClient"/></bean></beans>
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 读取 jdbc.properties --><bean id="propertyConfigurer"destroy-method="close"><property name="driverClassName" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="poolPreparedStatements" value="true" /></bean> <!-- 配置sqlMapClient --><bean id="sqlMapClient" name="code"><?xml version="1.0" encoding="UTF-8"?><beans default-autowire="byType" 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 系统业务逻辑层的注入 --><bean id="userService"ref="userDao"></property></bean></beans>jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/zhimingjdbc.username=rootjdbc.password=123456#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver#jdbc.url=jdbc:oracle:thin:@172.16.24.85:1521:gdyp#jdbc.username=it#jdbc.password=it#datasource.name=jdbc/ci
struts.devMode=truestruts.enable.DynamicMethodInvocation=falsestruts.objectTypeDeterminer=notigerstruts.objectFactory=springstruts.locale=zh_CNstruts.i18n.encoding=UTF-8
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <!-- 这里的type对应spring配置文件的<beans>元素中的default-autowire属性,若不写则默认 --><constant name="struts.objectFactory.spring.autoWire" value="type" /><constant name="struts.objectFactory" value="spring" /><include file="struts-default.xml" /><!-- 在这里可以include多个struts的配置文件 --><package name="ling" extends="struts-default" namespace="/"><default-interceptor-ref name="paramsPrepareParamsStack" /><action name="findAllUsers" method="findAllUsers"><result name="success">/jsp/user.jsp</result></action></package></struts>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"><sqlMapConfig> <sqlMap resource="com/sqlmap/User_sqlMap.xml"/> </sqlMapConfig>
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><!-- 配置Spring --><context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/applicationContext.xml</param-value><!-- 也可以在这里引入多个spring xml配置文件 --> <!-- <param-value>WEB-INF/applicationContext.xml,WEB-INF/dataAccessContext.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.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- dwr servlet--><servlet><servlet-name>dwr-invoker</servlet-name><servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class><init-param><param-name>debug</param-name><param-value>true</param-value></init-param><init-param><param-name>logLevel</param-name><param-value>FATAL</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dwr-invoker</servlet-name><url-pattern>/dwr/*</url-pattern></servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd"><dwr><allow><convert match="com.vo.User" converter="bean"></convert><create javascript="jsUser" creator="spring" scope="request"><param name="beanName" value="userService"></param><include method="findAllUsers" /></create></allow></dwr>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>用户列表</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> <table value="%{#session.userList}">--%><%-- <s:iterator id="user" value="#session.userList">--%> <s:iterator id="user" value="#request.userList"> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.sex}</td> <td>${user.age}</td> <td>${user.address}</td> <td>${user.phone}</td> <td>${user.email}</td> </tr></s:iterator> </table> </body><%request.removeAttribute("userList");%><%--<%session.removeAttribute("userList");%>--%></html>