首页 > 其他分享 >struts2值栈

struts2值栈

时间:2023-09-21 16:31:54浏览次数:20  
标签:拦截器 name struts2 ValueStack Action size 值栈 属性


[size=small]众所周知,Strut 2的Action类通过属性可以获得所有相关的值,如请求参数、Action配置参数、向其他Action传递属性值(通过chain结果)等等。要获得这些参数值,我们要做的唯一一件事就是在Action类中声明与参数同名的属性,在Struts 2调用Action类的Action方法(默认是execute方法)之前,就会为相应的Action属性赋值。

  要完成这个功能,有很大程度上,Struts 2要依赖于ValueStack对象。这个对象贯穿整个Action的生命周期(每个Action类的对象实例会拥有一个ValueStack对象)。当Struts 2接收到一个.action的请求后,会先建立Action类的对象实例,但并不会调用Action方法,而是先将Action类的相应属性放到ValueStack对象的顶层节点(ValueStack对象相当于一个栈)。只是所有的属性值都是默认的值,如String类型的属性值为null,int类型的属性值为0等。

  在处理完上述工作后,Struts 2就会调用拦截器链中的拦截器,当调用完所有的拦截器后,最后会调用Action类的Action方法,在调用Action方法之前,会将ValueStack对象顶层节点中的属性值赋给Action类中相应的属性。大家要注意,在这里就给我们带来了很大的灵活性。也就是说,在Struts 2调用拦截器的过程中,可以改变ValueStack对象中属性的值,当改变某个属性值后,Action类的相应属性值就会变成在拦截器中最后改变该属性的这个值。

  从上面的描述很容易知道,在Struts 2的的Action类可以获得与属性同名的参数值就是通过不同的拦截器来处理的,如获得请求参数的拦截器是params,获得Action的配置参数的拦截器是staticParams等。在这些拦截器内部读取相应的值,并更新ValueStack对象顶层节点的相应属性的值。而ValueStack对象就象一个传送带,将属性值从一个拦截器传到了另一个拦截器(当然,在这其间,属性值可能改变),最后会传到Action对象,并将ValueStack对象中的属性的值终值赋给Action类的相应属性


拦截器的源代码:

packageinterceptors; 

importjava.util.Enumeration; 

importjava.util.Map; 

importjava.util.Properties; 

importjava.io.InputStream; 

importjava.io.FileInputStream; 

importcom.opensymphony.xwork2.ActionContext; 

importcom.opensymphony.xwork2.ActionInvocation; 

importcom.opensymphony.xwork2.config.entities.ActionConfig; 

importcom.opensymphony.xwork2.interceptor.AbstractInterceptor; 

importcom.opensymphony.xwork2.util.ValueStack; 

publicclassPropertyInterceptorextendsAbstractInterceptor 

{ 

  privatestaticfinalStringDEFAULT_PATH_KEY="path"; 

  privatestaticfinalStringDEFAULT_ENCODING_KEY="encoding"; 

  privatestaticfinalStringDEFAULT_SEPARATOR_KEY="separator"; 

  protectedStringpathKey=DEFAULT_PATH_KEY; 

  protectedStringencodingKey=DEFAULT_ENCODING_KEY; 

  protectedStringseparatorKey=DEFAULT_SEPARATOR_KEY; 

  publicvoidsetPathKey(StringpathKey) 

  { 

    this.pathKey=pathKey; 

  } 

  publicvoidsetEncodingKey(StringencodingKey) 

  { 

    this.encodingKey=encodingKey; 

  } 

  publicvoidsetSeparatorKey(StringseparatorKey) 

  { 

    this.separatorKey=separatorKey; 

  } 

  @Override 

  publicStringintercept(ActionInvocationinvocation)throwsException 

  { 

    ActionConfigconfig=invocation.getProxy().getConfig(); 

    Map<String,String>parameters=config.getParams(); 

    if(parameters.containsKey(pathKey)) 

    { 

      Stringpath=parameters.get(pathKey); 

      Stringencoding=parameters.get(encodingKey); 

      Stringseparator=parameters.get(separatorKey); 

      if(encoding==null) 

        encoding="UTF-8"; 

      if(separator==null) 

        separator=""; 

      path=invocation.getAction().getClass().getResource(path) 

          .getPath(); 

      Propertiesproperties=newProperties(); 

      InputStreamis=newFileInputStream(path); 

      java.io.Readerreader=newjava.io.InputStreamReader(is,encoding); 

       

      properties.load(reader); 

      ActionContextac=invocation.getInvocationContext(); 

      ValueStackstack=ac.getValueStack(); 

      System.out.println(stack.hashCode()); 

      Enumerationnames=properties.propertyNames(); 

      while(names.hasMoreElements()) 

      { 

        // 下面会使用setValue方法修改ValueStack对象中的相应属性值 

        Stringname=names.nextElement().toString(); 

        if(!name.contains(".")) 

          stack.setValue(name,properties.get(name)); 

        StringnewName=null; 

        newName=parameters.get(name.replaceAll(".","")); 

        if(newName!=null) 

          stack.setValue(newName,properties.get(name)); 

        if(!separator.equals("")) 

        { 

          newName=name.replaceAll(".",""); 

          stack.setValue(newName,properties.get(name)); 

        }         

        newName=name.replaceAll(".",separator); 

        stack.setValue(newName,properties.get(name)); 

      } 

    } 

    returninvocation.invoke(); 

  } 

}



用于测试的Action类的源代码:

packageactions; 

publicclassMyAction 

