设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 12784|回复: 1
打印 上一主题 下一主题

Spring+Webwork+iBatis 组合实例(转)

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-29 02:20:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一、/WEB-INF/web.xml      (配置WebWork + Spring)
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<filter>
      <filter-name>webwork</filter-name>
      <filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
      <filter-name>webwork</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
二、/WEB-INF/applicationContext.xml (配置Spring + iBatis)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-autowire="autodetect">

<!-- 读取Mysql驱动信息参数文件      -->
<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
       <list>
        <value>WEB-INF/jdbc.properties</value>
       </list>
      </property>
</bean>
<!-- C3P0 数据源      -->
<bean id="dataSource"
      class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">
      <property name="driverClass" value="${jdbc.driverClassName}" />
      <property name="jdbcUrl" value="${jdbc.url}" />
      <property name="user" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlMapClient"
      class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
      <property name="configLocation"
       value="WEB-INF/sql-map-config.xml" />
      <property name="dataSource" ref="dataSource" />
</bean>
<bean id="userservices" class="userServices"/>   
<bean id="mymysqldriver" class="MyMysqlDriver"/>  

<!-- 在personservices 对象中注入 sqlMapClient -->
<bean id="personservices" class="PersonServices">
  <property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
</beans>
三、/WEB-INF/jdbc.properties (配置 iBatis 的数据库驱动(MySql驱动))
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql1
jdbc.username=root
jdbc.password=1234
四、/classes/xwork.xml (配置 WebWork )
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<!-- START SNIPPET: xworkSample -->
<xwork>
<include file="webwork-default.xml"/>
<include file="xwork-Global.xml"/>
<include file="xwork-PersonAction.xml"/>
</xwork>
五、/classes/xwork-Global.xml (配置 WebWork )
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<package name="global" extends="webwork-default">
      <global-results>
       <result name="error" type="redirect">/isnull.jsp</result>
      </global-results>
</package>
</xwork>
六、/classes/xwork-PersonAction.xml (配置 WebWork )
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
      <package name="ppp" extends="global">
       <action name="personaction" class="PersonAction">
        <result name="success" type="dispatcher">/Person.jsp</result>
        <interceptor-ref name="params"/>
       </action>
      </package>
</xwork>
七、/WEB-INF/sql-map-config.xml (配置 iBatis)
<?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>
        <settings
            cacheModelsEnabled="true"
            enhancementEnabled="true"
            lazyLoadingEnabled="true"
            maxRequests="32"
            maxSessions="10"
            maxTransactions="5"
            useStatementNamespaces="false"
        />
        <sqlMap resource="/PersonDataEntity.xml" />
</sqlMapConfig>
八、/classes/PersonDataEntity.xml (配置      iBatis      说白了就是写Sql)  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
      "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Person">
<select id="selectPerson" resultClass="fizi.PersonDataEntity">
      SELECT PER_ID as id,
      PER_FIRST_NAME as firstName,
      PER_LAST_NAME as lastName,
      PER_BIRTH_DATE as birthDate,
      PER_WEIGHT_KG as weightInKilograms,
      PER_HEIGHT_M as heightInMeters
      FROM PERSON
      WHERE PER_ID = #value#
