当前位置: 代码迷 >> Eclipse >> Myeclipse上开发ssh项目流程和注意事项
  详细解决方案

Myeclipse上开发ssh项目流程和注意事项

热度:84   发布时间:2016-04-23 12:06:25.0
Myeclipse下开发ssh项目流程和注意事项

大体的我就不说了 因为现在没开发就是看我写的代码来讲,所以没有截图了

1 再认真读懂项目需求后,新建和web项目
2 导入 struts 选用最新的版本。
3 导入spring 在选择hibernate的jar时选择3.0 不要用3.1或更高,经过实践证明那样会在后面使用hibernate时带来不变。
4.导入hibernate 同时也选择3.0,其实是因为spring中的hibernate的jar没有更新好,所以都最好选择3.0。当然以后等spring和hibernate都更新了咱们当然用最新的了,前题是版本一样

现在一个一个说:
struts的struts-config.xml
在选择action和form时我的选择是这样的
form:org.apache.struts.validator.DynaValidatorForm
action在xml看不到在java文件的action中extends:MappingDispatchAction
写这个的目的:jsp跳到,spring启动,validate验证
下面是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>
<data-sources />
<form-beans >
<form-bean name="wordbookForm" type="org.apache.struts.validator.DynaValidatorForm">
??? <form-property name="wid" type="java.lang.String"/>
??? <form-property name="wtype" type="java.lang.String"/>
??? <form-property name="wname" type="java.lang.String"/>
??? <form-property name="wvalue" type="java.lang.String"/>
??? <form-property name="wremark" type="java.lang.String"/>
??? <form-property name="wdate" type="java.lang.String"/>
??? <form-property name="wprd" type="java.lang.String"/>
??? </form-bean>
</form-beans>
<global-exceptions />
<global-forwards >
<forward name="Jump" path="/index.jsp" />
<forward name="Login" path="/Login.jsp" />
</global-forwards>
<action-mappings >
<action
????? parameter="particularManager"
????? path="/particularManager"
????? scope="request"
????? validate="false"
????? type="action.ParticularAction" />
<action
????? parameter="saveParticular"
????? path="/saveParticular"
????? scope="request"
????? validate="false"
????? type="action.ParticularAction" />
</action-mappings>
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<message-resources parameter="struts/ApplicationResources.properties" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
??? <set-property property="StopOnFristError" value="false" />
??? <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validatorSpeed.xml" />
</plug-in>
<!-- -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property value="/WEB-INF/applicationContext.xml" property="contextConfigLocation"/>
</plug-in>
??
</struts-config>
其中最后的xml有一些spring的内容,是必须记住的,还有一些是服务器端validator验证数据的内容的,有必要时可以使用,不过个人观点,用这样非常麻烦,jsp需要大量用html标记语言,validator到是写写就可以了,用这个的主要原因就是应付数据的正确,虽然ajax和js可以在客户端验证。但网络环境是复杂的,不知道到了服务器端会是什么样了,所以才有了validator,就是html标记语言写jsp实在是不如人意,所以我一直没有用,而为了数据的正确我是在action用非null验证的方法写的,虽然见证简单点,不过可以省去很多时间,还是看项目的开发时间来定。

spring和hibernate的applicationContext.xml
首先确定一点是这个xml是spring和hibernate共有的,所以不会再有什么hibernate的xml了,可是这养的xml会有2个,而且同名,一个在服务器私有路径下,作用是可以得到sessionFactory,一个在项目类下,作用和资源文件一样。
现在服务器下的:这也是主要的
内容包括了:dateSource,sessionFactory,一个daofactoryImpl,一个bussinessFactoryImpl,还有就是action的类名,方法组成的bean。
写中的目的:用依赖和注入技术给两个factory注入sessionFactory;给sessionFactory注入dateSource,同时在sessionFactory中有映射的表的xml的路径和名字;而dateSource去连接库,个人建议先用jdbc去连,再去用数据源连,原因jdbc比较好连,当连接成功,说明网络没有问题,现在再去用数据源配置,因为项目最后肯定是用数据源的,控制力好。这样工作都是为了action的类名,方法组成的bean服务,因为我们要在这里给他们注入sessionFactory,从而在action中可以轻易的调用库中的内容,当然在action你调用的已经被封住的看不到什么了。
下面是和applicationContext.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!-- -->
<bean id="DAOFactoryImpla" class="factory.DAOFactoryImpl" factory-method="newInstance" singleton="true" >
?? <property name="sessionFactory">
??? <ref bean="MySessionFactory"/>
?? </property>
</bean>

