Struts2.properties属性文件中的devModel和DynamicMethodInvocation。。。Action中servlet对象的获取

来源:互联网 发布:织梦cms移动版静态化 编辑:程序博客网 时间:2024/04/30 18:44

一、<constant name="struts.devMode" value="true"/>(默认为false)

:struts的开发模式,就是设置了开发模式,并且对struts.xml做了改动不用重新启动Tomcat就可以自动加载struts.xml。一大利好!

<constant name="struts.enable.DynamicMethodInvocation" value="false"/>(默认为true)

为true,表示采用动态方法调用,例:URL为http://127.0.0.1:8080/strutsproj/login!add.action就可以调用名称为login的Action中的add方法。方便调试,但一般在开发中不见时使用!


二、Action中servlet对象的获取:

session可以通过实现SessionAware接口

setSession(Map<String,Object> session)

{

     this.session=session

}获取session对象。这种方式是通过ServletConfigIntercepter拦截器创建session对象的(使用IOC模式:哪个Action中需要session对象,拦截器会为其注入session,不用在当前Action中主动创建),源码如下

... 

if ((action instanceof SessionAware)) {
      ((SessionAware)action).setSession(context.getSession());
    }

...

也可以通过ActionContext中的getSession()直接获取。


application是通过ApplicationAware,实现方式和session一样。


request可以通过实现RequestAware接口(通过拦截器ServletConfigIntercepter注入(IOC模式))或者通过

ServletActionContext中的getRequest()获取request对象。

源码如下:

 public static HttpServletRequest getRequest()
  {
    return (HttpServletRequest)ActionContext.getContext().get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
  }

0 0
原创粉丝点击