struts2.xml 中使用*拦截所有action,统一处理问题

zhsw 发布于 2016/07/08 16:29
阅读 740
收藏 0
Struts2 的配置文件:
<?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>
    <constant name="struts.devMode" value="true" />
    <!--返回数据-->
    <package name="jsonPackage" extends="json-default">
        <action name="*_*" method="{1}" class="com.zsw.frame.action.{2}Action">
            <!--返回json数据-->
            <result name="jsonData" type="json">
                <param name="root">responseData</param>
            </result>
        </action>
    </package>
</struts>


这是action代码:
public class UserAction extends AbstractJsonAction{

    public String index1() {

        String json = sendJsonData(new RespData<User>().success());
        return json;
    }

    public String index() {
        String html = redirectHtml("html/error.jsp");
        System.out.println(html);
        return html;
    }

    public String uploads() {

        RespData<?> respData = new RespData<Object>();
        return sendJsonData(respData.success());
    }
}


问题:

这种方式只能访问 User_index.action中的index方法, 不能访问UserAction中的其他方法,如访问:User_index1.action,则报如下错:


Struts Problem Report

Struts has detected an unhandled exception:

Messages:
  • There is no Action mapped for namespace [/] and action name [User_index1] associated with context path [/studyFrame].

  • Stacktraces

    There is no Action mapped for namespace [/] and action name [User_index1] associated with context path [/studyFrame]. - [unknown location]
    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:192)
        org.apache.struts2.factory.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
        org.apache.struts2.factory.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
        com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
        org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:546)
        org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:81)
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:113)
        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
        org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
        org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
        org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
        org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
        org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        java.lang.Thread.run(Thread.java:745)

    You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra debugging behaviors and reports to assist developers. To disable this mode, set:

    struts.devMode=false
    in your WEB-INF/classes/struts.properties file.


    请问各位大神,这个问题要怎么解决?





    加载中
    0
    开源艺龙
    开源艺龙
    第一个“*”对应method方法,你确定你访问的对??
    0
    z
    zhsw
    即使把,action中的1,2对换也貌似访问不了,此处是我写错了,因为改了很多次了。直接写死配置是能访问各个方法的。
    0
    z
    zhsw
    我晚上测试了一下,2.3的版本这样写是可以的,原来2.5版本要在package上添加
    strict-method-invocation="false"
    0
    pantrick
    pantrick

    引用来自“zhsw”的评论

    我晚上测试了一下,2.3的版本这样写是可以的,原来2.5版本要在package上添加
    strict-method-invocation="false"
    2.5 对需要动态访问的方法要配置allowed-method 
    0
    z
    zhsw
    谢谢你们啊,,还是英文不够好,,不然看文档就不会有这些问题了
    返回顶部
    顶部