<bean id="BussinessFactoryImpla" class="factory.BussinessFactoryImpl" singleton="true" factory-method="newInstance" >
?? <property name="daoFactory">
??? <ref bean="DAOFactoryImpla"/>
?? </property>
</bean>

<bean id="dateSource"
?? class="org.springframework.jndi.JndiObjectFactoryBean">
?? <property name="jndiName">
??? <value>java:comp/env/jdbc/Speed</value>
?? </property>
</bean>

<bean id="MySessionFactory"
?? class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
?? <property name="dataSource">
??? <ref bean="dateSource" />
?? </property>
?? <property name="hibernateProperties">
??? <props>
???? <prop key="hibernate.dialect">
????? org.hibernate.dialect.MySQLDialect
???? </prop>
???? <!--
???? <prop key="hibernate.show_sql">true</prop>
??? -->
??? </props>
?? </property>
?? <property name="mappingResources">
??? <list>
???? <value>xml/TDept.hbm.xml</value>
???? <value>xml/TIdbypk.hbm.xml</value>
???? <value>xml/TLog4j.hbm.xml</value>
???? <value>xml/TParticulardroit.hbm.xml</value>
???? <value>xml/TPersonnel.hbm.xml</value>
???? <value>xml/TPersonnelassign.hbm.xml</value>
???? <value>xml/TRole.hbm.xml</value>
???? <value>xml/TRoleassign.hbm.xml</value>
???? <value>xml/TRoom.hbm.xml</value>
???? <value>xml/TWordbook.hbm.xml</value>
??? </list>
?? </property>
</bean>

<bean id="TDeptDAO" factory-method="createDept" factory-bean="DAOFactoryImpla" />
<bean id="TRoomDAO" factory-method="createRoom" factory-bean="DAOFactoryImpla"/>
<bean id="TPersonnelDAO" factory-method="createPersonnel" factory-bean="DAOFactoryImpla"/>
<bean id="TLog4jDAO" factory-method="createLog4j" factory-bean="DAOFactoryImpla"/>
<bean id="TParticulardroitDAO" factory-method="createParticulardroit" factory-bean="DAOFactoryImpla"/>
<bean id="TPersonnelassignDAO" factory-method="createPersonnelassign" factory-bean="DAOFactoryImpla"/>
<bean id="TRoleDAO" factory-method="createRole" factory-bean="DAOFactoryImpla"/>
<bean id="TRoleassignDAO" factory-method="createRoleassign" factory-bean="DAOFactoryImpla"/>
<bean id="TWordbookDAO" factory-method="createWordbook" factory-bean="DAOFactoryImpla"/>


<bean id="DeptBusinessServiceImlp" factory-method="createDeptBusinessService" factory-bean="BussinessFactoryImpla" />
<bean id="PersonnelBusinessServiceImlp" factory-method="createPersonnelBusinessService" factory-bean="BussinessFactoryImpla" />
<bean id="RoomBusinessServiceImlp" factory-method="createRoomBusinessService" factory-bean="BussinessFactoryImpla" />
<bean id="Log4jBusinessServiceImlp" factory-method="createLog4jBusinessService" factory-bean="BussinessFactoryImpla" />
<bean id="ParticularBusinessServiceImlp" factory-method="createParticularBusinessService" factory-bean="BussinessFactoryImpla" />
<bean id="WordBookBusinessServiceImlp" factory-method="createWordBookBusinessService" factory-bean="BussinessFactoryImpla" />
<bean id="RoleBusinessServiceImlp" factory-method="createRoleBusinessService" factory-bean="BussinessFactoryImpla" />