</select>
<insert id="insertPerson" parameterClass="fizi.PersonDataEntity">
      <![CDATA[
       INSERT INTO
       PERSON (PER_ID, PER_FIRST_NAME, PER_LAST_NAME,
       PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M)
       VALUES (#id#, #firstName#, #lastName#,
                 #birthDate#, #weightInKilograms#, #heightInMeters#)
      ]]>
</insert>
<update id="updatePerson" parameterClass="fizi.PersonDataEntity">
      <![CDATA[
       UPDATE PERSON
       SET PER_FIRST_NAME = #firstName#,
       PER_LAST_NAME = #lastName#, PER_BIRTH_DATE = #birthDate#,
       PER_WEIGHT_KG = #weightInKilograms#,
       PER_HEIGHT_M = #heightInMeters#
       WHERE PER_ID = #id#
      ]]>
</update>
<delete id="deletePerson" parameterClass="fizi.PersonDataEntity">
      <![CDATA[
       DELETE PERSON
       WHERE PER_ID = #id#
      ]]>
</delete>     
</sqlMap>
九、/classes/webwork.properties (配置      WebWork )
### extension for actions  这里有点意思啊!
webwork.action.extension=action,java,aspx,jhtml  
### Configuration reloading
### This will cause the configuration to reload xwork.xml when it is changed
webwork.i18n.reload=true
webwork.configuration.xml.reload=true
### Load custom default resource bundles
webwork.custom.i18n.resources=default
### character encoding
### webwork.locale=zh_CN
webwork.i18n.encoding=UTF-8
### upload jakarta, temp dir, maxSize = 10M
webwork.multipart.parser=jakarta
### webwork.ui.theme=xhtml
webwork.ui.theme=simple
### WebWork-Spring Integration
webwork.objectFactory = spring
webwork.objectFactory.spring.autoWire = name
十、部分学习代码 PersonDataEntity.java
import java.util.Date;
public class PersonDataEntity {
private int id;
private String firstName;
private String lastName;
private Date birthDate;
private double weightInKilograms;
private double heightInMeters;
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
public Date getBirthDate() {
      return birthDate;
}
public void setBirthDate(Date birthDate) {
      this.birthDate = birthDate;
}
public String getFirstName() {
      return firstName;
}
public void setFirstName(String firstName) {
      this.firstName = firstName;
}
public double getHeightInMeters() {
      return heightInMeters;
}
public void setHeightInMeters(double heightInMeters) {
      this.heightInMeters = heightInMeters;
}
public String getLastName() {
      return lastName;
}
public void setLastName(String lastName) {
      this.lastName = lastName;
}
public double getWeightInKilograms() {
      return weightInKilograms;
}
public void setWeightInKilograms(double weightInKilograms) {
      this.weightInKilograms = weightInKilograms;
}
}
十一、PersonServices.java (要使用iBatis 则需要 继承 类 SqlMapClientDaoSupport )
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.ibatis.sqlmap.client.SqlMapClient;
public class PersonServices extends SqlMapClientDaoSupport {
public PersonDataEntity GetPerson(Integer pID) {
      return (PersonDataEntity) getSqlMapClientTemplate()
        .queryForObject("selectPerson", pID);
}
}
十二、PersonAction.java 在WebWork中方便调用的Action所以需要实现 接口Action
import Socket.sclient;
import com.opensymphony.xwork.Action;
public class PersonAction implements Action{
private PersonDataEntity persondataentity = new PersonDataEntity();
private PersonServices personservices;
public String execute() throws Exception {
      // TODO Auto-generated method stub
      Integer pid = new Integer(100001);
      sclient client = new sclient();       //调用Socket,这里你可以不需要(我写的:)
      client.ReadServer();
      persondataentity = personservices.GetPerson(pid);
      if (persondataentity!=null)
      {
       return SUCCESS;
      }
      else
      {
       return ERROR;
      }
}
public void setPersondataentity(PersonDataEntity persondataentity) {
      this.persondataentity = persondataentity;
}
public void setPersonservices(PersonServices personservices) {
      this.personservices = personservices;
}
public PersonDataEntity getPersondataentity() {
      return persondataentity;
}
}
十三、实际调用 PersonAction的 jsp文件 QueryPerson.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Person查询</title>
</head>
<body>
<table border=0 width=97%>
      <tr><td align="left">
       <form name="personaction" action="personaction.action" method="post">
       //想开个玩笑的话也可以写成,嘿嘿,原因参见文件:webwork.properties
       <form name="personaction" action="personaction.aspx" method="post">
        PersonID:&nbsp;<input type="text" name="persondataentity.id"><br>
        <input type="submit" name="查询"><br>
       </form>
      </td></tr>
</table>
</body>
</html>
十四、做个简单的查询结果返回页面 person.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/webwork" prefix="ww" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
人员:<label style="color: red;" ><ww:property value='persondataentity.firstName'/></label>查询成功!
</body>
</html>
您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-18 23:40 , Processed in 0.101208 second(s), 10 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表