欢迎来到天天文库
浏览记录
ID:34550998
大小:93.67 KB
页数:9页
时间:2019-03-07
《j2ee框架技术第2章实训题》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实训题22-1x使用Action访问ActionContext方式,编写一个网站计数器。步骤:(1)启动MyEclipse,新建Webproject项目,项目名称为“lab02_l(2)添加Struts2框架库(3)双击struts.xml文件,打JFstruts的视图编辑,设计如下:OstrutsjcmlCounterACtion.java]題indexjsp)•?Palette>c/3Extends/Location田PackageQActionU>ResultUInclud
2、e區1JSP區]HTML圖1indexjspurl:/index.jsp3、Hclass-uaction.CounterActionu>/index.jsp(1)双击Action图标,打开CounterAction.java代码,在execute()方法中加入如卜代码:publicStringexecute(){//TODOAuto-generatedmethodstub//(1)ActionContextActionContextac=ActionConte
3、Hclass-uaction.CounterActionu>/index.jsp
4、xt•getContext();intcounter;if(ac.getSession()>get(”counterH)==null)counter=0;elsecounter=Integer.parseInt(String>vaLueO/(ac>getSession()・get(”counterH)));counter++;ac•getSession()•put("counter”,counter);//(2)IoC方式/*HttpSessionsession=request.getSession()
5、;intcounter;if(session.getAttribute(HcounterH)==nu11)counter=0;elsecounter=Integer.parselnt(String.valueOf(session.getAttribute(”counter'1)));counter++;session•setAttribute("counter'1,counter);*///(3)非:EoC方式/*HttpServletRequestrequest二ServletActionContex
6、t.getRequest();HttpSessionsession=request.getSession();intcounter;if(session.getAttribute("counterM)==null)counter=0;elsecounter=Integer.parselnt(String.valueOf(session.getAttribute(”countern)));counter++;session•setAttribute(Hcounter'counter);*/returnS
7、UCCESS;}(1)修改index.jsp页面,加入如下代码:<%@taglibprefixes"uri="/struts-tags"%>
8、诃112>(2)运行WebApplication程序,运行结果如卜•图所示。http://chen:8080/lab02/共计13人访问注:采用IoC方式的要修改CounterActionjava代码publiccl
9、assCounterActionextendsActionSupportimplementsServletRequestAware{//(2)IoC方式//(2)IoC方式privateHttpServletRequestrequest;//(2)IoC方式^OverridepublicvoidsetServletRequest(HttpServletRequestrequest){//TODOAuto-generatedmethodstu
此文档下载收益归作者所有