五、功能模块的详细设计
1、程序目录结构图
2、数据库访问模块
数据库的设计是需要针对每个模块而不是每个页面进行设置数据库模块,所有的数据访问类DUser、DIncome、Dcustomer、DFeedback、DRemind从同一个父类Dcommom继承,该父类提供了一个方法getDBConnection来获取数据库的链接。
11
DCommon代码
public class DCommon {
public Connection getDBConnection( ) {//尝试连接数据库 try
{ //载入MySQL的JDBC驱动类
Class.forName(CommonConst.DB_DRIVER_CLASSNAME); Connectionconn=DriverManager.getConnection
( CommonConst.DB_CONN_STRING );//获得数据库连接
return conn; }
catch(Exception ex) {
ex.printStackTrace(); return null; } } }
3、共通Servlet的处理
本系统中一旦代码出现了异常,将直接向上抛出,一直抛到共通类
Servlet进行处理。
在共通Servlet中发现异常,将页面跳转到错误处理页面,并把错误信息在页面上显示出来,同时提供一个往登录页面迁移的链接。 对应的共通Servlet的相关代码如下:
public class CommonServlet extends HttpServlet {
//保存各页面Id对应的action类的对象
private Hashtable hPageHandler = new Hashtable();
//配置文件的存放位置
private JXPathContext configContext = null;
public void init() {
12
//取得配置文件,并获得其中的dom元素
String filePath = getInitParameter(\
String fileRealPath = getServletContext().getRealPath(filePath);
//尝试建立配置文件的DOM try {
org.jdom.input.SAXBuilder builder = new SAXBuilder(); org.jdom.Document pDoc = builder.build(fileRealPath );
configContext = JXPathContext.newContext(pDoc);
GlobalObjectProvider.init( configContext ); }
catch(Exception e) {
System.out.println(\初始化失败!\ }
//初始化共通类以获取页面信息 CommonConst.init(); }
//每一种动作第一次执行的时候,初始化对应的类 public void doPost ( HttpServletRequest request,
HttpServletResponse response ) throws ServletException, IOException {
//设置提交表单的中文编码
request.setCharacterEncoding(\
HttpSession mySession = request.getSession(true);
//得到用户输入信息
String sPageId = request.getParameter(\ String sActionId = request.getParameter(\
if ( sPageId == null || sPageId.equals(\
|| sActionId == null || sActionId.equals(\ {
//非法进入页面,跳转到首页 mySession.invalidate();
response.sendRedirect(\ return;
13
}
//如果非法进入页面(登录页面除外) if ( !sPageId.equals(\
&& mySession.getAttribute(\ {
//非法进入页面,跳转到首页 mySession.invalidate();
response.sendRedirect(\ return; } try {
//根据pageId获得处理对象,如果没有则创建一个对象 Object oActionObject = hPageHandler.get( sPageId ); if ( oActionObject == null ) {
//根据配置文件创建一个新对象
String sClassName = (String)configContext.getValue(
\
oActionObject = Class.forName( sClassName ).newInstance(); hPageHandler.put( sPageId, oActionObject); }
//取得方法名
String sMethodName = (String)configContext.getValue(
\
//生成对应的参数,并调用对应对象的对应方法 //inputData是根据传入的参数做成的 Hashtable inputData = new Hashtable();
Enumeration params = request.getParameterNames(); while( params.hasMoreElements()) {
String sParaName = (String)params.nextElement();
inputData.put( sParaName, request.getParameter(sParaName) ); }
//outputData是下一个页面的值域,在此只是被初始化 Hashtable outputData = new Hashtable();
//生成参数列表
Class[] paraType = { Class.forName(\
14
Class.forName(\
Class.forName(\ Object[] paraObj = { inputData, outputData, mySession };
//生成Method对象
Method invokeMethod = oActionObject.getClass().getMethod( sMethodName, paraType );
//调用方法
invokeMethod.invoke( oActionObject, paraObj );
//根据outputData的结果决定下一个页面
String sNextPageId = (String)outputData.get(\ String sRealPagePath = (String)configContext.getValue(
\ //设置下一个页面的值域
mySession.setAttribute( sNextPageId, outputData ); response.sendRedirect( sRealPagePath ); return; }
catch(Exception e) {
//页面处理出错,跳转到错误处理页面 e.printStackTrace();
Hashtable outputData = new Hashtable(); outputData.put( \ //设置错误页面的值域
mySession.setAttribute( CommonConst.VIEWID_ERROR, outputData ); response.sendRedirect(\ return; } }
public void doGet ( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
doPost( request, response ); } }
错误页面:
15