snail的Struts学习笔记(1)

snailStruts学习笔记1 --- Simple Form Example的流程

 

06 10 20

http://struts.apache.org/网站在下载struts- 1.3.5 -all.zip

 

参考资料:

在网上找了2本书Jakarta Struts 编程.pdfStruts in Action 中文修正版.pdf

加上官方的http://struts.apache.org/1.x/userGuide/index.html 的英文文档

 

 

看了Struts in Action的第二章节,内容上没有什么难理解的地方,但是光看没什么实际的认识。还

得操作一下,所以就把struts- 1.3.5 -all.zip解压缩,找里面的一个例子实际学习一下!(找个最简单的)

struts- 1.3.5 -all/struts-1.3.5/apps/struts-cookbook-1.3.5.war

 

整个的程序的所有文件:

Source Code for Simple Form Example


JavaServer Pages

Simple.jsp

SimpleResults.jsp

Actions

SuccessAction.java

ProcessSimpleAction.java

ActionForm

SimpleActionForm.java

Configuration files

struts-config.xml

Other source files

None

 

 

现在把整个流程走一下L(不知道有没有错)

 

1 用户浏览器访问Simple.jsp,填写表单,点击submit按钮。(客户端发生的事情)

 

这是Simple.jsp页面的一部分。

<html:form action="/processSimple">

         <p>* What's your first name?:<br/><html:text property="name" size="40" maxlength="50"/></p>

         <p>* Enter a secret word or phrase:<br/><html:password property="secret" size="40" maxlength="50"/></p>

。。。。。

 

2 当用户提交Submit按钮时,提交表单。用户的请求就发送到服务器,服务器把请求传给ActionServlet。(服务端发生的事情)

ActionServlet在自己的配置表里查找表单指定的/processSimple

/processSimplestruts-config.xml已配置,见下图。应用服务器一开启,ActionServlet就将struts-config里的所有配置加载进自己的某个或多个实例变量中去了。

所以上面ActionServlet能找到/processSimple”,见下图/processSimple的配置)

 

"/processSimple"struts-config.xml中的配置。

<action path="/processSimple"

                type="examples.simple.ProcessSimpleAction"

                name="simpleForm"//表单的名字,不是实际javaBean类的名字

                scope="request"

                input="/jsp/simple/Simple.jsp"

                cancellable="true" //表单可以被取消

                validate="true">  //表单在提交到ProcessSimpleAction前需要被验证

            <forward name="success" path="/jsp/simple/SimpleResults.jsp"/>

        </action>

 

 

Struts in Action第二章书上的一张图,下面参考这张图详细描述下接下来发生的事情。

 

根据图中所示:

1 ActionServlet查找到/processSimple时,查找属性中有没有form bean,结果发现了simpleForm”.

2 然后在又在自己的配置里找到了simpleForm所对应的SimpleActionForm的类。

" simpleForm "struts-config.xml中的配置。

  <form-beans>

        <!-- Simple ActionForm Bean -->

        <form-bean name="simpleForm" type="examples.simple.SimpleActionForm"/>

 </form-beans>

3如果是服务器开始后的第一次访问这个bean,则创建SimpleActionForm的一个新的实例。否则则重设它。

4 查找/processSimplevalidate的属性是否是”true”.如果为true的话,就调用SimpleActionForm

validata()方法

public ActionErrors validate(

        ActionMapping mapping,

        HttpServletRequest request) {

 

        ActionErrors errors = new ActionErrors();

 

        // Name must be entered

        if ((name == null) || (name.length() < 1)) {

            errors.add("name", new ActionMessage("errors.name.required"));

        }

 

        // Secret Phrase must be entered

        if ((secret == null) || (secret.length() < 1)) {

            errors.add("secret", new ActionMessage("errors.secret.required"));

        }

        return (errors);

}                            

5 现在假设validata()验证正确,则ActionServlet创建ActionProcessSimpleAction

  ProcessSimpleAction类如果已实例化过的话,则直接调用,否则创建)。

6 ProcessSimpleActionexecute()方法被调用

public ActionForward execute(

        ActionMapping mapping,

        ActionForm form,

        HttpServletRequest request,

        HttpServletResponse response)

        throws Exception {

 

        // If user pressed 'Cancel' button,

        // return to home page

        if (isCancelled(request)) { 

            return mapping.findForward("home");

        }

 

        // Forward to result page

        return mapping.findForward("success");

    }

7         ProcessSimpleAction返回ActionForward对象给ActionServlet,这里假设执行到

     return mapping.findForward("success");

ActionServlet在自己本身的配置中找到名为” success”ActionForward类。

"/processSimple"struts-config.xml中的配置中有

<forward name="success" path="/jsp/simple/SimpleResults.jsp"/>这个ActionForward类的配置)

 

3 服务起通过ActionServlet把响应发送到了客户那里,则在客户那里显示如下。(客户端发生的事情)

虽然在浏览器地址栏显示的是http://localhost:8888/struts-cookbook-1.3.5/processSimple.do

但实际用户所在的页面是上面

"/processSimple"struts-config.xml中的配置中的

<forward name="success" path="/jsp/simple/SimpleResults.jsp"/>

SimpleResults.jsp这个页面

 

 

 

这个例子虽然很简单,但大致把strute做的的主要工作体现了出来。

相对于下面这张表,上面的例子么有涉及到UserDelegate, HelperBean这两个过程,不过相信对上面的流程很熟悉的话,再找个复杂一点点的例子

把这两个过程也走一遍的话,相信对Struts的工作模式有个更好的里。

 

现在对Struts 的整个工作模式有了很好的理解,可以正式开始看书了,这样的话对书上的所讲的东西就不会不知所云了。明天继续学习Struts,今天

到此为止了。

 

反正第一天学习下来,感觉struts框架就是什么事情都问ActionServletActionServlet什么事情都去找Struts-config.xml

 

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值