传统三大框架(SSH)之初识Struts2

 Struts2(Struts1+webworks的整合)

 Struts和SpringMVC都是基于servlet来做的控制器   struts是Apache的一个子项目

 servlet默认是单例模式,而Struts2的action默认的是prototype模式(一个请求创建一次对象

    Prototype模式允许对象在不了解要创建对象的确切类以及如何创建等细节的情况下创建自定义对象。)

  定位:Apache Struts is a free, open-source, MVC framework for creating elegant, modern Java web applications.

  废话少说,直接上例子 servlet

HelloSvl

Hello.jsp                                  测试结果

上述为传统的servlet容器进行接收数据并转向(较为简洁 目的是与Struts做对比)

Struts容器的实现步骤

  a、引包(对于初学者,MyEclipse有向导,后期指导意义就不大了)

    MyEclipse-->Project Capabilities-->Add Struts Capabilities.....  --->Struts specification(选择Struts2.1)

  

推荐使用*.action    *.do(Spring MVC)

添加之后   会自动在src下创建一个strtus.xml文件  它的名字和位置固定  不能变

在web.xml里边增加了下列的一个核心过滤器

b、在struts.xml中开始写配置文件

   

  package name为自定义的、action name为*.action中的* 、result name则是HelloAction中return的结果

  result内部为结果返回路径                                                  

 转向类型   在本文中用的是默认 即第二个

  <result-types>
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
        </result-types>

c、编写HelloAction 

 

 Hello.jsp                              测试结果

Struts项目的启动    (当进行到init.initDispatcher(config),对下列的几个文件进行Parsing)

  2016-10-15 11:36:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
  信息: Parsing configuration file [struts-default.xml]
  2016-10-15 11:36:03 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
  信息: Parsing configuration file [struts-plugin.xml]
  2016-10-15 11:36:03 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
  信息: Parsing configuration file [struts.xml]

运行一次具体的流程(浏览器中的*.action中的*即Struts中的action name)

Struts项目的启动:
     1.
       2016-10-14 10:58:33 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
    信息: Parsing configuration file [struts-default.xml]
    2016-10-14 10:58:33 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
    信息: Parsing configuration file [struts-plugin.xml]
    2016-10-14 10:58:33 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
    信息: Parsing configuration file [struts.xml]


    2. 访问http://localhost:8089/Hello/hello.action?msg=hello

       被StrutsPrepareAndExecuteFilter的doFilter拦截
      
       A、切取URL,获得ActionMapping对象    (读出action名字,method名字,namespace名字)
       B、读取Struts.xml中的配置项 ,通过反射技术,动态创建Action对象
       C、通过反射,调用Action中的execute()方法
       D、返回Action方法中的result值
       E、读取Struts.xml中的配置项 ,自动转向到需要的jsp页面

 

posted @ 2016-10-17 18:54  不喜欢吃香蕉的臭猴子  阅读(374)  评论(0编辑  收藏  举报