<!-- 测试使用 -->
<bean name="/Test1" class="action.Test1Action">
?? <property name="deptBusinessService">
??? <ref bean="DeptBusinessServiceImlp"/>
?? </property>
?? <property name="log4jBusinessService">
??? <ref bean="Log4jBusinessServiceImlp"/>
?? </property>
?? <property name="particularBusinessService">
??? <ref bean="ParticularBusinessServiceImlp"/>
?? </property>
?? <property name="personnelBusinessService">
??? <ref bean="PersonnelBusinessServiceImlp"/>
?? </property>
?? <property name="roleBusinessService">
??? <ref bean="RoleBusinessServiceImlp"/>
?? </property>
?? <property name="roomBusinessServic">
??? <ref bean="RoomBusinessServiceImlp"/>
?? </property>
?? <property name="wordBookBusinessService">
??? <ref bean="WordBookBusinessServiceImlp"/>
?? </property>
</bean>
<!-- 登录 -->
<bean name="/login" class="action.LoginAction">
?? <property name="personnelBusinessService">
??? <ref bean="PersonnelBusinessServiceImlp"/>
?? </property>
</bean>
<!-- 部门 -->
<bean name="/deptJump" class="action.DeptAction" >
?? <property name="deptBusinessService">
??? <ref bean="DeptBusinessServiceImlp"/>
?? </property>
</bean>
<bean name="/deptManager" class="action.DeptAction" >
?? <property name="deptBusinessService">
??? <ref bean="DeptBusinessServiceImlp"/>
?? </property>
</bean>
</beans>

用了dao的模式,看时注意。在dateSource中这是用的数据源的方式,所以服务器一定要配好。

在项目类下的applicationContext.xml:不说什么了=资源文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="BussinessFactoryImpl" class="factory.BussinessFactoryImpl" />
<bean id="DAOFactoryImpl" class="factory.DAOFactoryImpl"/>
</beans>

wab.xml:
这个没有什么可以说的:都是写死的 如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
<servlet-mapping>
??? <servlet-name>action</servlet-name>
??? <url-pattern>*.log</url-pattern>
</servlet-mapping>
<!-- -->
<context-param>
??? <param-name>contextConfigLocation</param-name>
??? <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
??? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
????? <filter-name>encodingFilter</filter-name>
????? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
????? <init-param>
????? ?? <param-name>encoding</param-name>
????? ?? <param-value>UTF-8</param-value>
????? </init-param>
</filter>
<filter-mapping>
?? <filter-name>encodingFilter</filter-name>
?? <url-pattern>*</url-pattern>
</filter-mapping>
<!-- -->
<filter>
?? <filter-name>LoginFilter</filter-name>
?? <filter-class>filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
?? <filter-name>LoginFilter</filter-name>
?? <url-pattern>/index.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
?? <filter-name>LoginFilter</filter-name>
?? <url-pattern>*.do</url-pattern>
</filter-mapping>

</web-app>

包括了 filter ,servlet,有自己写的,还有用spring的jar的内容和别的jar的内容的。

项目总体xml就这些,此外还有表的xml和log4j的xml或是资源文件。

?

简单说下log4j的文件和表的xml
log4j日志必须导入包,大部分人用资源文件:写库如下
#级别
log4j.rootLogger = WARN,file

######DataBase
#方向
log4j.appender.file = org.apache.log4j.jdbc.JDBCAppender
#文件
log4j.appender.file.Driver=com.mysql.jdbc.Driver
log4j.appender.file.URL=jdbc:mysql://localhost:3306/mobileservicesystem
log4j.appender.file.User=root
log4j.appender.file.Password=root
#layout
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.Sql = insert into t_log4j(ld,lp,lc,lt,lm) values ('%d{yyyy-MM-dd HH:mm:ss}','%p','%c','%t','%m')

