zoukankan      html  css  js  c++  java
  • struts2开篇

    一:struts2介绍

              Struts2是在WebWork2基础发展而来的。和struts1一样, Struts2也属于MVC框架。不过有一点大家需要注意的是:尽管Struts2和struts1在名字上的差别不是很大,但Struts2和struts1在代码编写风格上几乎是不一样的。

              主要是因为struts2有以下优点:

    1 > 在软件设计上Struts2没有像struts1那样跟Servlet API和struts API有着紧密的耦合,Struts2的应用可以不依赖于Servlet API和struts API。 Struts2的这种设计属于无侵入式设计,而Struts1却属于侵入式设计。

    public class OrderListAction extends Action {
        public ActionForward execute(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        }
    }

    2> Struts2提供了拦截器,利用拦截器可以进行AOP编程,实现如权限拦截等功能。

    3> Strut2提供了类型转换器,我们可以把特殊的请求参数转换成需要的类型。在Struts1中,如果我们要实现同样的功能,就必须向Struts1的底层实现BeanUtil注册类型转换器才行。

    4> Struts2提供支持多种表现层技术,如:JSP、freeMarker、Velocity等

    5> Struts2的输入校验可以对指定方法进行校验,解决了Struts1长久之痛。

    6> 提供了全局范围、包范围和Action范围的国际化资源文件管理实现

    二:搭建struts2开发环境

           搭建Struts2环境时,我们一般需要做以下几个步骤的工作:

    1》找到开发Struts2应用需要使用到的jar文件.

    2》编写Struts2的配置文件struts.xml ,该文件需要存放在WEB-INF/classes下

    3》在web.xml中加入Struts2 MVC框架启动配置

    三:Action名称的搜索顺序

        获得请求路径的URI,例如url是:http://server/struts2/path1/path2/path3/test.action

    1.首先寻找namespace为/path1/path2/path3的package,如果存在这个package,则在这个package中寻找名字为test的action,如果不存在这个package则转步骤3;

    2.寻找namespace为/path1/path2的package,如果存在这个package,则在这个package中寻找名字为test的action,如果不存在这个package,则转步骤4;

    3.寻找namespace为/path1的package,如果存在这个package,则在这个package中寻找名字为test的action,如果仍然不存在这个package,就去默认的namaspace的package下面去找名字为test的action(默认的命名空间为空字符串“” ),如果还是找不到,页面提示找不到action。

    四:url访问方式

    访问struts2中action的URL路径由两部份组成:包的命名空间+action的名称,例如访问本例子HelloWorldAction的URL路径为:/test/helloworld (注意:完整路径为:http://localhost:端口/内容路径/test/helloworld)。另外我们也可以加上.action后缀访问此Action。

    1 <package name="itcast" namespace="/test" extends="struts-default">
    2         <action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute" >
    3     <result name="success">/WEB-INF/page/hello.jsp</result>
    4         </action>
    5  </package> 

    五:Action配置中的各项默认值

    1 <package name="itcast" namespace="/test" extends="struts-default">
    2 
    3         <action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute" >
    4 
    5              <result name="success">/WEB-INF/page/hello.jsp</result>
    6 
    7         </action>
    8 
    9   </package>

    1>如果没有为action指定class,默认是ActionSupport。

    2>如果没有为action指定method,默认执行action中的execute() 方法。

    3>如果没有指定result的name属性,默认值为success。

  • 相关阅读:
    树线段hdu 4508 美素数(线段树)
    自定义context自定义Dialog之Progress(二)
    实现语言C语言简单实现五子棋
    调用博客paip.基于HTML gui界面的javascript JS实现SLEEP。。
    水印控件windows phone中,制作一个自定义的密码输入框控件,含图片,有水印,星号显示
    请求网络网络编程
    调试网页PAIP HTML的调试与分析工具
    输出流输入输入输出流
    标记协议http协议与XML书写规范及解析技术
    描述算法10673 Play with Floor and Ceil
  • 原文地址:https://www.cnblogs.com/feimo/p/2943647.html
Copyright © 2011-2022 走看看