{ 

  privateStringname; 

  privateIntegerprice; 

  privateStringlog4jappenderstdout; 

  privateStringlog4j_rootLogger; 

  privateStringconversionPattern; 

  publicStringgetName() 

  { 

    returnname; 

  } 

  publicvoidsetName(Stringname) 

  { 

    this.name=name; 

  } 

  publicIntegergetPrice() 

  { 

    returnprice; 

  } 

  publicvoidsetPrice(Integerprice) 

  { 

    this.price=price; 

  } 

  publicStringgetLog4jappenderstdout() 

  { 

    returnlog4jappenderstdout; 

  } 

  publicvoidsetLog4jappenderstdout(Stringlog4jappenderstdout) 

  { 

    this.log4jappenderstdout=log4jappenderstdout; 

  } 

  publicStringgetLog4j_rootLogger() 

  { 

    returnlog4j_rootLogger; 

  } 

  publicvoidsetLog4j_rootLogger(Stringlog4j_rootLogger) 

  { 

    this.log4j_rootLogger=log4j_rootLogger; 

  } 

  publicStringgetConversionPattern() 

  { 

    returnconversionPattern; 

  } 

  publicvoidsetConversionPattern(StringconversionPattern) 

  { 

    this.conversionPattern=conversionPattern; 

  } 

  publicStringexecute() 

  { 

    System.out.println("name:"+name); 

    System.out.println("price:"+price); 

    System.out.println("log4jappenderstdout:"+log4jappenderstdout); 

    System.out.println("log4j_rootLogger:"+log4j_rootLogger); 

    System.out.println("conversionPattern:"+conversionPattern); 

    returnnull; 

  } 

}[/size][/size][/size][/size][/size][/size][/size][/size]

标签:拦截器,name,struts2,ValueStack,Action,size,值栈,属性
From: https://blog.51cto.com/u_16230604/7555234

相关文章

  • struts2与ajax交互
    实现动态检验用户是否在数据库中存在. 一:JSP页面<scripttype="text/javascript"src="JS/Ajax.js"<scripttype="text/javascript">functionif(value!=""){"get","check?usersName="+value,deal,null);}......
  • struts2.1.8.1+jquery1.4.2返回json数据
    1、引入包(本文中的包全部引自struts-2.1.8.1\lib):struts2-json-plugin-2.1.8.1.jarjson-lib-2.1.jarcommons-collections-3.2.jarcommons-beanutils-1.7.0.jarcommons-lang-2.3.jarcommons-logging-1.0.4.jarezmorph-1.0.3.jar这7个包是返回json形式的数据必须的。因为jso......
  • struts2.1 + Spring 3.X + hibernate3.X架构搭建问题记录
    目前正在试图搭建一个SSH的架构,之前在myeclipse8.6+ssh(struts2.1,spring2.5,hibernate3) +mysql+tomcat6.0做过例子,当时有老师带着,遇到问题也都解决了。而且,自己练习单表的增删改查时也能独立完成了。但是现在换成了myeclipse2014+orcle后,就是通不过去,郁闷中:现在想一遍解......
  • Exception starting filter struts2(Caught exception while loading file struts-def
    2008-9-1811:31:57org.apache.catalina.core.StandardContextfilterStart严重:Exceptionstartingfilterstruts2Caughtexceptionwhileloadingfilestruts-default.xml-[unknownlocation] atcom.opensymphony.xwork2.config.providers.XmlConfigurationProvider.lo......
  • 奇妙的paramsPrepareParamsStack——Struts2中ModelDriven问题
    每次调用全文检索都要传入所有的参数值,不然导致当某个参数没有传值的时候但取到的却是上一次搜索提供的参数值。最后才发现原来是Action类里implementsModelDriven实现的原因:publicclassDocuIndexActionextendsBaseActionimplementsModelDriven{DocSearchParamdocSearchPa......
  • Struts2-045 Remote Code Execution Vulnerablity(CVE-2017-5638)
    目录1.1、漏洞描述1.2、漏洞等级1.3、影响版本1.4、漏洞复现1、基础环境2、漏洞扫描nacs3、漏洞验证1.5、修复建议说明内容漏洞编号CVE-2017-5638漏洞名称S2-045远程代码执行漏洞漏洞评级高危影响范围Struts2.3.5-Struts2.3.31Struts2.5-Struts2.......
  • Struts2 v2.1.6 笔记
    1.启动<constantname="struts.devMode"value="true"/>或者<constantname="struts.configuration.xml.reload"value="true"/>时启动tomcat报错。org.apache.catalina.core.StandardContextfilterStart严重:Exceptionstarting......
  • [struts2]配置dispatcher INCLUDE和Forward可能问题解决
    Struts2.1.6GA不支持<dispatcher>FORWARD</dispatcher>和<dispatcher>INCLUDE</dispatcher>你要是和URLRewrite过滤器一起工作会报错。目前最新版本GeneralAvailability(GA)Releases-ReadyforPrimeTime!Struts2.1.8("bestavailable")Struts2.0.14(&qu......
  • Struts2与URL Rewrite整合注意的地方
    1.在重新定向的jsp界面中不要使用<s:include>标签,会发生空指针异常,你可以使用<jsp:include>去解决 2.web.xml中UrlRewrite过滤器要在struts2过滤器前面,在Struts2过滤器映射要这样写<filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern>......
  • 使用Struts2拦截器需要注意的地方
    今天在写拦截器的时候发现一个貌似属于bug的问题我使用的版本是2.1.8 当访问一个满足拦截器拦截条件的请求时,虽然代码已经执行进入拦截器,并且返回结果也是拦截后改变的跳转结果,但是却没有按照返回结果跳转,而是依旧进入了action。 源代码是这样的:publicStringintercept(ActionIn......