您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > Struts2获取WEB资源的几种方法汇总
Struts2获取WEB资源本文档的代码都是在同一个工程中建立的1、Struts2_通过ActionContext获取WEB资源(一)首先创建动态web工程struts2-6(工程名)(二)文件总目录:(三)把Struts2所需的核心jar包复制到WebContent/WEB-INF/lib下,所需jar包如下:(四)先在WebContent下创建index.jsp,其代码如下:%@pageimport=java.util.Date%%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8titleInserttitlehere/title/headbodyahref=TestActionContext.action?name=atguiguTestActionContext/abrbrahref=TestAware.action?name=atguiguTestAware/abrbrahref=TestServletActionContextTestServletActionContext/abrbrahref=TestServletAwareTestServletAware/abrbr%if(application.getAttribute(date)==null)application.setAttribute(date,newDate());%/body(五)/html在WebContent下创建test-actionContext.jsp,其代码如下:%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8titleInserttitlehere/title/headbodyh4TestActionContextPage/h4application:${applicationScope.applicationKey}brbrsession:${sessionScope.sessionKey}brbrrequest:${requestScope.requestKey}brbrage:${parameters.ageKey}brbr/body/html(六)在src\创建struts.xml,其代码如下:?xmlversion=1.0encoding=UTF-8?!DOCTYPEstrutsPUBLIC-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN=defaultnamespace=/extends=struts-defaultactionname=TestActionContextclass=com.atguigu.struts2.action.TestActionContextActionresult/test-actionContext.jsp/result/actionactionname=TestAwareclass=com.atguigu.struts2.action.TestAwareActionresult/test-aware.jsp/result/actionactionname=TestServletActionContextclass=com.atguigu.struts2.action.TestServletActionContextActionresult/success.jsp/result/actionactionname=TestServletAwareclass=com.atguigu.struts2.action.TestServletAwareAction1result/success.jsp/result/action/package(七)/struts在src\创建包(com.atguigu.struts2.action)在该包下创建类文件(TestActionContextAction.java)其代码如下:packagecom.atguigu.struts2.action;importjava.util.Map;importjavax.print.DocFlavor.STRING;importorg.apache.struts2.dispatcher.SessionMap;importcom.opensymphony.xwork2.ActionContext;publicclassTestActionContextAction{publicStringexecute(){//0.获取ActionContext对象//ActionContext是Action的上下文对象,可以从中获取到当往Action需要的一切信息ActionContextactionContext=ActionContext.getContext();//1.获取application对应的Map,并向其中添加一个属性//通过调用ActionContext对象的getApplication()方法来获取application对象的Map对象MapString,ObjectapplicationMap=actionContext.getApplication();//设置属性applicationMap.put(applicationKey,applicationValue);//获取属性Objectdate=applicationMap.get(date);System.out.println(date:+date);//2.sessionMapString,ObjectsessionMap=actionContext.getSession();sessionMap.put(sessionKey,sessionValue);System.out.println(sessionMap.getClass());if(sessionMapinstanceofSessionMap){SessionMapsm=(SessionMap)sessionMap;sm.invalidate();System.out.println(session失效了.);}//3.request*//ActionContext中并没有提供getRequest方法来获取request对应的Map//需要手工调用get()方法,传入request字符串来获取.MapString,ObjectrequestMap=(MapString,Object)actionContext.get(request);requestMap.put(requestKey,requestValue);//4.获取去请求参数对应的Map,并获取指定的参数值。//键:请求参数的名字,值:请求参数的值对应的字符串数组//注意:1.getParameter的返回值为在MapString,Object,而不是MapString,String[]//2.MapString,Objectparameters=actionContext.getParameters();System.out.println(((String[])parameters.get(name))[0]);parameters.put(age,100);returnsuccess;}}(八)Web.xml代码:?xmlversion=1.0encoding=UTF-8?web-appxmlns:xsi==:web=:schemaLocation=://java.sun.com/xml/ns/javaee/web-app_2_5.xsdid=WebApp_IDversion=2.5!--配置Struts2的Filter--filterfilter-namestruts2/filter-namefilter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class/filterfilter-mappingfilter-namestruts2/filter-nameurl-pattern/*/url-pattern/filter-mappingwelcome-file-listwelcome-fileindex.jsp/welcome-file/welcome-file-list/web-app/web-app(九)效果图:2、Struts2_通过Aware接口获取WEB资源在src\下的包(com.atguigu.struts2.action)下再该包下创建类文件(TestAwareAction.java)继承(ApplicationAware)其代码如下:packagecom.atguigu.struts2.action;importjava.util.Map;importorg.apache.struts2.interceptor.ApplicationAware;importorg.apache.struts2.interceptor.ParameterAware;importorg.apache.struts2.interceptor.RequestAware;importorg.apache.struts2.interceptor.SessionAware;publicclassTestAwareActionimplementsApplicationAware,SessionAware,RequestAware,ParameterAware{publicStringexecute(){//1.向application中加入一个属性:applicationKey2-applicationValueapplication.put(applicationKey2,applicationValue2);//2.从application中读取一个属性date,并打印.Sys
本文标题:Struts2获取WEB资源的几种方法汇总
链接地址:https://www.777doc.com/doc-2861054 .html