zoukankan      html  css  js  c++  java
  • struts2讲义----建立一个struts2工程

    建立一个Struts2 工程

    Ø 1MyEclipse中新建web工程

    Ø 2struts-2.2.1.1-allstruts-2.2.1.1解压struts2-blank.war( 最基础的示例程序 )

    Ø 3进入struts-2.2.1.1appsstruts2-blankWEB-INFclasses下把struts.xml拷到web工程的src下面,因为工程编译完它默认就把src下的文件放到class文件下面。



    Ø 4.拷类库,在这个项目的lib文件下面拷


    jar放入lib后看不见jar文件,是因为MyEclipse默认视图是package Explorer,如果要看硬盘上对应的视图,应该打开windows-Show View-other-navigatior


    4.配置web.xml,参考struts自带的web.xml,把filter的配置拷过来

    <filter>
            <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter[Ng:next generation下一代的filter,用的是2.1的;跟2.0有区别,
    2.0用的filter用的是:org.apache.struts2.
    dispatcher.FilterDispatcher].StrutsPrepareAndExecuteFilter[通过这个名字可以看出跟2.0的区别,这里调用了两个filter,一个是prepare一个是execute filter.]
    </filter-class>
     </filter>
     <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>
    批注:

    gnext generation下一代的filter,用的是2.1的;跟2.0有区别,

    2.0用的filter用的是:org.apache.struts2.

    dispatcher.FilterDispatcher

    通过这个名字可以看出跟2.0的区别,这里调用了两个filter,一个是prepare一个是execute filter.


     第一个示例程序Hello Struts

    <struts>
    <constant name="struts.devMode" value="true" />[Struts常量的配置,struts.devMode开发模式,开发模式改为true之后,修改配置文件可以马上生效,不用重启服务器。]
    <package name="default" namespace="/" extends="struts-default">
            <action name="hello">
                <result>/hello.jsp</result>
            </action>
        </package>
    </struts>

    http://localhost:8080/strust2_0100_Introduction/ 

    http://localhost:8080/strust2_0100_Introduction/hello 或者

    http://localhost:8080/strust2_0100_Introduction/hello.action

    跳转到hello.jsp,第一个示例程序成功!



     Struts2读源码

    配置文件中的

     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>

    一看就应该知道它是对应一个类,在jar文件中找到对应的源码编译完的class文件,


    查看源码: jar文件点右键---àproperties--àJava Source AttachmentàExternal Folder 

    (外部文件)

    struts-2.2.1.1-all/struts-2.2.1.1/src/core/src/main/java 


    点击class文件可以查看源码了,假如想看它的doc文档,同样的方法

    jar文件点右键---àproperties--àJavadoc Location-à导入doc就可以在源码中右键或者F1观察对应的文档了。

     敲尖括号不提示的问题

    Struts.xml文件头定义了

    <!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

    配置:windows---preferences---catalog---。。。xmlxml CatalogAdd

    Add入本地定义当前xmldtd文件:找到struts2-core-2.2.1.1.jar解压开找到struts-2.1.7.dtd


    完成,验证代码提示成功!

     Struts2的运行机制

    当你在客户端敲http://localhost:8080/strust2_0100_Introduction/hello

    首先找到:strust2_0100_Introduction这个web application,找到后去执行这个web application下的web.xml

    Tomcat接收到请求之后,会发现这个web.xml下面,配了一个filter,而这个filter过滤所有的url地址,所以当我们在地址栏敲http://localhost:8080/strust2_0100_Introduction/hello后,会被StrutsPrepareAndExecuteFilter接收到

    <filter>
            <filter-name>struts2</filter-name>    
    <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>


    StrutsPrepareAndExecuteFilter
    接收到后url情求后,它首先看namespace

    Struts.xml
    <package name="default" namespace="/" extends="struts-default">
            <action name="hello" >
                <result>/hello.jsp</result>
            </action>
      </package>

    查到“/”后面的hello,它就会去package下面查是否有name属性叫“hello”的action,有的话,找里面对应的result是谁--àhello.jsp




    Struts的好处就是:我可以把“请求”和“视图展现”分开,而不是写死。分开的好处就是:如果要换成其他视图,配一下就好了,所以更加灵活。Struts核心的本质就是解决了:把你的请求和最后的结果分开。


  • 相关阅读:
    html5存储相关 coookie localstorage session storage
    LeetCode 3. 无重复字符的最长子串(Longest Substring Without Repeating Characters)
    LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27
    LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)
    LeetCode 151. 翻转字符串里的单词(Reverse Words in a String)
    【剑指offer】面试题 31. 栈的压入、弹出序列
    LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
    LeetCode 946. 验证栈序列(Validate Stack Sequences) 26
    【剑指offer】面试题 14. 剪绳子
    LeetCode 343. 整数拆分(Integer Break) 25
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3221539.html
Copyright © 2011-2022 走看看