版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、基于Struts+Spring+JDBC架构的课程设计实训教学示例项目网上银行账户管理系统在项目中实现对XML文件进行解析的各种功能在项目中实现对XML文件进行解析的各种功能1、添加一个对XML进行解析的JavaBean组件的接口及实现类(1)接口名称为XMLInformationConfig,包名称为(2)设计该接口package com.px1987.webbank.util;import com.px1987.webbank.exception.*;public interface XMLInformationConfig public void xmlInit(String confi
2、gFilePathAndName) throws WebBankException;2、为上面的接口提供一个实现类(1)添加SAXInformationConfig类类名称为SAXInformationConfig,包名称为,基类为tHandler(采用SAX技术)。(2)编程该SAXInformationConfig类package com.px1987.webbank.util;import org.xml.sax.helpers.DefaultHandler;import com.px1987.webbank.exception.WebBankException;import java.
3、io.*;import org.xml.sax.Attributes;ion;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;public class SAXInformationConfig extends DefaultHandler implements XMLInformationConfigprivate String marqueeText = ;public String getMarqueeText()return marqueeText;public SAXInforma
4、tionConfig()/* * 读取配置文件信息,并设置相关参数。 * param configFileName String 配置文件路径及文件名。 */public void xmlInit(String configFilePathAndName) throws WebBankExceptionSAXParserFactory saxParserFactory = null;SAXParser saxParser = null;saxParserFactory = SAXParserFactory.newInstance(); /获取SAX工厂对象saxParserFactory.se
5、tNamespaceAware(false);saxParserFactory.setValidating(false);trysaxParser = saxParserFactory.newSAXParser(); /创建出SAX解析/*将解析器和解析对象XMLDOMData.xml联系起来,同时指定事件回调方法的对象开始解析*/saxParser.parse(new File(configFilePathAndName), this);catch (javax.xml.parsers.ParserConfigurationException pe)throw new WebBankExce
6、ption(在SAXInformationConfig类中的xmlInit方法中出现ParserConfigurationException);catch (SAXException se)throw new WebBankException(在SAXInformationConfig类中的xmlInit方法中出现SAXException);catch (java.io.IOException ioe)throw new WebBankException(在SAXInformationConfig类中的xmlInit方法中出现IOException);catch (Exception ex)t
7、hrow new WebBankException(在SAXInformationConfig类中的xmlInit方法中出现Exception);finallysaxParserFactory = null;saxParser = null;private String tagElementName = null;/定义开始解析元素的方法. 这里是将中的名称xxx提取出来.public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXExceptionth
8、is.tagElementName = qName; /获得该标签的名称/这里是将之间的标签体的值加入到currentValuepublic void characters(char ch, int start, int length) throws SAXExceptionString tagBodyText = new String(ch, start, length); /获得标签体的文字串内容if (this.tagElementName.equals(marquee-text) & !tagBodyText.trim().equals()marqueeText = tagBodyTe
9、xt;3、为SAXInformationConfig实现类提供一个工厂类XMLInformationConfigFactory(1)用来统一创建XMLInformationConfig接口的对象实例名称为XMLInformationConfigFactory类,包名称为 ,并且从类继承。(2)编程该XMLInformationConfigFactory类package com.px1987.webbank.factory;import java.util.Observable;import com.px1987.webbank.config.ClassNameConfig;ebbank.exc
10、eption.WebBankException;import com.px1987.webbank.util.*;public class XMLInformationConfigFactory extends Observable public XMLInformationConfigFactory() public XMLInformationConfig newXMLInformationConfigBean(String xmlInformationConfigImpleClassName) throws WebBankExceptionXMLInformationConfig one
11、XMLInformationConfigImple=null;Class oneClass=null;try oneClass = Class.forName(xmlInformationConfigImpleClassName); catch (ClassNotFoundException e)int logImpleKind=Integer.parseInt(ClassNameConfig.getProperty(logImpleKind);this.addObserver(LogInfoFactory.newLogInstance(logImpleKind);this.setChange
12、d(); /注意要设置变化点notifyObservers(e);/当出现异常时将通知各个观察者throw new WebBankException(不能正确地获得+xmlInformationConfigImpleClassName+类);try oneXMLInformationConfigImple = (XMLInformationConfig)oneClass.newInstance(); catch (InstantiationException e)int logImpleKind=Integer.parseInt(ClassNameConfig.getProperty(logI
13、mpleKind);this.addObserver(LogInfoFactory.newLogInstance(logImpleKind);this.setChanged(); /注意要设置变化点notifyObservers(e);/当出现异常时将通知各个观察者throw new WebBankException(不能正确地创建+xmlInformationConfigImpleClassName+类的对象实例); catch (IllegalAccessException e)int logImpleKind=Integer.parseInt(ClassNameConfig.getPro
14、perty(logImpleKind);this.addObserver(LogInfoFactory.newLogInstance(logImpleKind);this.setChanged(); /注意要设置变化点notifyObservers(e);/当出现异常时将通知各个观察者throw new WebBankException(不能正确地创建+xmlInformationConfigImpleClassName+类的对象实例);return oneXMLInformationConfigImple;4、修改classNameConfig.xml文件以添加对SAXInformation
15、Config类的定义sName5、在WebBankListener类中对上面的SAXInformationConfig类进行使用package com.px1987.webbank.listener;import java.util.ArrayList;import java.util.List;import java.util.Timer;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import
16、javax.servlet. . SessionAttributeListener;import javax.servlet. . SessionBindingEvent;import javax.servlet. . SessionEvent;import javax.servlet. . SessionListener;import com.px1987.webbank.config.ClassNameConfig;import com.px1987.webbank.exception.WebBankException;import com.px1987.webbank.factory.X
17、MLInformationConfigFactory;import com.px1987.webbank.model.vo.UserInfoVO;import com.px1987.webbank.util.OnLineCounter;import com.px1987.webbank.util.WebBankScheduleTask;import com.px1987.webbank.util.XMLInformationConfig;public class WebBankListener implements SessionListener, SessionAttributeListen
18、er, ServletContextListener private String saxXMLPathAndFileName=null;private static List onLineUserList=new ArrayList();/当在线人员数比较多时,开销比较大!private ServletContext oneServletContext=null;public void sessionCreated( SessionEvent arg0) /某用户访问,将增加访问计数总数OnLineCounter.addOneUser();public void sessionDestroy
19、ed( SessionEvent arg0) OnLineCounter.reduceOneUser();/某用户离线,将减少访问计数总数public void attributeAdded( SessionBindingEvent SessionBindingEvent)/识别该用户是否已经登录if( SessionBindingEvent.getName().equals(oneUserInfoVO)UserInfoVO nowLoginUserInfo=(UserInfoVO) SessionBindingEvent.getValue();onLineUserList.add(nowLo
20、ginUserInfo);/* * 一旦该用户登陆成功,将减少“游客”的计数 */ Integer onLineGuest=(Integer)oneServletContext.getAttribute(onLineGuest);int onLineGuestDigit=onLineGValue();oneServletContext.setAttribute(onLineGuest,new Integer(onLineGuestDigit-1)=0?(onLineGuestDigit-1):0);public void attributeRemoved( SessionBindingEven
21、t SessionBindingEvent)if( SessionBindingEvent.getName().equals(oneUserInfoVO)UserInfoVO nowLoginUserInfo=(UserInfoVO) SessionBindingEvent.getValue();onLineUserList.remove(nowLoginUserInfo);/除掉该登陆用户的信息public void attributeReplaced( SessionBindingEvent SessionBindingEvent)public static List getOnLineU
22、serList()return onLineUserList;public void contextDestroyed(ServletContextEvent arg0) scheduletimer.cancel();private Timer scheduletimer = null;public void contextInitialized(ServletContextEvent arg0) oneServletContext=arg0.getServletContext(); XMLInformationConfig xmlSaxInfoBeanID=null;saxXMLPathAn
23、dFileName=oneServletContext.getInitParameter(saxXMLPathAndFileName); String saxXMLFileName=oneServletContext.getRealPath()+saxXMLPathAndFileName; try String xmlInformationConfigImpleClassName=ClassNameConfig.getProperty(xmlInformationConfigImple.className); xmlSaxInfoBeanID=new XMLInformationConfigFactory().newXMLInformationConfigBean(xmlInformationConfigImpleClassName); catch(WebBankException e) e.printStackTrace(); try xmlSaxInfoBeanID.xmlInit(saxXMLFileName); catch (WebBankException e) e.printStackTrace(); System.out.println(在WebBankListener类中的contextInitialized方法出现了We
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年七年级历史上册期末考试试卷及答案(九)
- 智能护理实操患者康复训练动作速度创新应用训练课件
- 中国人民财产保险股份有限公司漳州市分公司2026校园招聘备考题库及1套参考答案详解
- 2026年佛山市高明区富湾湖实验中学公开招聘临聘教师备考题库及1套完整答案详解
- 2026年旭辉实验学校招聘教师备考题库及答案详解(考点梳理)
- 2026年韶山旅游发展集团招聘中层管理人员备考题库及1套参考答案详解
- 2026年上海市临床检验中心招聘备考题库带答案详解
- 中山市西区聚星学校2026年春季学期教师招聘备考题库及1套完整答案详解
- 2026年浙江中医药大学临床医学院及直属附属医院公开招聘人员备考题库及一套完整答案详解
- 中山市西区聚星学校2026年春季学期教师招聘备考题库及1套参考答案详解
- 四环素的发酵工艺课件
- 中山大学研究生因公临时出国境申报表
- 泥浆护壁钻孔灌注桩的施工
- 征信调研报告3篇
- YY/T 0127.18-2016口腔医疗器械生物学评价第18部分:牙本质屏障细胞毒性试验
- LY/T 2677-2016油茶整形修剪技术规程
- GB/T 8924-2005纤维增强塑料燃烧性能试验方法氧指数法
- GB/T 20969.2-2021特殊环境条件高原机械第2部分:高原对工程机械的要求
- 马克思主义经典著作导读课后练习试题答案与解析搜集
- 快速记忆法训练课程速读课件
- 集体教学活动中有效提问和回应课件
评论
0/150
提交评论