论坛首页 入门技术论坛

Spring,Struts2.0,Ibatis框架整合_3

浏览 2168 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-28   最后修改:2010-01-28
到配置文件部分了,这个是把所有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>



如果是全部写在一起的话,actionContext.xml,daoContext.xml,dataAccessContext.xml,serviceContext.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集中写在一起的情况  -->

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource">
			<ref local="dataSource"/>
		</property>
		<property name="configLocation">
			<value>WEB-INF/sqlMapConfig.xml</value>
		</property>
	</bean>

	<bean id="userAction" class="com.action.UserAction" scope="prototype">
		<property name="userService" ref="userService" />
	</bean>

	<bean id="userService"
		class="com.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>

	<bean id="userDao" class="com.dao.impl.UserDao">
		<property name="sqlMapClient" ref="sqlMapClient"/>
	</bean>	

</beans>



applicationContext.xml所import进来的xml文件的文件头那些都要和applicationContext.xml的文件头一样,actionContext.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">

	<!-- Struts2 action类注入 -->
	<bean id="userAction" class="com.action.UserAction" scope="prototype">
		<property name="userService" ref="userService" />
	</bean>
	
</beans>


daoContext.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">

<!-- IBATIS的DAO的配置注入 -->	
	<bean id="userDao" class="com.dao.impl.UserDao">
		<property name="sqlMapClient" ref="sqlMapClient"/>
	</bean>	
	
</beans>


dataAccessContext.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"
	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"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>WEB-INF/jdbc.properties</value>
			</list>
		</property>
	</bean>

	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource">
			<ref local="dataSource"/>
		</property>
		<property name="configLocation">
			<value>WEB-INF/sqlMapConfig.xml</value>
		</property>
	</bean>
</beans>



serviceContext.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">

	<!-- 系统业务逻辑层的注入 -->
	<bean id="userService"
		class="com.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>
</beans>


数据库配置文件,jdbc.properties部分:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/zhiming
jdbc.username=root
jdbc.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


struts2配置文件一,struts.properties部分:
struts.devMode=true
struts.enable.DynamicMethodInvocation=false
struts.objectTypeDeterminer=notiger
struts.objectFactory=spring
struts.locale=zh_CN
struts.i18n.encoding=UTF-8


struts2配置文件二,struts.xml部分:
<?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" class="userAction" method="findAllUsers">
			<result name="success">/jsp/user.jsp</result>
		</action>

	</package>
		
</struts>



Ibatis配置文件部分,sqlMapConfig.xml:
<?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>


web.xml部分:
<?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>



dwr.xml配置部分,其作用后面再讲:
<?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>



最后是jsp页面部分:
<%@ 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 class="table_report">
	<tr> 
		<th>用户ID</th>
		<th>用户名称</th>
		<th>用户性别</th>
		<th>用户年龄</th>
		<th>用户地址</th>
		<th>用户电话</th>
		<th>用户邮箱</th>
    </tr>
    <!-- struts2最正规的取值方式 -->
<%--  <s:iterator id="user" 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>



访问地址:
http://localhost:8080/ZhiMing/findAllUsers.action



论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics