JavaJava 杂谈Java学习笔记

Struts 2.5 动态方法调用(DMI)问题

2017-06-11  本文已影响334人  春泥村雨

错误:

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [helloworld_add] associated with context path [/Struts2Demo].

Struts 2.5 中的通配符使用出错

解决方法:

DMI(Dynamic Method Invocation,动态方法调用),动态方法调用是为了解决一个 Action 对应多个请求的处理,以免 Action 太多。有三种方法:

在 Struts 2.5 中,为了限制 DMI,默认启用了严格的方法访问,增加了新的标签(strict-method-invocation)设置,strict-method-invocation 的值默认为 true 。

Struts 2.5 ,Struts 官网文档建议使用的头部信息(struts.xml 如下):

Struts 2.5 DTD

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
...
</struts>

Struts 2.5 中 SMI(strict-method-invocation)的使用方式:

SMI via struts.xml

<package ...>
    ...
    <global-allowed-methods>execute,input,back,cancel,browse</global-allowed-methods>
    ...
    <action name="Bar">
        <allowed-methods>foo,bar</allowed-methods>
    </action>
    ...
</package>

示例代码:

  1. strict-method-invocation="false"
<package name="default" namespace="/" extends="struts-default" strict-method-invocation="false">
    <action name="*_*" class="com.imooc.action.{1}Action" method="{2}">
        <result>/result.jsp</result>
        <result name="add">/{2}.jsp</result>
        <result name="update">/{2}.jsp</result>
    </action>
</package>
  1. strict-method-invocation="true",同时设置 <allowed-methods>
<package name="default" namespace="/" extends="struts-default"
    strict-method-invocation="true">
    <action name="*_*" class="com.imooc.action.{1}Action" method="{2}">
        <result>/result.jsp</result>
        <result name="add">/{2}.jsp</result>
        <result name="update">/{2}.jsp</result>
        <allowed-methods>add,update</allowed-methods>
    </action>
</package>
  1. strict-method-invocation="true",同时设置 <global-allowed-methods> 标签
<package name="default" namespace="/" extends="struts-default"
    strict-method-invocation="true">
    <global-allowed-methods>add,update</global-allowed-methods>
    <action name="*_*" class="com.imooc.action.{1}Action" method="{2}">
            <result>/result.jsp</result>
            <result name="add">/{2}.jsp</result>
            <result name="update">/{2}.jsp</result>
    </action>
</package>

参考链接:
http://www.jianshu.com/p/3aaadaef5c80
https://struts.apache.org/docs/action-configuration.html

上一篇下一篇

猜你喜欢

热点阅读