最近由于项目的需要一直忙于回顾j2ee的相关内容,因为做安卓时间已久,之前会的那点j2ee的知识早就忘了个精光,好在基础打的牢现在回顾起来还不算很困难。下面就来总结一下webservices常见的开发框架搭建流程: 开发环境MyEclipse 8.5
这里介绍基于xfrie1.2.6实现的webservices,其中整合了sping2.0 、hibernate3.0和struts1.1等流行框架。熟悉xfire的程序员都知道xfire和sping的整合经常用的有两种结合方式,下面会有具体的介绍。这里先说说基本框架的搭建吧直接上图了
1、新建项目 选择 web service project
2、选则Next
以上就是搭建xfire 、spring和hibernate,添加struts的配置包也是用的MyEclipse自带的,很简单,一路next 这里就不在给图了。这样搭建完成以后你会发现项目lib文件夹下有些相同的jar包会有多个版本,这样你只需要保留一个最新版本号就可以了。因为相同的jar包多个版本同时存在的话容易引起包冲突。这里不建议用这种方法来搭建开发环境,如果自己以前有SSH方面的项目,还是直接把里面的jar包拿来用吧,MyEclipse 自带的方式添加很容易引起不必要的麻烦。
2、以上我们已经成功搭建了SSH+Xfire的开发环境,只是有些配置文件可能需要我们在开发的过程中来完善。接下来我们如果需要proxool连接池的话还需要导入两个包文件proxool-0.8.3.jar、proxool-cglib.jar 。
这是整个框架搭建完成后的样子。
3、具体的实现类
1)、LoginAction.java 代码如下
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
return super.execute(mapping, form, request, response);
}
}
2)、LoginActionForm.java 代码如下:
/**
* @author Superfy
*
*/
public class LoginActionForm extends ActionForm {
String username;
String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3)、ILoginDAO.java 开发的访问接口
public interface ILoginDAO {
public boolean login(String username, String userpwd);
}
LoginDAOImpl.java 接口的具体实现
public class LoginDAOImpl extends HibernateDaoSupport implements ILoginDAO {
public boolean login(String username, String userpwd) {
boolean flag = false;
String hql = "FROM User AS u WHERE u.username=? AND u.userpwd=?";
Query q = super.getSession().createQuery(hql);
q.setString(0, username);
q.setString(1, userpwd);
List all = q.list();
if (all.size() > 0) {
flag = true;
//SysUsers t = (SysUsers) all.get(0);
}
return flag;
}
}
4)、User.java 定义了三个成员变量,get,set方法自己生成吧这里不在贴了。
private Integer id;
private String userpwd;
private String username;
User.hbm.xml Hibernate的映射文件
<hibernate-mapping>
<class name="com.easylife.model.User" table="user" catalog="easylife">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="userpwd" type="java.lang.String">
<column name="userpwd" length="20" />
</property>
<property name="username" type="java.lang.String">
<column name="username" length="20" />
</property>
</class>
</hibernate-mapping>
下面看一下几个配置文件的写法:
applicationContext.xml
<beans>
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!--
spring 主要来完成三件事 :
1、利用数据库连接池来配置数据源。
2、声明Hibernate的事物,并把事物注入到逻辑对象
3、如何集成sturts,要将逻辑对象注入到action中
-->
<bean name="/login" class="com.easylife.action.LoginAction"></bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="org.logicalcobwebs.proxool.ProxoolDriver" />
<property name="url" value="proxool.dbs" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<!--加载tomcat的时候进行自动创建表,不加默认不会创建-->
<!--<prop key="hibernate.hbm2ddl.auto">create</prop>
-->
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.connection.release_mode">after_statement</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/easylife/model/User.hbm.xml</value></list>
</property>
</bean>
<bean id="LoginDAOImpl" class="com.easylife.dao.LoginDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler" />
<bean name="ILoginDAO" class="org.codehaus.xfire.spring.ServiceBean">
<property name="serviceBean" ref="LoginDAOImpl" />
<property name="serviceClass" value="com.easylife.dao.ILoginDAO" />
<property name="inHandlers">
<list>
<ref bean="addressingHandler" />
</list>
</property>
</bean>
<!-- end -->
</beans>
proxool.xml 文件的配置 ,详细的标签含义这里不在给出,不清楚的自己google吧
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>dbs</alias>
<driver-url>jdbc:mysql://localhost:3306/easylife</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="admin"/>
<property name="useUnicode" value="true" />
<property name="characterEncoding" value="UTF-8" />
</driver-properties>
<maximum-connection-count>100</maximum-connection-count>
<minimum-connection-count>10</minimum-connection-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>10</maximum-new-connections>
<prototype-count>5</prototype-count>
<test-before-use>true</test-before-use>
<house-keeping-test-sql>select 1 from dual</house-keeping-test-sql>
</proxool>
</something-else-entirely>
紧接着 struts-config.xml 文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.easylife.actionform.LoginActionForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/login" forward="/successful.jsp" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy" name="loginForm"></action>
</action-mappings>
<message-resources parameter="com.ekfq.struts.ApplicationResources" />
</struts-config>
最后也是最容易出错的地方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" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--连接池proxool服务配置 start-->
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>/WEB-INF/classes/proxool.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--观察连接池proxool 管理信息,可以不用加载-->
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
<!--连接池proxool服务配置 end-->
<!--spring服务配置 start-->
<servlet>
<servlet-name>contextConfigLocation</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!--spring服务配置 end-->
<!--初始化系统配置文件 start-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!--初始化系统配置文件 end-->
<!--struts 的配置 start-->
<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>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--struts 的配置 end-->
<!--xfire 和 sping 的结合 -->
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!--xfire 和 sping 的结合 end-->
</web-app>
看一下运行结果:
到此 SSH+xfire+proxool的开发框架已基本搭建完成,当然了这只是spring和xfire结合方式中的一种配置,下一篇我会介绍另外一种配置的实现方式,敬请期待吧!!!
第一次发帖,有不对的地方还望指出!!!