关于Struts2的通配符的匹配问题

用SSH搭的项目
Struts.xml
package的namespcae没写,所以默认是"/"

<action name="emp-*" class="employeeAction" method="{1}" >
        <result name="{1}">/WEB-INF/pages/emp-{1}.jsp</result>
        <result name="success" type="redirectAction">emp-list</result>
</action>

employeeAction.java

 public String test() throws IOException{
                        return NONE;
 }

超链接测试

 <a href="emp-test?name=AAA">emp-test</a>

报错信息

 [16:25:13:385] [DEBUG] - com.opensymphony.xwork2.config.impl.AbstractMatcher.match(AbstractMatcher.java:117) - Attempting to match 'emp-test' to a wildcard pattern, 2 available
[16:25:13:385] [DEBUG] - com.opensymphony.xwork2.config.impl.AbstractMatcher.match(AbstractMatcher.java:122) - Value matches pattern 'emp-*'
[16:25:13:455] [WARN] - org.apache.struts2.dispatcher.Dispatcher.logConfigurationException(Dispatcher.java:601) - Could not find action or result: /emp-test?name=AAA
com.opensymphony.xwork2.config.ConfigurationException: There is no Action mapped for namespace [/] and action name [emp-test] associated with context path [].
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:192) ~[struts2-core-2.5.10.jar:2.5.10]
    at org.apache.struts2.factory.StrutsActionProxy.prepare(StrutsActionProxy.java:63) ~[struts2-core-2.5.10.jar:2.5.10]
    at org.apache.struts2.factory.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37) ~[struts2-core-2.5.10.jar:2.5.10]
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58) ~[struts2-core-2.5.10.jar:2.5.10]
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:554) [struts2-core-2.5.10.jar:2.5.10]
    at org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:81) [struts2-core-2.5.10.jar:2.5.10]
    at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:143) [struts2-core-2.5.10.jar:2.5.10]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [catalina.jar:9.0.0.M18]
    at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71) [log4j-web-2.9.0.jar:2.9.0]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:9.0.0.M18]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) [catalina.jar:9.0.0.M18]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495) [tomcat-coyote.jar:9.0.0.M18]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:9.0.0.M18]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767) [tomcat-coyote.jar:9.0.0.M18]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1354) [tomcat-coyote.jar:9.0.0.M18]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:9.0.0.M18]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_121]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_121]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:9.0.0.M18]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]

然后我尝试在
struts.xml里加了

 <action name="emp-test" class="employeeAction" method="test">
   </action>

然后居然就不报错了,惊了,这是为什么

接着又试验了其他的
employeeAction.java

  public String list(){

        System.out.println("list");
        return NONE;
   }

   public String list2(){

        System.out.println("list2");
        return NONE;
   }

struts.xml

 <action name="emp-*" class="employeeAction" method="{1}" >
            <result name="{1}">/WEB-INF/pages/emp-{1}.jsp</result>
            <result name="success" type="redirectAction">emp-list</result>
  </action>

超链接

<a href="emp-list">emp-list</a>
<a href="emp-list2">emp-list2</a>

点list没问题
点list2就报错
惊了,这是什么鬼,
跪求大神指导

4个回答

自己解决了,给出解决方案
Struts2.5的行如【test-*】这样的通配符中的【*】只能匹配 delete,list,input,save等少量的关键字,
底层原因不明

Tommy____
Tommy____ 在struts2.3之前的版本,正常的配置就可以了,但在struts2.3版本之后,使用通配符调用方法时,内部会验证是否允许访问该方法,所以要加上 <allowed-methods>方法名1,方法名2…</allowed-methods>代码。
接近 2 年之前 回复

http://blog.csdn.net/liu578182160/article/details/17266879

重点:
反复确认之后,还是没找到错误

找度娘,好多种解决方案,木有一个可以解决我的问题的

最后发现是我的struts.xml是在src目录下,没有任何疑问

但是我的编译后classes文件默认却是在build目录下

不在WEB-INF下.......

这样它是找不到struts.xml文件的,报如上错误,也是情有可原的

最后,明确一点,struts.xm位于src下是为了编译后能找到struts配置文件,确保其在WEB-INF下才是根本!!!

更改eclipse web 项目默认编译输出路径:

eclipse中只能针对项目更改,因为其默认的是build目录下的,只能以项目更改:
项目右键 -》properties -》Java Build Path -》source -》Default output folder,选择你的路径,ok!

@学编程的司马光
谢谢你。
不过我用的是idea,项目是Maven管理的,struts.xml是放在resources下的
点list没问题,说明struts.xml位置应该是没问题的
点list2就报错

错误信息提示
[16:25:13:455] [WARN] - org.apache.struts2.dispatcher.Dispatcher.logConfigurationException(Dispatcher.java:601) - Could not find action or result: /emp-test?name=AAA

public String test() throws IOException{
return NONE;
}

的返回值,,在action中找不到与之相匹配的字符串 要么就你的Struts.xml中过滤器配置的方式不对!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问