还有一个是写每天文件的:

#级别WARN
log4j.rootLogger = INFO,file

#方向
log4j.appender.file.encoding = utf-8
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=D:\\logs\\logs.txt
log4j.appender.file.layout=org.apache.log4j.PatternLayout

#layout
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern =DATE:%d{yyyy-MM-dd HH:mm:ss} LEVEL:%p CALSSNAME:%c?? ThunderName:%t?? INFORMATION:%m?? %n

总体开始log4j比较方便。实用不实用要看是什么项目和需求什么日志了。

表的xml是hibernate自动生成的 我们做的其实没有什么,有的时候会出现连接不到的现象我们只要在一些参数上改动就可以了:lazy="false",意思是不让它延迟加载。

为此项目中的xml说完了,下面说的是类文件和jsp文件

?

看这个贴前一定要认真阅读客户需求!

本帖说明的类:之前说说模式:

我们把这些分为 dao面向数据库的数据持久层和business面向业务逻辑的业务逻辑层。而这些类的得到需要从工厂模式得到。为了达到稳定我们写的是接口和实现类的方式来做的。所以我们会建立不少包:

dao:接口包
daoImpl:dao实现类包
business:接口包
businessImpl:business实现类包
factory:得到以上内容的工厂
只有这些是不够的,此外我们还有hibernate自动生成的模型类,我们把它们统一放在:
model:下
还有就是自己写的过滤器需要放在一个包:
filter:下
而还有一个是必须的,那就是
action:包,没有它就没有控制层
如果还有一些个别的方法可以再写个
util:包,
当然我们的表的xml也需要写个
xml:包,来放置
在这同时struts为我们写了个很长的包来放置他的资源文件,作用是为了I18N,国际化做的。

现在我们来一一说起:

开始时你导入了ssh就已经有了xml和model包的内容。但需要改改里面的路径和spring.xml的表映射路径。
当然在导入表是一定要让他帮你生成daoImpl的内容。选择spring的方式。这样可以省去写好多底层方法奥。但不全面我们需要改动:
就从这开始说起:我们保留的方法有:

public abstract void save(TPersonnel transientInstance);

public abstract void update(TPersonnel transientInstance);

public abstract void delete(TPersonnel persistentInstance);

public abstract TPersonnel findById(java.lang.String id);

public abstract List<TPersonnel> findAllTPersonnel();

public abstract List<TPersonnel> findByProperty(String propertyName, Object value);

public abstract List<String> findTPersonnelDistinctProperty(String propertyName);

其实这是一个dao接口的内容。有些需要自己写。具体的我就不写了。看方法名也应该知道怎么写!
还有不要搞错,这些不是我写的,是自己生产的。我们做的事就是在daoImpl中删除我添加方法,而接口用eclipse可以自动生成。来Impl类中右键提取就可以了。dao包中的内容和daoImpl包中的内容就这样做的。

在business中和businessImpl中不是这样做的。
现在我们做的是写business,而businessImpl我们先不写。
business中的类不是看表,而是看你项目有几个模块,怎么分的,充分考虑业务逻辑,当然里面的方法也就是该模块下要用的方法。至于businessImpl我们说完factory包再说。

现在说所以的源头:factory
我先不说什么 直接放上代码:这有4个文件。daoFactory,daoFactoryImpl,businessFactory,businessFactoryImpl。

package factory;


import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;


import dao.*;


