美文网首页JAVA基础知识Java学习笔记
struts2框架(二)通配符,动态方法调用,全局结果,resu

struts2框架(二)通配符,动态方法调用,全局结果,resu

作者: Acamy丶 | 来源:发表于2017-06-11 12:09 被阅读25次

    1.Struts2通配符映射机制

    一个 Web 应用可能有成百上千个 action 声明. 可以利用 struts 提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系

    1.1 通配符映射规则

    • 若找到多个匹配, 没有通配符的那个将胜出
    • 若指定的动作不存在, Struts 将会尝试把这个 URI 与任何一个包含着通配符 的动作名及进行匹配
    • 若 Struts 找到的带有通配符的匹配不止一个, 最后一个匹配将胜出
    • 被通配符匹配到的 URI 字符串的子串可以用 {1}, {2} 来引用. {1} 匹配第一个子串, {2} 匹配第二个子串…
    • {0} 匹配整个 URI
    • 通配符可以匹配零个或多个字符, 但不包括 / 字符. 如果想把 / 字符包括在内, 需要使用 **. 如果需要对某个字符进行转义, 需要使用 .

    1.2 通配符案例

    案例一: 多个路径访问同一个Action的同一个方法

    pattern.jsp映射路径



    所有的路径都是以xxxx_save.action模式出现的! 这三个路径需要访问同一个Action的 save()方法,如果没有通配符配置,那么就需要重复配置三个Action。
    通配符映射规则:

    案例二: 不同的路径执行不同Action的相同方法

    pattern.jsp映射路径


    所有的路径都是以Action类名XxxAction_save开头的。
    普通映射规则

    使用通配符简化配置

    使用*先将请求的路径匹配上,然后根据匹配的结果值传递给{1}中的参数。

    案例三: 不同的路径执行同一个Action中不同的方法CRUD

    pattern.jsp映射路径


    通配符映射规则:

    2.Struts2动态方法调用

    动态方法调用: 通过 url 动态调用 Action 中的方法

     如果Action中存在多个方法时,我们可以使用!+方法名调用指定方法
    

    默认情况下, Struts 的动态方法调用处于激活状态, 若想禁用该功能, 则可以在 struts.xml 文件中添加如下 constant 元素:
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    实现

    注意:如果开发者手工的将动态方法调用设置为不可用,那么使用!直接报错。

    3.Struts2全局结果

    当多个action中都使用到了相同result,为了避免result的重复,这时我们
    应该把result定义为全局结果。

    实现


    注意:
    如果全局和局部有同名的result,那么局部会覆盖全局的result。
    同一个应用中每次请求Struts2框架都会创建一个新的Action实例。
    ( 开发者可以定义构造函数输出来测试 )

    4. Struts2的resultType

    resultType可以在配置文件struts-default.xml里看到,如下

    <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>
    

    4.1 resultType之dispatcher

    默认的结果类型,以下两种情况等价:


    参见dispatcher对应的class: ServletDispatcherResult API 推荐写法如下:

    **注意: ** dispatcher只是请求转发,不能转发到外部的资源,只能在本应用中转发, 如果要跨应用访问资源,需要使用重定向。

    4.2 resultType之redirect

    参见redirect对应的class: ServletRedirectResult API 推荐写法如下:


    location: 指定跳转的路径,/开头默认添加应用名。
    parse : 指定是否把 location 参数的值视为一个 OGNL 表达式来解释. 默认值为 true

    4.3 resultType之redirectAction

    参见redirectAction对应的class: ServletActionRedirectResult API 推荐写法如下:

    actionName: 指定跳转的Action的名字, 该Action已经定义。
    namespace: 指定该Action所在的命名空间。

    相关文章

      网友评论

        本文标题:struts2框架(二)通配符,动态方法调用,全局结果,resu

        本文链接:https://www.haomeiwen.com/subject/vvsqqxtx.html