Struts1、Struts2

1、web.xml主要配置:

struts1:

  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
 </servlet>

  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

 

struts1 控制器的核心是actionservlet,主要由它来负责各个功能的调度。启动时Actonservlet调用digester来解析web.xml、struts-config.xml等配置文件,对应组装到moduleconfig实现类;请求到来时调用requestProcessor来具体处理请求。


struts2:

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>

    </filter>
   
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 

struts2 控制的核心是Dispatcher,该对象由FilterDispatcher初始化时创建。 Dispatcher负责创建各种ConfigurationProvider来解析不同的资源文件,如struts.properties,struts.xml等,并负责创建xwork框架的注入核心:Container;请求到来时,FilterDispatcher检查Uri后缀是否是.action、检查Uri能否对应到配置文件的命名空间,通过检查后组装成ActionMapping对象,然后再由 Dispatcher来处理请求。

 

2.如何解析配置文件

struts1:

struts-config.xml中actionmapping配置:

 

struts-config.xml默认位置是:/WEB-INF/struts-config.xml,如果放在其他位置,则必须在web.xml配置actionServlet的地方手动配置:

 

解析过程大致如下:

        getServletContext().setAttribute(Globals.MODULE_KEY
            + config.getPrefix(), config);

 

struts2:

这里主要指.xml文件。struts2默认的配置文件有三个:struts-default.xml,struts-plugin.xml,struts.xml,且位置必须是放在工程的根目录下。如果需要放在其他地方,则必须在web.xml配置FilterDispatcher的时候配置init-param参数。

 

以上三个文件是由StrutsXmlConfigurationProvider来完成解析的。


ConfigurationManager获得该provider方法如下:



 

ConfigurationManager获得Configuration的方法如下:

 

Configuration的reloadContainer方法:

 

 

3. 页面提交的数据如何组装?

struts1:

页面提交后, 由 ActionServlet交给RequestProcessor的processPopulate()方法,由processPopulate()方法 收集请求数据,放在map中,key为表单域的name属性,如 name, account.name, stocks[0].code. 然后借助于 Common-beanutils 工具包设置到 ActionForm 的相应属性中,
如果key是简单的'name',直接form.setName(map.get('name'));
如果key是'account.name', 执行的操作是 form.getAccount().setName(map.get('account.name');
如 果key是'stocks[0].code', 它可以对应到数组或集合中,如对于数组 (form.getStocks())[0].setCode(map.get('stocks[0].code')); 对于集合((List)form.getStocks()).get(0).setCode(map.get('stocks[0].code'))。
因此form 中的非简单成员(即不是简单的String类型)必须初始化,不然会出现空指针错.

  如:private MyBean mybean = new MyBean();

        private MyObjList myList = new BeanArrayList(MyBean2.class);  // 自定义类BeanArrayList继承自ArrayList或者LinkedList,主要是为了覆盖get(index)方法,避免出现索引越界.

 

struts2:

Dispatcher接收到action请求后,首先wrapped请求对象,放入context map里边。然后创建actionproxy、actionInvocation,调度者actionInvocation负责递归调用interceptor栈和实际的action。而请求数据的组装就是由ParametersInterceptor来完成的,具体利用了ognlValueStack.

提问 :Ognl操作的根对象Action里的非简单成员为什么不需要事先初始化好(像struts1那样)?

ognlValueStack已经封装好了。

 

提问 :对应集合的数据,是否需要自定义一个List?

参考 struts2的参数传递   Array、List、Map等容器类型的参数传递 部分。

JDK1.5后最方便是使用泛型或者注释类。

 

提问 :ognl如何工作?

参考 OGNL--数据运转的催化剂

Ognl三要素:表达式(Expression),根对象(Root Object),上下文环境(context)。

Struts2 Reference 的描述:

The framework uses a standard naming context to evaluate OGNL expressions. The top level object dealing with OGNL is a Map (usually referred as a context map or context). OGNL has a notion of there being a root (or default) object within the context. In expression, the properties of the root object can be referenced without any special "marker" notion. References to other objects are marked with a pound sign (#).

 

提问 :struts2的OgnlValueStack是个什么概念?

参考 在Struts2中使用OGNL

Struts2 Reference 的描述:

The biggest addition that XWork provides on top of OGNL is the support for the ValueStack. While OGNL operates under the assumption there is only one "root", XWork's ValueStack concept requires there be many "roots".

 

 

4.如何根据请求路径找到对应的action类?

struts1:

首先根据请求路径在moduleconfig 的actionConfigs map中找出ActionMapping :

        ActionMapping mapping =
            (ActionMapping) moduleConfig.findActionConfig(path);

然后根据ActionMapping的type利用RequestUtils实例化出来。

 

struts2:

struts2的ActionMapping是只有name,namespace,method等属性的简单bean,由FilterDispatcher接收到请求时组装好。

然后 actionproxy根据name、namespace在configuration里找到对应的actionConfig:

 

该actionConfig对应于配置文件里的action,并且包含了interceptor栈:

 

然后actionInvocation根据该actionConfig创建实际的acton:

 

5. 如何返回result?

struts1:

通常是由requestDispather来处理


 

 

struts2:

参考:Result机制,让视图更丰富

Struts2将Result列为一个独立的层次,在Action执行完毕之后,框架需要把代码的执行权重新交还给Web容器,并转向到相应的页面或者其他类型的View层。 这个跳转逻辑,就由Result来完成。View层的显示类型非常多,有最常见的JSP、当下非常流行的Freemarker/Velocity模板、Redirect到一个新的地址、文本流、图片流、甚至是JSON对象等等。

 

常见的Result:

dispatcher

dispatcher主要用于返回JSP,HTML等以页面为基础View视图,这个也是Struts2默认的Result类型。

在使用dispatcher时,唯一需要指定的,是JSP或者HTML页面的位置,这个位置将被用于定位返回的页面:

 

stream

< result-type  name ="stream"  class ="org.apache.struts2.dispatcher.StreamResult" />

 

StreamResult等价于在Servlet中直接输出Stream流。这种Result被经常使用于输出图片、文档等二进制流到客户端。通过使用StreamResult,我们只需要在Action中准备好需要输出的InputStream即可。(这部分在后面文件下载部分有重述)

另参考:http://struts.apache.org/2.0.14/docs/stream-result.html

 

6.线程安全问题

Struts的线程安全问题

 

7.文件上传和下载

struts1

 

 

struts2

 

 

小结:Struts1与Struts2的区别:

http://ikingqu.javaeye.com/blog/68597

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值