public abstract class DAOFactory {
private static DAOFactory mf=null;
private static Object mutex=new Object();

public static DAOFactory newInstance(){
?? if(mf==null){
??? synchronized (mutex) {
???? if(mf==null){
????? try {
?????? Resource resource = new ClassPathResource("applicationContext.xml");
?????? XmlBeanFactory xbf = new XmlBeanFactory(resource);
?????? mf = (DAOFactory)xbf.getBean("DAOFactoryImpl");
????? } catch (Exception e) {
?????? e.printStackTrace();
?????? return null;
????? }
???? }
??? }
?? }
?? return mf;
}
public abstract IDept createDept();
public abstract IRoom createRoom();
public abstract IPersonnel createPersonnel();
public abstract ILog4j createLog4j();
public abstract IParticulardroit createParticulardroit();
public abstract IPersonnelassign createPersonnelassign();
public abstract IRole createRole();
public abstract IRoleassign createRoleassign();
public abstract IWordbook createWordbook();
}
———————————————————————————————————————————————

package factory;

import daoImpl.*;

import model.TPersonnel;

import org.hibernate.SessionFactory;

import util.IdForModel;

import dao.*;

public class DAOFactoryImpl extends DAOFactory{

private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
?? return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
?? this.sessionFactory = sessionFactory;
}

@Override
public IDept createDept() {
??
?? TDeptDAO deptDao = new TDeptDAO();
?? deptDao.setSessionFactory(sessionFactory);
?? return deptDao;
}


@Override
public ILog4j createLog4j() {
?? TLog4jDAO Log4jDao = new TLog4jDAO();
?? Log4jDao.setSessionFactory(sessionFactory);
?? return Log4jDao;
}

@Override
public IParticulardroit createParticulardroit() {
?? TParticulardroitDAO ParticulardroitDao = new TParticulardroitDAO();
?? ParticulardroitDao.setSessionFactory(sessionFactory);
?? return ParticulardroitDao;
}

@Override
public IPersonnel createPersonnel() {
?? TPersonnelDAO PersonnelDao = new TPersonnelDAO();
?? PersonnelDao.setSessionFactory(sessionFactory);
?? return PersonnelDao;
}

@Override
public IPersonnelassign createPersonnelassign() {
?? TPersonnelassignDAO PersonnelassignDao = new TPersonnelassignDAO();
?? PersonnelassignDao.setSessionFactory(sessionFactory);
?? return PersonnelassignDao;
}

@Override
public IRole createRole() {
?? TRoleDAO RoleDao = new TRoleDAO();
?? RoleDao.setSessionFactory(sessionFactory);
?? return RoleDao;
}

@Override
public IRoleassign createRoleassign() {
?? TRoleassignDAO RoleassignDao = new TRoleassignDAO();
?? RoleassignDao.setSessionFactory(sessionFactory);
?? return RoleassignDao;
}

@Override
public IRoom createRoom() {
?? TRoomDAO RoomDao = new TRoomDAO();
?? RoomDao.setSessionFactory(sessionFactory);
?? return RoomDao;
}

@Override
public IWordbook createWordbook() {
?? TWordbookDAO WordbookDAO = new TWordbookDAO();
?? WordbookDAO.setSessionFactory(sessionFactory);
?? return WordbookDAO;
}

}
———————————————————————————————————————————————

package factory;

import daoImpl.*;

import model.TPersonnel;

import org.hibernate.SessionFactory;

import util.IdForModel;

import dao.*;

public class DAOFactoryImpl extends DAOFactory{

private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
?? return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
?? this.sessionFactory = sessionFactory;
}

@Override
public IDept createDept() {
??
?? TDeptDAO deptDao = new TDeptDAO();
?? deptDao.setSessionFactory(sessionFactory);
?? return deptDao;
}


@Override
public ILog4j createLog4j() {
?? TLog4jDAO Log4jDao = new TLog4jDAO();
?? Log4jDao.setSessionFactory(sessionFactory);
?? return Log4jDao;
}

@Override
public IParticulardroit createParticulardroit() {
?? TParticulardroitDAO ParticulardroitDao = new TParticulardroitDAO();
?? ParticulardroitDao.setSessionFactory(sessionFactory);
?? return ParticulardroitDao;
}

