本例参照这篇Struts iBATIS Spring文,基于JDK1.5,Tomcat5.5.25,mysql-5.0.37-win32,以及Spring2.0.8,Ibatis-2.3.4.726和相应的struts,JSTL,Standard包搭建,基本思路如下:
首先建立一个基本的,可运行的框架。在可运行前提下,通用类包考虑使用最新版本包。不能确定某类包版本的情况下,选择一个已知版本的相关类包,以该类包自带的对应类包的版本为准。
逐步增加核心的功能模块,调试可运行,再考虑增加。重复此步骤增加更多的包,抽取更多的功能增加,调试使可运行,直到完成可运行实例。
1 类包及简介
commons-beanutils-1.8.3.jar :为Java的反射和内省API提供了易用的包装接口
commons-digester-1.8.1.jar :XML和Java对象的映射工具
commons-logging-1.1.1.jar :提供各种日志接口实现包的包装接口
log4j-1.2.16.jar :一种应用程序操作日志服务组件
commons-collections-3.2.1.jar:扩展了Java的Collections框架
commons-dbcp-1.3.jar:数据库连接池服务
commons-pool-1.5.4.jar:普通对象池组件
jstl.jar:JSP标准标签库规范的一个实现包
standard.jar :JSP标准标签库规范的一个实现包
ibatis-2.3.4.726.jar:对象和关系数据库的数据映射框架,依赖于对象,XML和SQL语句(或存储过程)
spring2.0.8.jar :以轻量级,可配置方式,提供了建立企业级Java,Rich Web应用和企业集成应用功能的开源框架
struts.jar:用来建立基于MVC架构的Web应用的开源框架,提供了请求和URI的映射处理,响应导向处理和页面标签库功能
mysql-connector-java-5.1.6-bin.jar:mysql数据库驱动
2 框架组合的配置
2.1 Struts的配置
web.xml中增加:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.2 Spring和Struts的组合配置
struts-config.xml中增加:
<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property
property=”contextConfigLocation”
value=”/WEB-INF/classes/spring.xml”/>
</plug-in>
其中的action配置形如:
<action
path=”/employeeProcess”
name=”employeeForm”
type=”org.springframework.web.struts.DelegatingActionProxy”
scope=”request”
parameter=”dispatch”>
<forward name=”failure” path=”/employeeForm.jsp”/>
<forward name=”success” path=”/employees.jsp”/>
</action>
action在Spring.xml中形如:
<bean name=”/employeeSetUp” class=”net.reumann.demo.action.EmployeeAction”>
<constructor-arg index=”0″ ref=”employeeService”/>
<constructor-arg index=”1″ ref=”departmentService”/>
</bean>
2.3 Spring和Ibatis的组合配置
2.3.1 spring.xml中增加
<bean id=”sqlMapClient”
class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”>
<property name=”configLocation”>
<value>classpath:net/reumann/demo/persistence/SqlMapConfig.xml</value>
</property>
<property name=”useTransactionAwareDataSource”>
<value>true</value>
</property>
<property name=”dataSource”>
<ref bean=”dataSource”/>
</property>
</bean>
<bean id=”sqlMapClientTemplate”
class=”org.springframework.orm.ibatis.SqlMapClientTemplate”>
<property name=”sqlMapClient”>
<ref bean=”sqlMapClient”/>
</property>
</bean>
其中的bean调用形如:
<bean id=”employeeDao” class=”net.reumann.demo.persistence.EmployeeIbatisDao”>
<property name=”sqlMapClient”>
<ref bean=”sqlMapClient”/>
</property>
</bean>
DAO类的实现继承SqlMapClientTemplate类,形如:
public class DepartmentIbatisDao extends SqlMapClientTemplate implements DepartmentDao {
public List getAllDepartments() {
return queryForList(“Department.getAll”, null);
}
}
2.3.2 Spring中数据源的配置
spring.xml中增加
<bean id=”propertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<property name=”location” value=”classpath:spring.properties”/>
</bean>
<bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>
<property name=”driverClassName” value=”${driverClassName}”/>
<property name=”url” value=”${url}”/>
<property name=”username” value=”${username}”/>
<property name=”password” value=”${password}”/>
</bean>
spring.properties中配置形如:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/xdb1?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&autoReconnectForPools=true
username=root
password=root
2.4 JSTL的配置
需要和Spring2.0.8匹配的jstl.jar和standard.jar两个包,在JSP页面顶部增加:
<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<%@ taglib prefix=”fmt” uri=”http://java.sun.com/jsp/jstl/fmt” %>
2.5 国际化资源包配置
web.xml中增加:
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>MessageResources</param-value>
</context-param>
在源码根目录(src)下增加MessageResources_en.properties,和本地的MessageResources_zh.properties,所有资源包都统一转换为UTF-8编码。
在相应的JSP页面完成2.4步骤前提下,增加<fmt:message key=”application.title”/>即可显示资源包中的相应国际化键值。
在系统无法确认选择何资源包情况下,默认显示MessageResources.properties中的键值,否则根据操作系统的默认编码选择合适的资源包显示。
另外,需要注意数据库,应用服务器以及相关程序,页面和配置文件的UTF-8编码的统一。
原创文章如转载,请注明:转载自瑞阳日志 本文链接地址: http://johncao.info/?p=387
本文来自: 瑞阳日志(http://johncao.info) 原文地址:http://johncao.info/?p=387
首先建立一个基本的,可运行的框架。在可运行前提下,通用类包考虑使用最新版本包。不能确定某类包版本的情况下,选择一个已知版本的相关类包,以该类包自带的对应类包的版本为准。
逐步增加核心的功能模块,调试可运行,再考虑增加。重复此步骤增加更多的包,抽取更多的功能增加,调试使可运行,直到完成可运行实例。
1 类包及简介
commons-beanutils-1.8.3.jar :为Java的反射和内省API提供了易用的包装接口
commons-digester-1.8.1.jar :XML和Java对象的映射工具
commons-logging-1.1.1.jar :提供各种日志接口实现包的包装接口
log4j-1.2.16.jar :一种应用程序操作日志服务组件
commons-collections-3.2.1.jar:扩展了Java的Collections框架
commons-dbcp-1.3.jar:数据库连接池服务
commons-pool-1.5.4.jar:普通对象池组件
jstl.jar:JSP标准标签库规范的一个实现包
standard.jar :JSP标准标签库规范的一个实现包
ibatis-2.3.4.726.jar:对象和关系数据库的数据映射框架,依赖于对象,XML和SQL语句(或存储过程)
spring2.0.8.jar :以轻量级,可配置方式,提供了建立企业级Java,Rich Web应用和企业集成应用功能的开源框架
struts.jar:用来建立基于MVC架构的Web应用的开源框架,提供了请求和URI的映射处理,响应导向处理和页面标签库功能
mysql-connector-java-5.1.6-bin.jar:mysql数据库驱动
2 框架组合的配置
2.1 Struts的配置
web.xml中增加:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.2 Spring和Struts的组合配置
struts-config.xml中增加:
<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property
property=”contextConfigLocation”
value=”/WEB-INF/classes/spring.xml”/>
</plug-in>
其中的action配置形如:
<action
path=”/employeeProcess”
name=”employeeForm”
type=”org.springframework.web.struts.DelegatingActionProxy”
scope=”request”
parameter=”dispatch”>
<forward name=”failure” path=”/employeeForm.jsp”/>
<forward name=”success” path=”/employees.jsp”/>
</action>
action在Spring.xml中形如:
<bean name=”/employeeSetUp” class=”net.reumann.demo.action.EmployeeAction”>
<constructor-arg index=”0″ ref=”employeeService”/>
<constructor-arg index=”1″ ref=”departmentService”/>
</bean>
2.3 Spring和Ibatis的组合配置
2.3.1 spring.xml中增加
<bean id=”sqlMapClient”
class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”>
<property name=”configLocation”>
<value>classpath:net/reumann/demo/persistence/SqlMapConfig.xml</value>
</property>
<property name=”useTransactionAwareDataSource”>
<value>true</value>
</property>
<property name=”dataSource”>
<ref bean=”dataSource”/>
</property>
</bean>
<bean id=”sqlMapClientTemplate”
class=”org.springframework.orm.ibatis.SqlMapClientTemplate”>
<property name=”sqlMapClient”>
<ref bean=”sqlMapClient”/>
</property>
</bean>
其中的bean调用形如:
<bean id=”employeeDao” class=”net.reumann.demo.persistence.EmployeeIbatisDao”>
<property name=”sqlMapClient”>
<ref bean=”sqlMapClient”/>
</property>
</bean>
DAO类的实现继承SqlMapClientTemplate类,形如:
public class DepartmentIbatisDao extends SqlMapClientTemplate implements DepartmentDao {
public List getAllDepartments() {
return queryForList(“Department.getAll”, null);
}
}
2.3.2 Spring中数据源的配置
spring.xml中增加
<bean id=”propertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<property name=”location” value=”classpath:spring.properties”/>
</bean>
<bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>
<property name=”driverClassName” value=”${driverClassName}”/>
<property name=”url” value=”${url}”/>
<property name=”username” value=”${username}”/>
<property name=”password” value=”${password}”/>
</bean>
spring.properties中配置形如:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/xdb1?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&autoReconnectForPools=true
username=root
password=root
2.4 JSTL的配置
需要和Spring2.0.8匹配的jstl.jar和standard.jar两个包,在JSP页面顶部增加:
<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<%@ taglib prefix=”fmt” uri=”http://java.sun.com/jsp/jstl/fmt” %>
2.5 国际化资源包配置
web.xml中增加:
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>MessageResources</param-value>
</context-param>
在源码根目录(src)下增加MessageResources_en.properties,和本地的MessageResources_zh.properties,所有资源包都统一转换为UTF-8编码。
在相应的JSP页面完成2.4步骤前提下,增加<fmt:message key=”application.title”/>即可显示资源包中的相应国际化键值。
在系统无法确认选择何资源包情况下,默认显示MessageResources.properties中的键值,否则根据操作系统的默认编码选择合适的资源包显示。
另外,需要注意数据库,应用服务器以及相关程序,页面和配置文件的UTF-8编码的统一。
原创文章如转载,请注明:转载自瑞阳日志 本文链接地址: http://johncao.info/?p=387
本文来自: 瑞阳日志(http://johncao.info) 原文地址:http://johncao.info/?p=387