@Override
public IPersonnel createPersonnel() {
?? TPersonnelDAO PersonnelDao = new TPersonnelDAO();
?? PersonnelDao.setSessionFactory(sessionFactory);
?? return PersonnelDao;
}

@Override
public IPersonnelassign createPersonnelassign() {
?? TPersonnelassignDAO PersonnelassignDao = new TPersonnelassignDAO();
?? PersonnelassignDao.setSessionFactory(sessionFactory);
?? return PersonnelassignDao;
}

@Override
public IRole createRole() {
?? TRoleDAO RoleDao = new TRoleDAO();
?? RoleDao.setSessionFactory(sessionFactory);
?? return RoleDao;
}

@Override
public IRoleassign createRoleassign() {
?? TRoleassignDAO RoleassignDao = new TRoleassignDAO();
?? RoleassignDao.setSessionFactory(sessionFactory);
?? return RoleassignDao;
}

@Override
public IRoom createRoom() {
?? TRoomDAO RoomDao = new TRoomDAO();
?? RoomDao.setSessionFactory(sessionFactory);
?? return RoomDao;
}

@Override
public IWordbook createWordbook() {
?? TWordbookDAO WordbookDAO = new TWordbookDAO();
?? WordbookDAO.setSessionFactory(sessionFactory);
?? return WordbookDAO;
}

}
———————————————————————————————————————————————

package factory;

import business.DeptBusinessService;
import business.Log4jBusinessService;
import business.ParticularBusinessService;
import business.PersonnelBusinessService;
import business.RoleBusinessService;
import business.RoomBusinessService;
import business.WordBookBusinessService;
import businessServiceImpl.DeptBusinessServiceImlp;
import businessServiceImpl.Log4jBusinessServiceImpl;
import businessServiceImpl.ParticularBusinessServiceImpl;
import businessServiceImpl.PersonnelBusinessServiceImlp;
import businessServiceImpl.RoleBusinessServiceImlp;
import businessServiceImpl.RoomBusinessServiceImlp;
import businessServiceImpl.WordBookBusinessServiceImpl;

public class BussinessFactoryImpl extends BussinessFactory{
private DAOFactory daoFactory;
public DAOFactory getDaoFactory() {
?? return daoFactory;
}

public void setDaoFactory(DAOFactory daoFactory) {
?? this.daoFactory = daoFactory;
}

@Override
public DeptBusinessService createDeptBusinessService() {
??
?? DeptBusinessServiceImlp dbsi = new DeptBusinessServiceImlp();
?? dbsi.setDaoFactory(daoFactory);
?? return dbsi;
}

@Override
public PersonnelBusinessService createPersonnelBusinessService() {
?? PersonnelBusinessServiceImlp personnelBusinessService = new PersonnelBusinessServiceImlp();
?? personnelBusinessService.setDaoFactory(daoFactory);
??? return personnelBusinessService;
}

@Override
public RoomBusinessService createRoomBusinessService() {
?? RoomBusinessServiceImlp roomBusinessService = new RoomBusinessServiceImlp();
?? roomBusinessService.setDaoFactory(daoFactory);
?? return roomBusinessService;
}

@Override
public Log4jBusinessService createLog4jBusinessService() {
?? Log4jBusinessServiceImpl log4jBusinessService = new Log4jBusinessServiceImpl();
?? log4jBusinessService.setDaoFactory(daoFactory);
?? return log4jBusinessService;
}

@Override
public ParticularBusinessService createParticularBusinessService() {
?? ParticularBusinessServiceImpl particularBusinessService = new ParticularBusinessServiceImpl();
?? particularBusinessService.setDaoFactory(daoFactory);
?? return particularBusinessService;
}

@Override
public WordBookBusinessService createWordBookBusinessService() {
?? WordBookBusinessServiceImpl wordBookBusinessService = new WordBookBusinessServiceImpl();
?? wordBookBusinessService.setDaoFactory(daoFactory);
?? return wordBookBusinessService;
}

@Override
public RoleBusinessService createRoleBusinessService() {
?? RoleBusinessServiceImlp roleBusinessService = new RoleBusinessServiceImlp();
?? roleBusinessService.setDaoFactory(daoFactory);
?? return roleBusinessService;
}

}
———————————————————————————————————————————————

这4个文件稳定了项目,那个spring的xml的资源文件就是在其中用的。本人认为这样的写法挺经典的。
此后如果看懂了这些也就可以理解businessImpl怎么写了,它实现了business,并有daofactory的内容。

这些说完了就还有fliter和action了,fliter我不想说了,这个关联性比较小,比较独立。我们来说action

其实action可以对应business写。如果真正看懂了会有一个疑问一直到到现在。就是:spring的xml放在服务器的私用路径下怎么吊,吊不了怎么在business中得到session。没有session怎么在action中写?难道要在action中写吗?那封装还有什么意义?
确实 我对这样问题迷惑了两天,其实不是这样的。开始我们写的spring的xml就写了action的类的方法就是用注入技术给方法注入被封装N层的session的。我具体在action中使用我们就需在action这样:

private PersonnelBusinessService personnelBusinessService;
public PersonnelBusinessService getPersonnelBusinessService() {
?? return personnelBusinessService;
}
public void setPersonnelBusinessService(
??? PersonnelBusinessService personnelBusinessService) {
?? this.personnelBusinessService = personnelBusinessService;
}

这样spring的xml的就给会在使用此方法时先给他注入了。spring的 xml写的是:

<!-- 人员 -->
<bean name="/personnelJump" class="action.PersonnelAction" >
?? <property name="personnelBusinessService">
??? <ref bean="PersonnelBusinessServiceImlp"/>
?? </property>
</bean>
<bean name="/personnelManager" class="action.PersonnelAction" >
?? <property name="personnelBusinessService">
??? <ref bean="PersonnelBusinessServiceImlp"/>
?? </property>
</bean>
<bean name="/findPname" class="action.PersonnelAction" >
?? <property name="personnelBusinessService">
??? <ref bean="PersonnelBusinessServiceImlp"/>
?? </property>
</bean>
<bean name="/savePersonnel" class="action.PersonnelAction" >
?? <property name="personnelBusinessService">
??? <ref bean="PersonnelBusinessServiceImlp"/>
?? </property>
</bean>

有几个方法就注入几个就可以了。

?

在action中你可能会遇到像分页,没有实现方法的麻烦,其实问题不仅如此。

对于分页我已经在别的贴中有所说明,而没有方法可以实现,其实并不可怕,你只要在business中加和方法,在在impl实现就好了。再问怎么实现。我们写的daoImpl包容了对表的任何方法,可以实现所有的,这就是spring生成daoImpl给我带来的思想,一定要写活。所以只要让businessImpl调用daoImpl的方法,填对参数就可以了。非常容易。这样写完了一定要测试!虽然底层的daoImpl方法是spring写的,不过我们改过,而且businessImpl中调用时的参数可是我们写的,人写的就一定要测试。将问题一一搞定。

当一切都做好时,我们只是做了一半的工作。还有服务器的问题,你做项目的服务器和实际的是不一样的。所以不到最后不要松气。最后的意思就是客户满意,并不是经理满意。

再劳动几句:日志要做规矩了啊,方便你自己啊。异常不要用时,一定要向外抛啊,这样才可以看到问题的所在。 开发ssh的wab项目我说的只是的总体方向,好多细节需要考虑。当然这样的项目需要好的工程师领导,需要3-5的人一起开发,一起努力。一个人写的漏洞总觉得太多了,毕竟人的脑子是有限的。

?

原文地址:http://hi.baidu.com/m2shuai/blog

  相关解决方案